- 基于maven搭建SpringMVC项目
在pom.xml添加依赖
- 基于maven搭建SpringMVC项目
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.8</version>
</dependency>
配置web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- SpringMVC入口,所有请求都由这个DispatcherServlet来处理 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- springMVC配置文件位置 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<!-- 请求参数转义过滤器 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<!-- DispatcherServlet入口,表示SpringMVC需要处理哪些请求 -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
spring-mvc.xml配置如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 开启注解驱动 -->
<!-- @Controller @RequestMapping等SpringMVC相关注解才能生效 -->
<mvc:annotation-driven/>
<!-- 需要扫描的包路径 -->
<context:component-scan base-package="org.example"/>
<!-- 视图解析器配置 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
新建TestController
@Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping("/test1/{id}")
public ModelAndView test1(ModelAndView modelAndView, Map map, Model model, String name,
@PathVariable("id") String id, HttpServletRequest request) {
request.setAttribute("result", name);
modelAndView.setViewName("index");
System.out.println(id);
map.put("temp", "123");
return modelAndView;
}
}
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
系统首页, ${requestScope.result}, ${requestScope.temp}
</body>
</html>
启动tomcat并访问地址:http://localhost:8080/test/test1/1?name=testUser
结果如下:
系统首页, testUser, 123
注解说明:
- @Controller 表明一个类是一个控制器
- @RestController 表明当前控制器是restfull风格的
- @RequestMapping 表明某个类或者方法的访问地址
- @RequestParam 配置请求参数,其有一个required属性,默认为true, 如果请求参数中没有该参数,则会报400 Bad Request
- @PathVariable 路径匹配,从请求路径中提取值
- @ReuquestBody 将整个request body解析为一个实体类
- @ResponseBody 将返回结果作为相应的body部分