javascript
Spring MVC入门示例教程--静态页面跳转
以下示例顯示如何使用Spring MVC Framework編寫一個簡單的基于Web的應用程序,它可以使用<mvc:resources>標記訪問靜態頁面和動態頁面。首先使用Eclipse IDE創建一個動態WEB項目,并按照以下步驟使用Spring Web Framework開發基于動態表單的Web應用程序:
完整的項目文件結構如下所示 -
WebController.java 的代碼如下所示 -
package com.yiibai.springmvc;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";} }Java
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"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="springmvc"/><mvc:resources mapping="/jsp/**" location="/WEB-INF/jsp/"/><mvc:annotation-driven/></beans>XML
這里使用<mvc:resources ..../>標記來映射靜態頁面。映射屬性必須是指定http請求的URL模式的Ant模式。location屬性必須指定一個或多個有效的資源目錄位置,其中包含靜態頁面,包括圖片,樣式表,JavaScript和其他靜態內容。可以使用逗號分隔的值列表指定多個資源位置。
下面是Spring視圖文件WEB-INF/jsp/index.jsp的內容。這將是一個登錄頁面,此頁面將發送一個請求訪問staticPage服務方法,該方法將此請求重定向到WEB-INF/pages文件夾中的靜態頁面。
index.jsp 頁面的代碼如下 -
<%@ page contentType="text/html; charset=UTF-8"%> <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head><title>Spring Landing Page</title> </head> <body> <h2>Spring Landing Page</h2> <p>點擊下面的按鈕獲得一個簡單的HTML頁面</p> <form:form method="GET" action="/StaticPages/staticPage"> <table><tr><td><input type="submit" value="獲取HTML頁面"/></td></tr> </table> </form:form> </body> </html>HTML
final.html 的完整代碼如下 -
<html> <head><title>Spring Static Page</title> </head> <body><h2>A simple HTML page</h2></body> </html>HTML
完成創建源和配置文件后,導出應用程序。右鍵單擊應用程序,并使用導出> WAR文件選項,并將文件保存為HelloWeb.war 在Tomcat的webapps文件夾中。
現在啟動Tomcat服務器,現在嘗試訪問URL => http://localhost:8080/HelloWeb/index 。 如果Spring Web應用程序沒有問題,應該看到以下結果:
單擊“獲取HTML頁面”按鈕訪問staticPage服務方法中提到的靜態頁面。如果Spring Web應用程序沒有問題,應該看到以下結果:
原文出自【易百教程】,商業轉載請聯系作者獲得授權,非商業轉載請保留原文鏈接:https://www.yiibai.com/spring_mvc/springmvc_static_pages.html
總結
以上是生活随笔為你收集整理的Spring MVC入门示例教程--静态页面跳转的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机网络--详述OSI七层模型与TCP
- 下一篇: 【软考】 2019年上半年软件设计师考试