3.Struts2-Result

注:

1.在struts.xml文件中使用include标签 可以将另外一个xml文件包含进struts.xml文件中,如:

1 <struts>
2     <constant name="struts.devMode" value="true" />
3     <include file="login.xml" />
4 </struts>    //就可以包含login.xml文件

2.可以在struts.xml文件中,使用 <default-action-ref> 标签 来指定默认Action,即找不到对应的Action进行处理时,调用这个Action

  如:<default-action-ref name="index"></default-action-ref>

 

 

Result

  1.result 标签  type 属性(用于指定某个Action执行后跳转的方式)

  type = "dispather"(默认) 类似于转发(转发到某个JSP)

  type="redirect" 类似于重定向 (重定向到某个JSP)

  type="chain"  转发到某个Action (转发的Action和自身的Action 不在同一个包下,需要使用参数去指定)

  type="redirectAction" 重定向到某个Action

  这四种需要了解  还有其他的不太常见,重点掌握前面两种

  

  2.全局结果集(<global-results>)

    在某个package中使用<global-results>标签 ,设置全局结果集

    全局结果集的作用:相当于为该package下的每个action 多加上一个result  

    如:在Action中,execute方法 既没有返回success,也没有返回error等,返回的是mainpage,

    struts.xml中对应的action 并没有配置 ‘mainpage’ 的result,但如果设置了全局结果集,就仍可以找到对应的result

    注:如果另外一个package,想要使用这个globle results,需要 extends 对应的package,这也是package标签中 extends属性的作用

 

  3.动态结果(所谓动态:就是在Action中动态指定结果,在struts.xml中再去取出这个结果)

  

 1 public class UserAction extends ActionSupport {
 2     private int type;
 3     private String result;
 4     
 5     public void setResult(String result) {
 6         this.result = result;
 7     }
 8     
 9     public String getResult() {
10         return result;
11     }
12     
13     public void setType(int type) {
14         this.type = type;
15     }
16     
17     public int getType() {
18         return type;
19     }
20     
21     @Override
22     public String execute() throws Exception {
23         if(type == 1) {
24             result="/success.jsp";
25         } else if(type == 2) {
26             result="/error.jsp";
27         } 
28         return SUCCESS;
29     }
30 }

 

  /*不根据result标签中的 name 属性来 进行筛选,而是动态指定想要访问的web资源*/

    1.在Action中,设置一个属性,用于保存动态结果,根据前台传来的信息,来为其赋值

    注意一定不要忘了为动态结果的保存值设置set get方法

    2.在struts.xml 中 取出这个动态结果 使用 ${result}   如:<result> ${result} </result>

 

  4.带参数的结果集(即返回的web资源路径,后面带有参数,这个参数是Action里面的属性)

 1 private int type;
 2     
 3     
 4 public void setType(int type) {
 5     this.type = type;
 6 }
 7     
 8 public int getType() {
 9     return type;
10 }
1 <result type="dispatcher"> /success.jsp?t=${type} </result>

   注:

   一次request请求,只有一个值栈 如果result标签中指定类型为 "redirect",重定向到某个Action 或 JSP 那么会有两次请求,

   之前的值栈的内容就无法保存,会被第二次请求覆盖
   如果result标签中指定类型为 "dispather" 转发到某个Action 或JSP 那么请求只有一次,可以共享值栈中的内容

  

posted @ 2017-08-17 09:17  白日梦想家12138  阅读(142)  评论(0编辑  收藏  举报