本章主要讲述Spring MVC中的重定向redirect,涉及到的知识点有:
1. redirect至页面
2. redirect至请求
3. redirect传参
4. 页面Post请求中文乱码 : CharacterEncodingFilter
1.程序目录
src
\---main
\---java
| \---pers
| \---hanchao
| \---hispringmvc
| \---redirect
| \---LoginAction.java
\---resources
| \---applicationcontext.xml
\---webapp
\---redirect
| \---login.jsp
| \---main.jsp
| \---redirect.jsp
\---WEB-INF
| \---spring-mvc-servlet.xml
| \---web.xml
\---index.jsp
1.LoginAction.java
package pers.hanchao.hespringmvc.redirect;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
/**
* <p>redirect</p>
* @author hanchao 2018/1/13 20:52
**/
@Controller
@RequestMapping("login")
public class LoginAction {
/**
* <p>redirect向至请求</p>
* @author hanchao 2018/1/13 20:53
**/
@PostMapping("/login")
public String login(@RequestParam(value = "name")String name, RedirectAttributes redirectAttributes){
System.out.println(name);
redirectAttributes.addFlashAttribute("name",name);
return "redirect:/login/main";
}
/**
* <p>forward向至View</p>
* @author hanchao 2018/1/13 20:53
**/
@GetMapping("/main")
public String index(@ModelAttribute("name")String name,Model model){
System.out.println(name);
model.addAttribute("name",name);
return "redirect/main";
}
/**
* <p>redirect至页面</p>
* @author hanchao 2018/1/13 20:54
**/
@RequestMapping("/redirect2jsp")
public String redirectToJsp(){
return "redirect:/redirect/redirect.jsp";
}
}
2.applicationcontext.xml
空
3.login.jsp
<%--
Created by IntelliJ IDEA.
User: hanchao
Date: 2018/1/13
Time: 19:46
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<html>
<head>
<title>登录页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<hr/>
<h3>重定向至页面</h3>
<a href="/login/redirect2jsp">redirect2jsp</a>
<hr/>
<h3>重定向请求,并传递参数</h3>
<form action="/login/login" method="post">
用户名:<input type="text" name="name"/>
<input type="submit" name="登录"/>
</form>
</body>
</html>
4.main.jsp
<%--
Created by IntelliJ IDEA.
User: hanchao
Date: 2018/1/13
Time: 19:46
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<html>
<head>
<title>首页</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h3>Welcome ${name}!</h3>
</body>
</html>
5.redirect.jsp
<%--
Created by IntelliJ IDEA.
User: hanchao
Date: 2018/1/13
Time: 20:06
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>redirect</title>
</head>
<body>
<h3>重定向至页面</h3>
</body>
</html>
6.spring-mvc-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">
<!--开启component注解自动扫描-->
<context:component-scan base-package="pers.hanchao.*"/>
<!--开启注解-->
<context:annotation-config/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
7.web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>Hi Spring MVC</display-name>
<!--request请求转码过滤器,解决POST请求乱码问题-->
<filter>
<filter-name>characterEncodingFilter</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>
<!--forceEncoding = forceRequestEncoding + forceResponseEncoding-->
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--spring-mvc前端分发器DispatcherServlet-->
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--spring上下文配置加载器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<!--spring配置文件总入口-->
<param-value>classpath:applicationcontext.xml</param-value>
</context-param>
<!--上下文加载监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--欢迎页面-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
8.index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!</h2>
<a href="/helloworld">Hello World!</a><br>
<a href="redirect/login.jsp">redirect实例</a>
</body>
</html>