關於Struts2的Action的跳轉方式(跳轉到JSP)(跳轉到Action)


success.jsp里:${name }---this is success.jsp

一、跳轉到JSP

請求轉發:http://localhost:8080/struts2action/testAction1!test1?name=tom

index.jsp里:<a href="testAction1!test1?name=tom">請求轉發</a><br>

struts.xml里:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
 3 <struts>
 4     <package name="xsl" namespace="/" extends="struts-default">
 5     
 6         <action name="testAction1" class="com.xsl.action.TestAction1">
 7             <!-- 
 8                 type屬性不寫,默認則為type="dispatcher"
 9                 tyoe="redirect"表示重定向到jsp
10                 type="chain"表示請求轉發至另一個action
11              -->
12             <result name="test1" type="dispatcher">/success.jsp</result>
13             
14         </action>
15         
16     </package>
17 </struts>    

TestAction1.java里:

 1 package com.xsl.action;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 public class TestAction1 extends ActionSupport {
 6     private String name;
 7     public String test1(){
 8         System.out.println("請求轉發--action1--test1--name--"+name);//請求轉發--action1--test1--name--tom 9         //request.setAttribute("name",name);
10         return "test1";
11     }
12     public String getName() {
13         return name;
14     }
15     public void setName(String name) {
16         this.name = name;
17     }
18     
19 }

success.jsp里顯示:tom---this is success.jsp 

 

重定向:http://localhost:8080/struts2action/success.jsp

index.jsp里:<a href="testAction1!test2?name=jack">重定向</a><br>

struts.xml里:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
 3 <struts>
 4     <package name="xsl" namespace="/" extends="global">
 5     
 6         <action name="testAction1" class="com.xsl.action.TestAction1">
 7             <!-- 
 8                 type屬性不寫,默認則為type="dispatcher"
 9                 tyoe="redirect"表示重定向到jsp
10                 type="chain"表示請求轉發至另一個action
11              -->
12             <result name="test2" type="redirect">/success.jsp</result>
13         </action>
14         
15     </package>
16 </struts>    

TestAction1.java里:

 1 package com.xsl.action;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 public class TestAction1 extends ActionSupport {
 6     private String name;
 7     public String test2(){
 8         System.out.println("重定向--action1--test2--name--"+name);//重定向--action1--test2--name--jack
 9         return "test2";
10     }
11     public String getName() {
12         return name;
13     }
14     public void setName(String name) {
15         this.name = name;
16     }
17     
18 }

success.jsp里顯示:---this is success.jsp  

 

二、跳轉到Action

請求轉發:http://localhost:8080/struts2action/testAction1!test3?name=rose

index.jsp里:<a href="testAction1!test3?name=rose">請求轉發</a><br>

struts.xml里:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
 3 <struts>
 4     <package name="xsl" namespace="/" extends="struts-default">
 5     
 6         <action name="testAction1" class="com.xsl.action.TestAction1">
 7             <!-- 
 8                 type屬性不寫,默認則為type="dispatcher"
 9                 tyoe="redirect"表示重定向到jsp
10                 type="chain"表示請求轉發至另一個action
11              -->
12             <result name="test3" type="chain">testAction2_test1</result>
13         </action>
14         <!-- struts2.1這個版本要求action在跳轉action時,不能寫死action的名字 -->
15         <action name="testAction2_*" class="com.xsl.action.TestAction2" method="{1}">
16             <result>/success.jsp</result>
17         </action>
18 
19     </package>
20 </struts>    

TestAction1.java里:

 1 package com.xsl.action;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 public class TestAction1 extends ActionSupport {
 6     private String name;
 7     public String test3(){
 8         System.out.println("action1--test3--name--"+name);//action1--test3--name--rose
 9         return "test3";
10     }
11     public String getName() {
12         return name;
13     }
14     public void setName(String name) {
15         this.name = name;
16     }
17     
18 }

TestAction2.java里:

 1 package com.xsl.action;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 public class TestAction2 extends ActionSupport {
 6     private String name;
 7     public String test1(){
 8         System.out.println("請求轉發--action2--test1--name--"+name);//請求轉發--action2--test1--name--rose
 
         
 9 return SUCCESS; 10  } 11 public String getName() { 12 return name; 13  } 14 public void setName(String name) { 15 this.name = name; 16  } 17 18 }

success.jsp里顯示: rose---this is success.jsp

 

重定向:http://localhost:8080/struts2action/testAction2_test2.action

index.jsp里:<a href="testAction1!test4?name=rain">重定向</a><br>

struts.xml里:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
 3 <struts>
 4     <package name="xsl" namespace="/" extends="struts-default">
 5     
 6         <action name="testAction1" class="com.xsl.action.TestAction1">
 7             <!-- 
 8                 type屬性不寫,默認則為type="dispatcher"
 9                 tyoe="redirect"表示重定向到jsp
10                 type="chain"表示請求轉發至另一個action
11              -->
12             <result name="test3" type="chain">testAction2_test1</result>
13             <result name="test4" type="redirectAction">testAction2_test2</result>
14         </action>
15         <!-- struts2.1這個版本要求action在跳轉action時,不能寫死action的名字 -->
16         <action name="testAction2_*" class="com.xsl.action.TestAction2" method="{1}">
17             <result>/success.jsp</result>
18         </action>
19 
20     </package>
21 </struts>    

TestAction1.java里:

 1 package com.xsl.action;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 public class TestAction1 extends ActionSupport {
 6     private String name;
 7     public String test4(){
 8         System.out.println("action1--test4--name--"+name);//action1--test4--name--rain
 9         return "test4";
10     }
11     public String getName() {
12         return name;
13     }
14     public void setName(String name) {
15         this.name = name;
16     }
17     
18 }

TestAction2.java里:

 1 package com.xsl.action;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 public class TestAction2 extends ActionSupport {
 6     private String name;
 7     public String test2(){
 8         System.out.println("重定向--action2--test2--name--"+name);//重定向--action2--test2--name--null  9         return SUCCESS;
10     }
11     public String getName() {
12         return name;
13     }
14     public void setName(String name) {
15         this.name = name;
16     }
17     
18 }

success.jsp里顯示:---this is success.jsp 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM