I m new to Struts2 and Hibernate. I am trying to save values from the form.
On submit the value of the textarea will be saved null;
My form is like this-
<s:form action="saveComment">
<s:push value="ai">
<s:hidden name="id"/>
<table cellpadding="5px">
<tr><td><s:textarea name="description" rows="5" cols="60" theme="simple" />
</td>
<td> <s:submit type="image" src="images/sbt.gif" >
</s:submit>
</td></tr>
</table>
</s:push>
</s:form>
and my Action Method is like this-
public String saveComment() throws Exception {
Map session = ActionContext.getContext().getSession();
ExternalUser user = (ExternalUser) session.get("user");
AIComment aiComment = new AIComment();
aiComment.setAi(ai);
aiComment.setPostedOn(new java.util.Date());
aiComment.setPostedBy(user);
aiCommentDao.saveAIComment(aiComment);
return SUCCESS;
}
Struts2 have build in mechanism to transfer your form values to your respected Action class all you need to do the following steps.
Struts2 will match those action property names with the names of the fields being sent from the form and will populate them for you
in you case all you need t do the following
So when you will submit the form it will contain id and description as form values.Struts2 interceptor (param in this case) will see your action class has these properties and will populate them before the
saveComment()method will get executed.Hope this will give you some understanding.
In short all these heavy work data-transfer/ type conversion is being done by interceptors behind the scene for you.
read the interceptors details for better understanding