Spring MVC - 靜態頁面示例



以下示例展示瞭如何使用 Spring MVC 框架編寫一個簡單的基於 Web 的應用程式,該應用程式可以透過 <mvc:resources> 標籤訪問靜態頁面以及動態頁面。

首先,讓我們準備好一個可用的 Eclipse IDE,並按照以下步驟使用 Spring Web 框架開發一個基於動態表單的 Web 應用程式。

步驟 描述
1 在 com.tutorialspoint 包下建立一個名為 HelloWeb 的專案,如 Spring MVC - Hello World 章節中所述。
2 在 com.tutorialspoint 包下建立一個名為 WebController 的 Java 類。
3 在 jsp 子資料夾下建立一個名為 final.htm 的靜態檔案。
4 更新 WebContent/WEB-INF 資料夾下的 Spring 配置檔案 HelloWeb-servlet.xml,如下所示。
5 最後一步是建立原始檔和配置檔案的內容並匯出應用程式,如下所述。

WebController.java

package com.tutorialspoint;

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.htm";
   }
}

HelloWeb-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-2.5.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
   <context:component-scan base-package = "com.tutorialspoint" />
     
   <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:resources..../> 標籤用於對映靜態頁面。mapping 屬性必須是指定 http 請求的 URL 模式的 Ant 模式。location 屬性必須指定一個或多個包含靜態頁面(包括影像、樣式表、JavaScript 和其他靜態內容)的有效資源目錄位置。可以使用逗號分隔的值列表指定多個資源位置。

以下是 Spring 檢視檔案 WEB-INF/jsp/index.jsp 的內容。這將是一個登陸頁面;此頁面將傳送請求以訪問 staticPage 服務方法,該方法將此請求重定向到 WEB-INF/pages 資料夾中可用的靜態頁面。

index.jsp

<%@taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%>
<html>
   <head>
      <title>Spring Landing Page</title>
   </head>
   <body>
      <h2>Spring Landing Pag</h2>
      <p>Click below button to get a simple HTML page</p>
      <form:form method = "GET" action = "/HelloWeb/staticPage">
         <table>
            <tr>
               <td>
                  <input type = "submit" value = "Get HTML Page"/>
               </td>
            </tr>
         </table>  
      </form:form>
   </body>
</html>

final.htm

<html>
   <head>
      <title>Spring Static Page</title>
   </head>
   <body>
      <h2>A simple HTML page</h2>
   </body>
</html>

建立完原始檔和配置檔案後,匯出您的應用程式。右鍵單擊您的應用程式,使用匯出 → WAR 檔案選項並將您的 HelloWeb.war 檔案儲存在 Tomcat 的 webapps 資料夾中。

現在,啟動您的 Tomcat 伺服器,並確保您可以使用標準瀏覽器從 webapps 資料夾訪問其他網頁。現在嘗試訪問 URL – https://:8080/HelloWeb/index。如果 Spring Web 應用程式一切正常,我們將看到以下螢幕。

Spring static page

單擊“獲取 HTML 頁面”按鈕以訪問 staticPage 服務方法中提到的靜態頁面。如果您的 Spring Web 應用程式一切正常,我們將看到以下螢幕。

Spring static page Result
廣告

© . All rights reserved.