首页 > 代码库 > 关于Struts2的动态URL和动态参数

关于Struts2的动态URL和动态参数

两种方式的DynaAction都一样,如下:

 1 package com.xsl.action;
 2 
 3 import java.util.Random;
 4 
 5 import com.opensymphony.xwork2.ActionSupport;
 6 
 7 public class DynaAction extends ActionSupport {
 8     private String url;
 9     private String name;
10     private int age;
11     public String test1(){
12         Random random = new Random();
13         int num = random.nextInt(2);
14         age = 22;
15         if(num == 0){
16             url = "r1.jsp";
17         }else{
18             url = "r2.jsp";
19         }
20         return SUCCESS;
21     }
22     public String getUrl() {
23         return url;
24     }
25     public void setUrl(String url) {
26         this.url = url;
27     }
28     public String getName() {
29         return name;
30     }
31     public void setName(String name) {
32         this.name = name;
33     }
34     public int getAge() {
35         return age;
36     }
37     public void setAge(int age) {
38         this.age = age;
39     }
40     
41 }

r1.jsp里:${param.name}---${param.age}--->r1.jsp

r2.jsp里:${param.name}---${param.age}--->r2.jsp

 

第一种方式(推荐):

index.jsp里:<a href="http://www.mamicode.com/dynaAction!test1?name=happy&age=21">测试动态URL和动态参数</a><br>

      或者:<a href="http://www.mamicode.com/dynaAction!test1?name=happy">测试动态URL和动态参数</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         <action name="dynaAction" class="com.xsl.action.DynaAction">
 6             <!-- &amp;是一个转义字符,相当于& -->
 7             <result>${url}?name=${name}&amp;age=${age}</result>
 8         </action>
 9     </package>
10 </struts>    

 最后跳转的页面的url为:http://localhost:8080/struts2action/dynaAction!test1?name=happy&age=21

            或者:http://localhost:8080/struts2action/dynaAction!test1?name=happy

最终跳转的页面显示:happy---22--->r1.jsp 或者happy---22--->r2.jsp    (因为在DynaAction中改变了age的值,所以最终显示age为22,但是url上的值还是不变)

 

第二种方式:

index.jsp里只能写:<a href="http://www.mamicode.com/dynaAction!test1?name=happy&age=21">测试动态URL和动态参数</a><br>

      不能写成这样,不然age显示不出来:<a href="http://www.mamicode.com/dynaAction!test1?name=happy">测试动态URL和动态参数</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         <action name="dynaAction" class="com.xsl.action.DynaAction">
 6             <result>
 7                 <param name="location">${url}</param>
 8                 <param name="name">${name}</param>
 9                 <param name="age">${age}</param>
10             </result>
11         </action>
12     </package>
13 </struts>    

最终跳转的页面显示:happy---21--->r1.jsp 或者happy---21--->r2.jsp     (即使在DynaAction中改变了age的值,但最终显示的age值还是和url上的值一样为21,不能实现动态改变的效果)

关于Struts2的动态URL和动态参数