更新時間:2022-02-10 08:16:07 來源:動力節點 瀏覽875次
重定向結果類型調用標準的response.sendRedirect()方法,導致瀏覽器創建到給定位置的新請求。
我們可以在<result...>元素的主體中或作為<param name = "location">元素提供位置。重定向還支持parse參數。這是一個使用 XML 配置的示例
<action name = "hello"
class = "com.tutorialspoint.struts2.HelloWorldAction"
method = "execute">
<result name = "success" type = "redirect">
<param name = "location">
/NewWorld.jsp
</param >
</result>
</action>
因此,只需修改您的WebContent/WEB-INF/classes/struts.xml文件以定義上述重定向類型
<?xml version = "1.0" Encoding = "UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name = "struts.devMode" value = "true" />
<package name = "helloworld" extends = "struts-default">
<action name = "hello"
class = "com.tutorialspoint.struts2.HelloWorldAction"
method = "execute">
<result name = "success" type = "redirect">
<param name = "location">
/NewWorld.jsp
</param >
</result>
</action>
<action name = "index">
<result >/index.jsp</result>
</action>
</package>
</struts>
這里NewWorld.jsp是一個新頁面,只要您的操作返回“成功”,您就會被重定向到該頁面。讓我們保持WebContent/WEB-INF/lib/web.xml不變,因此其內容如下
<?xml version = "1.0" Encoding = "UTF-8"?>
<web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns = "http://java.sun.com/xml/ns/javaee"
xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id = "WebApp_ID" version = "3.0">
<display-name>Struts 2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
創建動作類文件Java Resources/src/HelloWorldAction.java如下
package com.tutorialspoint.struts2;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport{
private String name;
public String execute() throws Exception {
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
讓我們使用以下內容創建主頁WebContent/WEB-INF/index.jsp
<%@ page language = "java" contentType = "text/html; charset = ISO-8859-1"
pageEncoding = "ISO-8859-1"%>
<%@ taglib prefix = "s" uri = "/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Hello World From Struts2</h1>
<form action = "hello">
<label for = "name">Please enter your name</label><br/>
<input type = "text" name = "name"/>
<input type = "submit" value = "Say Hello"/>
</form>
</body>
</html>
讓我們創建WebContent/WEB-INF/NewWorld.jsp,如果操作返回“成功”,請求將被重定向
<%@ page language = "java" contentType = "text/html; charset = ISO-8859-1"
pageEncoding = "ISO-8859-1"%>
<%@ taglib prefix = "s" uri = "/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Redirected Page</title>
</head>
<body>
<h1>New Page after redirection</h1>
</body>
</html>
現在右鍵單擊項目名稱,然后單擊導出 > WAR 文件以創建一個 War 文件。然后在 Tomcat 的 webapps 目錄中部署這個 WAR。最后,啟動 Tomcat 服務器并嘗試訪問 URL http://localhost:8080/HelloWorldStruts2/index.action。這將為您提供以下屏幕
在文本框中輸入任何值并提交頁面。重定向后您應該會看到下一頁
通過上述相信大家對Struts2重定向動作已經有所了解,大家如果想了解更多相關知識,不妨來關注一下動力節點的Struts2視頻教程,里面的課程內容通俗易懂,細致全面,比較適合沒有基礎的小伙伴學習,希望對大家能夠有所幫助。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習