1. 程式人生 > >簡單理解 spring MVC 靜態頁面流程

簡單理解 spring MVC 靜態頁面流程

該文章內容是個人的理解,僅供參考,若有錯誤,歡迎指正!


檔案目錄

​​在這裡插入圖片描述

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>

流程

  1. 輸入網址 http://localhost:8080/StaticPages/index
  1. web.xml 攔截器攔截,傳入dispatcher-servlet.xml
  1. <context:component-scan> 將掃描 base-package 中包或子包下的所有java類,並把匹配的java檔案註冊成bean
  1. 接下來就是WebController.java@RequestMapping 將到/index 的請求由 index()方法來處理,index()dispatcher-servlet.xml 返回 "index"dispatcher-servlet.xml
  1. 接下來到InternalResourceViewResolver,其中prefix是檔案目錄,suffix是字尾,與返回的"index"合併成"index.jsp",並在"/WEB-INF/jsp/"下找到index.jsp檔案
  1. 在頁面上點選按鈕後,通過get方法將表單提交,並跳轉到staticPage,完整的連結就是http://localhost:8080/StaticPages/staticPage
  1. 經過與上述類似的過程,WebController.java中的redirect()dispatcher-servlet.xml返回"redirect:/pages/final.html",但是,redirect直接讓連結跳轉到了http://localhost:8080/StaticPages/pages/final.html
  1. 經過類似的操作……嗯?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頁面了
  1. 寫到這裡,我突然想到一個操作,可不可以在表單提交的時候直接把action設定成/pages/final.html呢?這樣就可以直接跳過一步。試了一下,可以實現功能。

再次宣告,作者是spring MVC 初學者,本文僅代表個人觀點,僅供參考