javascript
Spring MVC入门示例教程--静态页面跳转
以下示例顯示如何使用Spring MVC Framework編寫一個簡單的基于Web的應(yīng)用程序,它可以使用<mvc:resources>標(biāo)記訪問靜態(tài)頁面和動態(tài)頁面。首先使用Eclipse IDE創(chuàng)建一個動態(tài)WEB項(xiàng)目,并按照以下步驟使用Spring Web Framework開發(fā)基于動態(tài)表單的Web應(yīng)用程序:
完整的項(xiàng)目文件結(jié)構(gòu)如下所示 -
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 ..../>標(biāo)記來映射靜態(tài)頁面。映射屬性必須是指定http請求的URL模式的Ant模式。location屬性必須指定一個或多個有效的資源目錄位置,其中包含靜態(tài)頁面,包括圖片,樣式表,JavaScript和其他靜態(tài)內(nèi)容。可以使用逗號分隔的值列表指定多個資源位置。
下面是Spring視圖文件WEB-INF/jsp/index.jsp的內(nèi)容。這將是一個登錄頁面,此頁面將發(fā)送一個請求訪問staticPage服務(wù)方法,該方法將此請求重定向到WEB-INF/pages文件夾中的靜態(tài)頁面。
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>點(diǎn)擊下面的按鈕獲得一個簡單的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
完成創(chuàng)建源和配置文件后,導(dǎo)出應(yīng)用程序。右鍵單擊應(yīng)用程序,并使用導(dǎo)出> WAR文件選項(xiàng),并將文件保存為HelloWeb.war 在Tomcat的webapps文件夾中。
現(xiàn)在啟動Tomcat服務(wù)器,現(xiàn)在嘗試訪問URL => http://localhost:8080/HelloWeb/index 。 如果Spring Web應(yīng)用程序沒有問題,應(yīng)該看到以下結(jié)果:
單擊“獲取HTML頁面”按鈕訪問staticPage服務(wù)方法中提到的靜態(tài)頁面。如果Spring Web應(yīng)用程序沒有問題,應(yīng)該看到以下結(jié)果:
原文出自【易百教程】,商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請保留原文鏈接:https://www.yiibai.com/spring_mvc/springmvc_static_pages.html
總結(jié)
以上是生活随笔為你收集整理的Spring MVC入门示例教程--静态页面跳转的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机网络--详述OSI七层模型与TCP
- 下一篇: Spring MVC Hibernate