環境搭建
以下示例顯示如何使用Spring MVC Framework編寫一個簡單的基于Web的應用程式,它可以使用标記通路靜态頁面和動态頁面。首先使用Intellij IDEA建立一個動态WEB項目,并按照以下步驟使用Spring Web Framework開發基于動态表單的Web應用程式:
建立一個簡單的動态Web項目:StaticPages,并在 src 目錄下建立一個 com.ktao.controller 包。
在com.ktao.controller包下建立一個Java類WebController。
在pages子檔案夾下建立一個靜态檔案final.html。
在web/WEB-INF檔案夾下建立一個Spring配置檔案 StaticPages-servlet.xml,如下所述。
最後一步是建立所有源和配置檔案的内容并運作應用程式,如下所述。
完整的項目檔案結構如下:

配置檔案
web.xml
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_3_1.xsd"
version="3.1">
contextConfigLocation
/WEB-INF/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
StaticPages
org.springframework.web.servlet.DispatcherServlet
1
StaticPages
/
StaticPages-servlet.xml
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"
xmlns:beans="http://www.springframework.org/schema/c"
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">
标記來映射靜态頁面。映射屬性必須是指定http請求的URL模式的Ant模式。location屬性必須指定一個或多個有效的資源目錄位置,其中包含靜态頁面,包括圖檔,樣式表,JavaScript和其他靜态内容。可以使用逗号分隔的值清單指定多個資源位置。
控制器
WebController.java
package com.ktao.controller;
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";
}
}
視圖
index.jsp
Created by IntelliJ IDEA.
User: ktao
Date: 17-12-2
Time: 上午8:52
To change this template use File | Settings | File Templates.
--%>
Spring Landing Page
Spring Landing Page
點選下面的按鈕獲得一個簡單的HTML頁面
final.html
Spring Static Page
A simple HTML page
運作結果