簡單理解 spring MVC 靜態頁面流程
阿新 • • 發佈:2018-11-26
該文章內容是個人的理解,僅供參考,若有錯誤,歡迎指正!
檔案目錄
WebController.java
package Static;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class WebController {
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index() {
return "index";
}
@RequestMapping(value = "/staticPage", method = RequestMethod.GET)
public String redirect() {
return "redirect:/pages/final.html";
}
}
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: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
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="Static"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:resources mapping="/pages/**" location="/WEB-INF/pages/" />
<mvc:annotation-driven/>
</beans>
jsp/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>
<h2>Spring</h2>
<p>點選下面的按鈕獲得一個簡單的HTML頁面</p>
<form:form method="get" action="staticPage">
<table>
<tr>
<td>
<input type="submit" value="獲取頁面" />
</td>
</tr>
</table>
</form:form>
</body>
</html>
流程
- web.xml 攔截器攔截,傳入
dispatcher-servlet.xml
<context:component-scan>
將掃描base-package
中包或子包下的所有java
類,並把匹配的java
檔案註冊成bean
- 接下來就是
WebController.java
,@RequestMapping
將到/index
的請求由index()
方法來處理,index()
向dispatcher-servlet.xml
返回"index"
到dispatcher-servlet.xml
- 接下來到
InternalResourceViewResolver
,其中prefix
是檔案目錄,suffix
是字尾,與返回的"index"
合併成"index.jsp"
,並在"/WEB-INF/jsp/"
下找到index.jsp
檔案
- 在頁面上點選按鈕後,通過
get
方法將表單提交,並跳轉到staticPage
,完整的連結就是http://localhost:8080/StaticPages/staticPage
- 經過與上述類似的過程,
WebController.java
中的redirect()
向dispatcher-servlet.xml
返回"redirect:/pages/final.html"
,但是,redirect
直接讓連結跳轉到了http://localhost:8080/StaticPages/pages/final.html
- 經過類似的操作……嗯?
WebController.java
裡沒有對/pages/final.html
的攔截,然後就重新回到dispatcher-servlet.xml
,被<mvc:resources>
給攔截了,location
定位到了/WEB-INF/pages/
你可能會有疑問,為什麼locatin
定位的是/WEB-INF/pages/
而不是/WEB-INF/
(不管你有沒有,反正我是有的),於是我試著把pages/
去掉,果然不行,再看這個<mvc:resources mapping="/pages/**" location="/WEB-INF/pages/" />
,攔截器其實把/pages
攔截了,只剩下final.html
了,這樣就成功地跳轉到了final.html
頁面了
- 寫到這裡,我突然想到一個操作,可不可以在表單提交的時候直接把
action
設定成/pages/final.html
呢?這樣就可以直接跳過一步。試了一下,可以實現功能。