動態結果配置:struts.xml中,屬性的type配置之redirectAction(請求重定向到指定的Action)

  實例:

    struts.xml的配置

    <!-- 開啓動態方法,value改爲true -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />


        <action name="login" class="com.house.action.UserAction" method="doLogin">
            <result name="success" type="redirectAction">${nextAction}</result> <!-- 權限分配 轉到下一個action -->
        </action>
        <!-- 普通用戶 -->
        <action name="common">
            <result>/page/house_list.jsp</result>
        </action>
        <!-- 管理員 -->
        <action name="manager">
            <result>/page/manage.jsp</result>

        </action>

-------------------------------------------------------------------------------------------------------------

    Action代碼中的寫法

    增加:
    private String nextAction;   //下一個action的名字,用於實現動態結果的配置 

    public String getNextAction() {
        return nextAction;
    }
    public void setNextAction(String nextAction) {
        this.nextAction = nextAction;
    }

  登錄方法的寫法:

    //登錄
    public String doLogin() throws UnsupportedEncodingException {        
        // 調用業務邏輯層
        User temp = biz.login(user.getUserName(), user.getUserPass());
        if (temp != null) {
            if("1".equals(temp.getIsAdmin())){
                nextAction = "common";
            } else if("0".equals(temp.getIsAdmin())){
                nextAction = "manager";
            }
            return "success";
        }
        return "error";
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章