2012-09-17 36 views
0

在struts2中實現了編輯功能。當我點擊提交按鈕後,我在JSP中顯示的bean值在操作類中正確獲取。爲什麼沒有得到一些bean對象的值?

但是我在JSP中沒有提到的其他bean的值是返回null。

如果我在JSP中顯示bean的所有值,那麼我可以在Action中獲取所有值。

這是解決此問題的方法。否則,還有其他方法。

的Action類的代碼

UserForm userForm = new UserForm(); 

public String edit(){ 
    String result = ActionSupport.ERROR; 
    HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST); 
     HttpSession session = request.getSession(false); 
     if (null != session 
       && null != (UserAccount) session.getAttribute(USER)) { 
      String editUser = (String) request 
      .getParameter(RequestAttributes.EDIT_USER); 
      UserAccount userAccount = userForm.getUserAccount(); 
     if (null != editUser) { 
        //invoked when edit user page is submitted 
      userUtils.updateUserAccount(userAccount); 

     } else { 
        // invoked when edit user page gets loaded 
      String userAccSID = (String) request 
        .getParameter(USER_ACC_SID); 
      String roleSID = (String) request.getParameter(ROLE_SID); 
      if (null != userAccSID && null != roleSID) { 
       Long userAccSIDVal = Long.valueOf(userAccSID); 
       Long roleSIDVal = Long.valueOf(roleSID); 
       userAccount = userUtils 
         .loadUserAccount(userAccSIDVal); 
       userForm.setUserAccount(userAccount); 
      } 
     } 
    } 

    return result; 
} 

public UserForm getUserForm() { 
    return userForm; 
} 

public void setUserForm(UserForm userForm) { 
    this.userForm = userForm; 
} 

而對於JSP頁面的代碼是

<s:form action="edit?editUser=edit"> 
<table align="center"> 
        <s:hidden name="userForm.userAccount.createdBy"/> 
     <tr align="center"> 
      <th>Edit User</th> 
     </tr> 
     <tr> 
      <td><s:textfield name="userForm.userAccount.firstName" label="First Name"/></td> 
     </tr> 
     <tr> 
      <td><s:textfield name="userForm.userAccount.lastName" label="Last Name"/></td> 
     </tr> 

     <tr> 
      <td><s:submit value="Save" /><s:reset value="Cancel" /></td> 
     </tr> 
</table> 

現在,如果我把createdBy爲隱藏,然後我可以得到的價值在行動中創建。 它的價值已經由行動類設置。 那麼,爲什麼我應該在jsp頁面中設置呢?

任何幫助將不勝感激。謝謝

+0

沒有特定的代碼/錯誤消息/堆棧跟蹤/值,幾乎不可能建議你。 – beny23

回答

1

如果您沒有從JSP返回值,那麼它們如何在表單提交時可用於Action類。 一個解決方案是創建隱藏字段並設置您不想在JSP頁面上向用戶顯示的值,這樣,當您點擊提交按鈕時,這些值將被提交到操作中。

其他選項是將數據存儲在會話中或者獲取操作類中的值,但它們不是首選解決方案,除非我們沒有其他選項。

+0

謝謝Umesh。正如你所說我嘗試過,它給了我價值觀。但是我在struts1.2中完成了同樣的任務而沒有使用隱藏值,並且我在表單提交後獲得了Action類中的所有值。 – Abinaya

+0

@Abinaya:我對Struts1不是很確定,但是概念和架構不同,因爲有不同的形式Bean,而S2不是這種情況。 –

+0

是的..我要使用隱藏的價值 – Abinaya

相關問題