------------吾亦無他,唯手熟爾,謙卑若愚,好學若飢-------------
轉發和重定向大家都熟悉,都學到框架了,怎么能不了解轉發和重定向呢?
如果有不熟悉的,可以去百度搜幾篇博客去看看,絕對比我在這兒再多扯點好,所以我這兒要講的重點就是springmvc的轉發和重定向的寫法
首先了解一個概念:攜帶數據的要用轉發而不是重定向,重定向是在客戶端完成,轉發是在服務器端完成,所以路徑寫法有所不同
我在這篇博客要寫的是:轉發到頁面,轉發到別的處理方法,重定向到頁面,重定向到別的處理方法。
(我將處理方法扔一塊了,不過我會解釋清楚的)
案例開始:
一,處理方法:(直接return:"abc")的是轉發到頁面
package cn.dawn.day14redirectandforward; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Created by Dawn on 2018/3/28. */ @Controller public class RedirectAndForwardController { /*轉發到處理方法*/ @RequestMapping("addBookforward") public String addBookforward(Model model){ model.addAttribute("msg","遲老大愛着原老大"); /*轉發到處理方法*/ return "forward:/BookListredirect"; } @RequestMapping("BookListforward") public String BookListforward(Model model){ /*轉發到頁面頁面*/ return "success"; } /*重定向到處理方法*/ @RequestMapping("addBookredirect") public String addBookredirect(Model model){ model.addAttribute("msg","遲老大愛着原老大"); /*重定向到處理方法*/ return "redirect:/BookListredirect"; } @RequestMapping("BookListredirect") public String BookListredirect(Model model){ /*轉發到頁面頁面*/ return "success"; } /*重定向頁面*/ @RequestMapping("pageredirect") public String pageredirect(Model model){ model.addAttribute("msg","遲老大愛着原老大"); /*重定向頁面*/ return "redirect:/day14/success.jsp"; } /*轉發到頁面*/ @RequestMapping("pageforward") public String pageforward(Model model){ model.addAttribute("msg","遲老大愛着原老大"); /*轉發到頁面*/ return "success"; } }
二,自定義的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:mvc="http://www.springframework.org/schema/mvc" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--包掃描器--> <context:component-scan base-package="cn.dawn.day14redirectandforward"></context:component-scan> <!--視圖解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/day14/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
三,修改web.xml的中央調度器的上下文配置位置
四,准備jsp頁面:倆個,我的在day14的包下:
1.login.jsp
<%@ page pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>Title</title> </head> <body> <h2>登錄</h2> <form action="${pageContext.request.contextPath}/pageforward" method="post"> <input type="submit" value="登錄"/> </form> </body> </html>
2.success.jsp
<%@ page language="java" pageEncoding="utf-8" isELIgnored="false" %> <html> <body> <%--<img src="image/1.jpg">--%> <h2>Success!</h2> <p>${msg}</p> </body> </html>
你這種方式需要改action中指向的處理方法名,才能調度到對應的處理方法
其實沒必要這么麻煩,如果通過網頁的url訪問,只需要一個success.jsp頁面即可,但我是給你提供思路的人,所以,我給你多一個寫法