Reputation: 5
Have implemented an edit functionality in struts2. After i click submit button, the bean value that i 've displayed in JSP is obtained correctly in action class.
But the other values of bean that i 've not mentioned in JSP is returning null.
If i display all values of bean in JSP, then i can get all values in Action.
This is the way to fix this issue. Or else , there is any other way.
The code for Action class is
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;
}
And the code for JSP page is
<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>
Now if i put the createdBy as hidden, then i can get value of createdBy in Action. It's value is already set by the action class. Then, why should i set in jsp page also?
Any help would be highly appreciated . Thanks
Upvotes: 0
Views: 1476
Reputation: 23587
If you are not returning values from your JSP, how can they be available to the Action class on form submit. One solution is to create hidden fields and set values which you don't want to show to the user on your JSP page, in this way those values will going to get submitted to the action when you hit submit button.
Other option is to store data in the Session or fetch the values in your action class but they are not preferable solutions until we have no other options.
Upvotes: 1