Struts2 Redirect Action示例

2018-12-26 07:20 更新
redirect結(jié)果類型調(diào)用標(biāo)準(zhǔn)的response.sendRedirect()方法,使得瀏覽器向給定的位置創(chuàng)建一個(gè)新請(qǐng)求。
我們可以在<result ...>元素的主體中或作為<param name="location">的元素中給定位置。redirect還支持parse參數(shù),以下是使用XML配置的示例:

<action name="hello" 
   class="cn.w3cschool.struts2.HelloWorldAction"
   method="execute">
   <result name="success" type="redirect">
       <param name="location">
         /NewWorld.jsp
      </param >
   </result>
</action>
可參考上面所說(shuō)的修改WebContent/WEB-INF/classes/struts.xml文件來(lái)定義redirect結(jié)果類型:

<?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="cn.w3cschool.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是一個(gè)新頁(yè)面,當(dāng)你的action返回“success”結(jié)果時(shí),將產(chǎn)生redirect結(jié)果。我們保留WebContent/WEB-INF/lib/web.xml不做修改,其內(nèi)容如下:

<?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>
創(chuàng)建action類文件Java Resources/src/HelloWorldAction.java如下:

package cn.w3cschool.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;
   }
}
創(chuàng)建包含以下內(nèi)容的主頁(yè)面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>
創(chuàng)建WebContent/WEB-INF/NewWorld.jsp,如果action返回“success”時(shí),請(qǐng)求將執(zhí)行redirect結(jié)果類型:

<%@ 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>
現(xiàn)在右鍵單擊項(xiàng)目名稱,然后單擊“Export”>“WAR File”創(chuàng)建WAR文件。然后在Tomcat的webapps目錄中部署這個(gè)WAR文件。最后,啟動(dòng)Tomcat服務(wù)器并嘗試訪問(wèn)URL http://localhost:8080/HelloWorldStruts2/index.action,將顯示如下界面:

在文本框中輸入任何值并提交,你可以看到redirection后的下一頁(yè):









以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)