- Spring核心基礎
- Spring - 首頁
- Spring - 概述
- Spring - 架構
- Spring - 環境設定
- Spring - Hello World示例
- Spring - IoC容器
- Spring - Bean定義
- Spring - Bean作用域
- Spring - Bean生命週期
- Spring - Bean後處理器
- Spring - Bean定義繼承
- Spring - 依賴注入
- Spring - 注入內部Bean
- Spring - 注入集合
- Spring - Bean自動裝配
- 基於註解的配置
- Spring - 基於Java的配置
- Spring - Spring中的事件處理
- Spring - Spring中的自定義事件
- Spring - 使用Spring框架進行AOP
- Spring - JDBC框架
- Spring - 事務管理
- Spring - Web MVC框架
- Spring - 使用Log4J進行日誌記錄
- Spring問答
- Spring - 問答
- Spring有用資源
- Spring - 快速指南
- Spring - 有用資源
- Spring - 討論
Spring - 頁面重定向示例
以下示例展示瞭如何編寫一個簡單的基於Web的應用程式,該應用程式利用重定向將HTTP請求轉移到另一個頁面。首先,讓我們準備好一個可用的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.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的內容。這將是一個登入頁面,此頁面將傳送請求以訪問redirect服務方法,該方法將此請求重定向到另一個服務方法,最後將顯示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
建立完原始碼和配置檔案後,匯出您的應用程式。右鍵單擊您的應用程式,使用匯出 > WAR檔案選項,並將您的HelloWeb.war檔案儲存到Tomcat的webapps資料夾中。
現在啟動您的Tomcat伺服器,並確保您可以使用標準瀏覽器從webapps資料夾訪問其他網頁。嘗試URL https://:8080/HelloWeb/index,如果您的Spring Web應用程式一切正常,您應該看到以下結果。
單擊“重定向頁面”按鈕提交表單並獲取最終重定向的頁面。如果您的Spring Web應用程式一切正常,您應該看到以下結果。
spring_web_mvc_framework.htm
廣告