@RequestMapping("/testDispatcher") public String testDispatcher() { return "forward:/login"; }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> <display-name>first-springmvc-demo</display-name> <!--请求和响应的字符串过滤器--> <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> <!--设置响应的编码,这里我们可以省略--> <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置SpringMVC的前端控制器,对浏览器发送的请求统一进行处理 --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--配置 DispatcherServlet 的一个初始化参数:spring mvc 配置文件按的位置和名称--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springMVC.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <!--设置springMVC的核心控制器所能处理的请求的请求路径/所匹配的请求可以是/login或.html或.js或.css方式的请求路径但是/不能匹配.jsp请求路径的请求--> <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="net.biancheng.c"></context:component-scan> <!-- 配置 Thymeleaf 视图解析器 --> <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver"> <!--视图解析器的优先级,值越小,优先级越高--> <property name="order" value="2"/> <!--定义视图文件的字符集--> <property name="characterEncoding" value="UTF-8"/> <property name="templateEngine"> <bean class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolver"> <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> <!--设置视图前缀 --> <property name="prefix" value="/WEB-INF/templates/"/> <!--设置视图后缀 --> <property name="suffix" value=".html"/> <property name="templateMode" value="HTML5"/> <property name="characterEncoding" value="UTF-8"/> </bean> </property> </bean> </property> </bean> <!--视图控制器,用于页面跳转--> <mvc:view-controller path="/user" view-name="userPage"></mvc:view-controller> <mvc:annotation-driven/> </beans>
package net.biancheng.c.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; /** * @author c语言中文网 */ @Controller public class ForwardController { /** * 通过控制器方法返回值实现转发 * @param model * @return */ @RequestMapping("/testForward") public String testDispatcher(Model model) { //通过 Model 向 request 域中添加信息 model.addAttribute("requestScope", "控制器返回值:转发!"); //通过返回值将请求转发的 /userPage 上 return "forward:/user"; } }
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <H1 th:text="${requestScope}" th:if="${not #strings.isEmpty(requestScope)}"></H1> </body> </html>
图1:转发-1
@RequestMapping("/testDispatcher") public ModelAndView testDispatcher() { ModelAndView modelAndView = new ModelAndView(); //设置逻辑视图名 modelAndView.setViewName("forward:/login"); return modelAndView; }
/** * 通过 ModeAndView 实现转发 * @return */ @RequestMapping("/testForward2") public ModelAndView testForward2() { ModelAndView modelAndView = new ModelAndView(); //设置逻辑视图名 modelAndView.setViewName("forward:/user"); //在request域对象中添加信息 modelAndView.addObject("requestScope", "ModelAndView:转发!"); return modelAndView; }
图2:请求转发-2
@RequestMapping("/testRedirect") public String testRedirect() { return "redirect:/login"; }
package net.biancheng.c.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpSession; /** * @author c语言中文网 */ @Controller public class RedirectController { /** * 通过 String 类型的返回值实现重定向 * @param session * @return */ @RequestMapping("/testRedirect") public String testRedirect(HttpSession session) { //向session域对象中添加信息 session.setAttribute("sessionScope", "控制器返回值:重定向!"); return "redirect:/user"; } }
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1 th:text="${requestScope}" th:if="${not #strings.isEmpty(requestScope)}"></h1> <h1 th:text="${session.sessionScope}" th:if="${not #strings.isEmpty(session.sessionScope)}"></h1> </body> </html>
图3:Spring MVC 实现重定向
@RequestMapping("/testRedirect") public ModelAndView testDispatcher() { ModelAndView modelAndView = new ModelAndView(); //设置逻辑视图名 modelAndView.setViewName("redirect:/login"); return modelAndView; }
/** * 通过 ModelAndView 实现重定向 * @param session * @return */ @RequestMapping("/testRedirect2") public ModelAndView testRedirect2(HttpSession session) { ModelAndView modelAndView = new ModelAndView(); //设置逻辑视图名 modelAndView.setViewName("redirect:/user"); session.setAttribute("sessionScope", "ModelAndView:重定向!"); return modelAndView; }
图4:Spring MVC 实现重定向-2
本文链接:http://task.lmcjl.com/news/15622.html