Spring 頁面重定向例子
Spring 頁面重定向例子
下面的例子說明了如何編寫一個簡單的基於 web 的應用程式,它利用重定向來傳送一個 http 請求到另一個頁面中。為了開始使用它,讓我們在恰當的位置使用 Eclipse IDE,然後按照下面的步驟使用 Spring 的 Web 框架來開發一個動態的基於表單的 Web 應用程式:
步驟 | 描述 |
---|---|
1 | 建立一個名稱為HelloWeb的動態 Web 專案,並且在已建立的專案的src資料夾中建立一個包com.tutorialspoint。 |
2 | 將上面提到的 Spring 和其他庫拖拽到資料夾WebContent/WEB-INF/lib中。 |
3 | 在com.tutorialspoint |
4 | 在WebContent/WEB-INF資料夾下建立 Spring 的配置檔案Web.xml和HelloWeb-servlet.xml。 |
5 | 在WebContent/WEB-INF資料夾下建立名稱為jsp的子資料夾。在這個子資料夾下建立檢視檔案index.jsp和final.jsp。 |
6 | 最後一步是建立所有的原始碼和配置檔案的內容,並匯出該應用程式,正如下面解釋的一樣。 |
這裡是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 = "/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.tutorialspoint" />
<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檔案的內容。這將是一個登陸頁面,這個頁面將傳送一個請求來訪問重定向service 方法,該方法將把這個請求重定向到另一個 service 方法中,最後將顯示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>
最後,下面是包含在你的 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
一旦你完成了建立原始碼和配置檔案後,匯出你的應用程式。右鍵單擊你的應用程式,並且使用Export > WAR File選項,並且在 Tomcat 的webapps資料夾中儲存你的HelloWeb.war檔案。
現在啟動你的 Tomcat 伺服器,並且確保你能夠使用標準的瀏覽器訪問 webapps 資料夾中的其他 web 頁面。現在嘗試訪問該 URLhttp://localhost:8080/HelloWeb/index。如果你的 Spring Web 應用程式一切都正常,你應該看到下面的結果:
現在單擊 “Redirect Page” 按鈕來提交表單,並且得到最終的重定向頁面。如果你的 Spring Web 應用程式一切都正常,你應該看到下面的結果: