環境搭建
以下示例顯示如何使用Spring MVC Framework編寫一個簡單的基於Web的應用程序,它可以使用<mvc:resources>
標記訪問靜態頁面和動態頁面。首先使用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
<?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_3_1.xsd" version="3.1"> <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>StaticPages</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>StaticPages</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
StaticPages-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: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"> <context:component-scan base-package="com.ktao.controller" /> <!-- <bean id="viewResolver" 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>
<mvc:resource ...../>標記來映射靜態頁面。映射屬性必須是指定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
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%-- Created by IntelliJ IDEA. User: ktao Date: 17-12-2 Time: 上午8:52 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Spring Landing Page</title> </head> <body> <h2>Spring Landing Page</h2> <p>點擊下面的按鈕獲得一個簡單的HTML頁面</p> <form:form method="get" action="staticPage"> <table> <tr> <td> <input type="submit" value="獲取HTML頁面"/> </td> </tr> </table> </form:form> </body> </html>
final.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Spring Static Page</title> </head> <body> <h2>A simple HTML page</h2> </body> </html>
運行結果