关于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 

转载:https://www.cnblogs.com/xsl1995/p/6638218.html

猜你喜欢

转载自blog.csdn.net/sunddy_x/article/details/80859147