How to set the default value for textarea in struts2
I would like to know how the default value can be set in textarea , the scenario would be after a page refresh , or fail of validation bedore save operation.
<s:t开发者_运维知识库extarea name="desc" value="" theme="xhtml" required="true" cssClass="text textarea small"/>
value="<%= "Default" %>" , this code is not working out.
Perhaps I'm missing something, but IMO this is the same as for any field: the value attribute of a Struts2 tag looks up the respective property in your stack. In the typical scenario, when you type, say, <s:textarea value="comment" ..>
Struts2 will use the MyAction.getComment()
and MyAction.setComment()
to read/write the textarea value. Then, you just have to assign a default value for the attribute in your action -which, BTW, is conceptually the right way.
public class MyAction extends ActionSupport {
public final static String DEFAULT_COMMENT = "Default value...";
private String comment = DEFAULT_COMMENT;
//... getters setters follow
}
Well one of the way would be use Javascript function to load Default value on pageload event... Though I am not sure why your tag is not working
Try to initialize the object linked to the field in the previous action method.
In your java File:
X object = new X() ;
object.setDesc("");
request.setAttribute("theFormObject",object);
In your JSP:
<s:textarea name="theFormObject" property="desc" ... />
精彩评论