struts配置文件中result的配置对请求结果的影响

result的配置中type的每一个值对应一个处理请求的方式,而name的值决定选择哪一种type去处理。

 

struts.xml中的配置

1 <action name="videoLineEx" class="mine.newwotv.common.VideoLineExAction">
2       <result name="video" type="redirect">newvideos.action?type=${type}&amp;province=${province}&amp;typeId=${typeId}&amp;type=${sourceType}&amp;curPage=${curPage}</result>
3       <result name="series" type="redirect">newSeriesList.action?type=${type}&amp;province=${province}&amp;typeId=${typeId}&amp;type=${sourceType}&amp;curPage=${curPage}</result>
4       <result name="variety" type="redirect">varietyList.action?type=${type}&amp;province=${province}&amp;typeId=${typeId}&amp;type=${sourceType}&amp;curPage=${curPage}</result>
5 </action>

Action类

 1 public class VideoLineExAction extends BaseAction{
 2 
 3     private static final long serialVersionUID = -6050649660151159453L;
 4     private String videoType;
 5     private String operatType;//0:下线;1:上线
 6     private String cid;
 7     private String tyid;
 8     private String sourceType;
 9     private int curPage;
10     private String type;
11     private String province;
12     public String getSourceType() {
13         return sourceType;
14     }
15     public String getTyid() {
16         return tyid;
17     }
18     public void setTyid(String tyid) {
19         this.tyid = tyid;
20     }
21     public String getType() {
22         return type;
23     }
24     public void setType(String type) {
25         this.type = type;
26     }
27     public String getProvince() {
28         return province;
29     }
30     public void setProvince(String province) {
31         this.province = province;
32     }
33     public void setSourceType(String sourceType) {
34         this.sourceType = sourceType;
35     }
36     public String getOperatType() {
37         return operatType;
38     }
39     public void setOperatType(String operatType) {
40         this.operatType = operatType;
41     }
42     public String getCid() {
43         return cid;
44     }
45     public void setCid(String cid) {
46         this.cid = cid;
47     }
48     public String getVideoType() {
49         return videoType;
50     }
51     public void setVideoType(String videoType) {
52         this.videoType = videoType;
53     }
54     
55     public String execute(){
56         if(cid=="1"){
57             videoType="video";
58         }
59         if(cid=="2"){
60            videoType="series";
61         }
62         if(cid=="3"){
63            videoType="variety";
64         }
65         return videoType;
66     }
67     public int getCurPage() {
68         return curPage;
69     }
70     public void setCurPage(int curPage) {
71         this.curPage = curPage;
72     }
73 }

代码中当videoType取不同值是,分别重定向于不同的Action中。

name

name的取值可以自己定义,如果实现了Action接口,就可以使用接口定义的常量,如果result中不配置name值就默认为name="success" ,java类中就返回对应的常量 return SUCCESS

1 public static final java.lang.String SUCCESS = "success";
2 public static final java.lang.String NONE = "none";
3 public static final java.lang.String ERROR = "error";
4 public static final java.lang.String INPUT = "input";
5 public static final java.lang.String LOGIN = "login";

type

type的取值表示一种处理方式,如果不写默认type="dispatcher",通常result支持的类型有:(还有一个json)

 数据参数的传递

1.在url中传递参数

要求:属性名必须相同,stuts会自行转化数据类型

请求路径:url=http://localhost:8080/test/view.action?id=64

 1 public class ProductAction {
 2       private Integer id;//属性必须提供set和get方法,以便利用反射能够赋值和获得属性值
 3       public void setId(Integer id){
 4              this.id = id;
 5       }
 6       public Integer getId() {
 7          return id;
 8        }
 9   }
10         

2.在url中传递参数(使用复合类型接收)

请求路径:url=http://localhost:8080/test/view.action?product.id=64

1  public class ProductAction {
2    private Product product;
3    public void setProduct(Product product) {  this.product = product;  }
4    public Product getProduct() {return product;}
5 }

3.Action配置时属性值的注入

1 <struts>
2      <package name="test" nameSpace="/test" extends="struts-default"> 
3           <action name="getPath" class="com.tocean.action.GetparamAction"    
4                                                                               method="excute">
5                <param name="savePath" >/image</param>
6                <result name="success" type="dispatcher">/view.jsp</result>
7           </action>
8      </package>
9 </struts>
 1 publicclass GetparamAction {
 2     private String savePath;
 3 
 4     public String getSavePath() {
 5         returnsavePath;
 6     }
 7 
 8     publicvoid setSavePath(String savePath) {
 9         this.savePath = savePath;
10     }
11 
12     public String execute() {
13         System.out.println(savePath);
14         return"success";
15     }
16 
17 }

4.dispatcher时

 

posted on 2017-06-22 18:14  繁花烈火  阅读(97)  评论(0)    收藏  举报