java-webwork:參數傳遞問題

本問題出現在用一個action實現CRUD的設計裏:

xwork.xml:

        <!-- Assets-Location crud function start here  -->
        <action name="ast_loc_service" class="com.hwccl.assets.system.action.LocationAction" method="list" >
            <result name="success">/web/assets/system/locations.jsp</result>
            <result name="error">/web/users/error.jsp</result>
            <interceptor-ref name="cookieStatck"/>
        </action>
        <action name="ast_loc_crud" class="com.hwccl.assets.system.action.LocationAction" method="input">
            <result name="success" type="redirect-action">ast_loc_service</result>
            <result name="input">/web/assets/system/locationForm.jsp</result>
            <result name="error">/web/users/error.jsp</result>
            
        </action>   

測試時,發現
http://localhost:8080/oa_hwccl/ast_loc_crud!input.action?location.id=1
參數傳遞失敗,無法獲得參數。

後來看到以下文字:
http://jxb8901.javaeye.com/blog/6103


在XWork中,它是OgnlValueStack)。當你提交一個表單到action中,在它執行之前,由框架去取得你的Action,並實例化這個Action對象,再將這個Action對象放入它的值堆棧中。
這時候,你在做編輯的時候,它一定會有原始的數據,假設這個數據對象就是user,同時假設這個數據就是從數據庫中取得的。
你要編輯這個user對象的數據,當然你的視圖(也許是頁面)要獲取原始的數據。如何獲取?你可以通過EL(表達式語言)從OgnlValueStack 中獲取。你的EL:user.username,它就是相當於調用Action對象的getUser().getUsername();方法。通過這種方 式,你可以在視圖中取得原始數據。
到這裏,我想你都能明白。但請注意下面:
在你對原始數據進行更新時,你希望這個更新的數據同步到user對象中。這時,你仍然是通過EL,進行值的設置(注意:前面是值的獲取)。你的EL:user.username,它這是相當於調用代碼:getUse.setUsername(你更新的值)。


原來自己在action裏定義了對象location,卻沒爲他做getter and setter,所以getLocation().getId()無法生效。

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