Struts2動態結果集

在action中獲得相應的參數,通過參數來來態指定需要跳轉的頁面.

package actions;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class Test extends ActionSupport {

    private int type;

    private String result;

    public String getResult() {

       return result;

    }

    public void setResult(String result) {

       this.result = result;

    }

    public int getType() {

       return type;

    }

    public void setType(int type) {

       this.type = type;

    }

   

    public String execute(){

       if(type==1) result="/index.jsp";

       else if(type==2) result="/delete.jsp";

       return SUCCESS;

    }

}

配置文件中通過特殊的OGNL表達式(和EL表達式的寫法一模一樣,這種OGNL表達式,是專門用在配置文件中訪問棧值的寫法.)訪問值棧中(value stack)result的值:

<package name="" namespace="/test" extends="struts-default">

    <action name="test" class="actions.Test">

        <result>${result}</result>

    </action>

</package>

當訪問/test下的test時,帶上type=1或者type=2,就會相應的跳轉到result指定的值.

 

 

 

 

帶參數的結果集

實際應用中,很多時候并不是要forward,而是需要redirect.那么可以通過result的類型來進行redirect,同時可以以表達式的方式給參數了.

package actions;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class Redi extends ActionSupport {

    private int t;

    public int getT() {

       return t;

    }

    public void setT(int t) {

       this.t = t;

    }

   

    public String execute(){

       return SUCCESS;

    }

}

 

 

Struts中的配置:

<package name="redi" namespace="/redi" extends="struts-default">

    <action name="redi" class="actions.Redi">

        <result type="redirect">/redirect.jsp?t=${t}</result>

    </action>

</package>

 

訪問路徑:

Struts2動態結果集/帶參數的結果集 - 盡頭 - 深山憨娃
 
 
打開Dubug.可以看到parameters中的值:
Struts2動態結果集/帶參數的結果集 - 盡頭 - 深山憨娃
 

 

在jsp頁面中,通過OGNL表達式訪問就行了。