Spring MVC基礎之頁面重定向
1、使用IDEA新建項目如下:
2、代碼:
index.jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form:form action="/day0826/redirect.do">
<table>
<tr>
<td><input type="submit" value="頁面重定向" /></td>
</tr>
</table>
</form:form>
</body>
</html>
redirect.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<p>
這是重定向頁面....
</p>
</body>
</html>
RedirectController.java
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class RedirectController {
//主頁:index.jsp
@RequestMapping(value = "/index.do")
public String index(){
return "/index"; //這是屬於轉發:forward
}
//重定向頁面:redirect.jsp
@RequestMapping("/redirect.do")
public String redirect(){
return "redirect:/pages/redirect.jsp"; //重定向到redirect.jsp頁面
}
}
dispatcher-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"
xmlns:p="http://www.springframework.org/schema/p"
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">
<!--開啓對註解的支持-->
<context:component-scan base-package="controller"/>
<!--配置映射-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp"/>
</beans>
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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3、運行:
點擊按鈕之後:
我們都知道重定向和轉發有個很明顯的區別:
1、重定向的url地址會發生改變
2、請求轉發的url地址不會發生改變
於是,我們可以看見,我們在地址欄輸入http://localhost:8080/day0826/index.do
回車之後會轉發到頁面index.jsp.
但我們點擊按鈕之後,響應的映射地址本該是http://localhost:8080/day0826/redirect.do
然後回重定向到頁面redirect.jsp
,但是由於是重定向,所以地址欄發生了改變爲:http://localhost:8080/day0826/pages/redirect.jsp
這就是爲什麼我在註釋裏面寫了這個的原因:(是屬於轉發)
//主頁:index.jsp
@RequestMapping(value = "/index.do")
public String index(){
return "/index"; //這是屬於轉發:forward
}