Spring 頁麵重定向示例
下麵的例子展示了如何編寫一個簡單的基於Web的應用程序,它利用重定向到一個http請求傳送到另一個另一頁。要開始使用它,我們使用Eclipse IDE,並按照以下步驟使用Spring Web框架開發動態表單的Web應用程序:
步驟 | 描述 |
---|---|
1 | Create a Dynamic Web Project with a name HelloWeb and create a packagecom.yiibai under the src folder in the created project. |
2 | Drag and drop below mentioned Spring and other libraries into the folder WebContent/WEB-INF/lib. |
3 | Create a Java class WebController under the com.yiibai package. |
4 | Create Spring configuration files Web.xml and HelloWeb-servlet.xml under theWebContent/WEB-INF folder. |
5 | Create a sub-folder with a name jsp under the WebContent/WEB-INF folder. Create view filesindex.jsp and final.jsp under this sub-folder. |
6 | The final step is to create the content of all the source and configuration files and export the application as explained below. |
這裡是WebController.java文件的內容:
package com.yiibai; 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 = "/redirect", method = RequestMethod.GET) public String redirect() { return "redirect:finalPage"; } @RequestMapping(value = "/finalPage", method = RequestMethod.GET) public String finalPage() { return "final"; } }
以下是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:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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.yiibai" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
以下是Spring的index.jsp文件的內容。這將是一個登陸頁麵,此頁麵會發送一個請求來訪問,將這個請求重定向到另一個服務方法,最後一個final.jsp頁麵將顯示重定向服務方法。
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head> <title>Spring Page Redirection</title> </head> <body> <h2>Spring Page Redirection</h2> <p>Click below button to redirect the result to new page</p> <form:form method="GET" action="/HelloWeb/redirect"> <table> <tr> <td> <input type="submit" value="Redirect Page"/> </td> </tr> </table> </form:form> </body> </html>
以下是Spring視圖文件final.jsp的內容。這是最後的重定向頁麵。
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head> <title>Spring Page Redirection</title> </head> <body> <h2>Redirected Page</h2> </body> </html>
最後,下麵是Spring和其他庫包含在Web應用程序的列表。你隻需將這些文件拖放它們到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
創建源代碼和配置文件完成後,導出您的應用程序。右鍵單擊應用程序並使用Export> WAR文件選項並保存HelloWeb.war文件到Tomcat的webapps文件夾中。
現在啟動Tomcat服務器,並確保您能夠訪問來自的webapps文件夾,使用標準的瀏覽器。現在嘗試一個URL http://localhost:8080/HelloWeb/index,應該看到下麵的結果:
現在單擊“Redirect Page”按鈕來提交表單,並獲得最終的重定向頁麵。你應該看到下麵的結果: