一、跳转方式
转发
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.comprehensive.controller"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
</beans>
@Controller
@RequestMapping("/skip")
public class Test_skip {
@RequestMapping("/test_forward")
public String test_forward(Model model) {
model.addAttribute("msg", "forward测试成功");
return "forward:/WEB-INF/jsp/test.jsp";
}
}

重定向
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
${msg}
</body>
</html>
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.comprehensive.controller"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
</beans>
@Controller
@RequestMapping("/skip")
public class Test_skip {
@RequestMapping("/test_redirect")
public String test_redirect(Model model) {
model.addAttribute("msg", "redirect测试成功");
return "redirect:/index.jsp";
}
}

二、数据处理
配置环境
<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.comprehensive.controller"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
提交的域名称和处理方法的参数名一致
@Controller
@RequestMapping("/user")
public class UserController {
// http://localhost:8080/user/test1?name=comprehensive
@RequestMapping("/test1")
public String test1(String name, Model model) {
System.out.println(name);
model.addAttribute("msg", name);
return "test1";
}
}

提交的域名称和处理方法的参数名不一致
@Controller
@RequestMapping("/user")
public class UserController {
// http://localhost:8080/user/test2?username=comprehensive
@RequestMapping("/test2")
public String test2(@RequestParam("username") String name, Model model) {
System.out.println(name);
model.addAttribute("msg", name);
return "test2";
}
}

提交的是一个对象
有参构造方法
public class User {
private int id;
private String name;
private String pwd;
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/test3")
public String test3(User user, Model model) {
System.out.println(user);
model.addAttribute("msg", user);
return "test3";
}
}
setter方法
public class User {
private int id;
private String name;
private String pwd;
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/test3")
public String test3(User user, Model model) {
System.out.println(user);
model.addAttribute("msg", user);
return "test3";
}
}
三、数据显示到前端
ModelAndView
ModelMap
Model
四、乱码问题
乱码问题发生
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>提交表单</title>
</head>
<body>
<form action="/encoding/test" method="post">
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>页面</title>
</head>
<body>
${msg}
</body>
</html>
@Controller
@RequestMapping("/encoding")
public class EncodingController {
@RequestMapping("/test")
public String test(String name, Model model) {
model.addAttribute("msg", name);
return "test";
}
}
解决乱码问题
修改tomcat配置文件:设置编码

web.xml文件中配置过滤器
<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置SpringMVC过滤器-->
<filter>
<filter-name>encoding_filter</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>
<filter-mapping>
<filter-name>encoding_filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
