IDEA2021.1 Spring MVC 简易配置

先创建一个空白新项目,再右击项目处添加框架支持

src处新建一个package,WEB-INF下新建一个jsp文件夹存放jsp文件

文件目录如下


 
新建一个class TestController 内容如下

package test.springmvc.helloworld;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/test") //类中所有响应请求的父路径

public class TestController {

    @RequestMapping("/index") //此处的函数通过该路径响应
    public String helloworld(@RequestParam(value = "id") String id, Model model){
        model.addAttribute("url","http://www.baidu.com/"); // 指定Model的值
        model.addAttribute("id",id);
        return "/WEB-INF/jsp/helloworld.jsp";
    }

}


@RequestParam(value = "id")用于获取传参id值 通过Model将变量传入视图
helloworld.jsp处的内容为
 

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
Hello World
<br/>id:${id}
<br/>url:${url}
</body>
</html>

配置dispatcher-servlet.xml设置包查找

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:context="http://www.springframework.org/schema/context"

       xsi:schemaLocation="

       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="test.springmvc.helloworld"/>

</beans>

接着配置web.xml 修改url-pattern即映射的url路径

下面对tomcat进行配置,以启动程序

先配置Artifact

单击Add Configuration

启动之后会在浏览器打开idea提供的demo

访问http://localhost:8080/test/index?id=111

猜你喜欢

转载自blog.csdn.net/weixin_43610673/article/details/122653795
今日推荐