Spring - 靜態頁面示例



以下示例演示瞭如何使用Spring MVC框架編寫簡單的基於Web的應用程式,該應用程式可以使用<mvc:resources>標籤訪問靜態頁面以及動態頁面。首先,讓我們準備好一個可用的Eclipse IDE,並按照以下步驟使用Spring Web框架開發基於動態表單的Web應用程式:

步驟 描述
1 建立一個名為HelloWeb動態Web專案,並在建立的專案中的src資料夾下建立一個包com.tutorialspoint
2 將下面提到的Spring和其他庫拖放到WebContent/WEB-INF/lib資料夾中。
3 com.tutorialspoint包下建立一個Java類WebController
4 WebContent/WEB-INF資料夾下建立Spring配置檔案Web.xmlHelloWeb-servlet.xml
5 WebContent/WEB-INF資料夾下建立一個名為jsp的子資料夾。在此子資料夾下建立一個檢視檔案index.jsp
6 WebContent/WEB-INF資料夾下建立一個名為pages的子資料夾。在此子資料夾下建立一個靜態檔案final.htm
7 最後一步是建立所有原始檔和配置檔案的內容,並按照以下說明匯出應用程式。

以下是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";
   }
}

以下是Spring Web配置檔案web.xml的內容

<web-app id = "WebApp_ID" version = "2.4"
   xmlns = "http://java.sun.com/xml/ns/j2ee" 
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
   <display-name>Spring Page Redirection</display-name>
 
   <servlet>
      <servlet-name>HelloWeb</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>
   
   <servlet-mapping>
      <servlet-name>HelloWeb</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>
  
</web-app>

以下是另一個Spring Web配置檔案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屬性必須是一個Ant模式,指定http請求的URL模式。location屬性必須指定一個或多個有效的資源目錄位置,其中包含靜態頁面,包括影像、樣式表、JavaScript和其他靜態內容。可以使用逗號分隔的值列表指定多個資源位置。

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

<%@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>

以下是Spring檢視檔案WEB-INF/pages/final.htm的內容。

<html>
   <head>
      <title>Spring Static Page</title>
   </head>

   <body>
      <h2>A simple HTML page</h2>
   </body>
</html>

最後,以下是您的Web應用程式中要包含的Spring和其他庫的列表。您只需將這些檔案拖放到WebContent/WEB-INF/lib資料夾中。

  • commons-logging-x.y.z.jar
  • org.springframework.asm-x.y.z.jar
  • org.springframework.beans-x.y.z.jar
  • org.springframework.context-x.y.z.jar
  • org.springframework.core-x.y.z.jar
  • org.springframework.expression-x.y.z.jar
  • org.springframework.web.servlet-x.y.z.jar
  • org.springframework.web-x.y.z.jar
  • spring-web.jar

建立完原始檔和配置檔案後,匯出您的應用程式。右鍵單擊您的應用程式,使用匯出 > 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
spring_web_mvc_framework.htm
廣告
© . All rights reserved.