I have a problem with giving the <form:textarea />
tag a default value.
When I created a .jsp file as follow,
<form:textarea path="Content" id="my-text-box" />${content}
jsp parser translate the above line to:
<textarea id="my-text-box" name="Content"></textarea>third hello world!
Also, giving the value
attribute does not work.
<form:textarea value="${content}" path="Content" id="my-text-box" />
jsp gives me as html output:
<textarea id="my-text-box" name="Content" value="third hello world!"></textarea>
you can see the <textarea>
tag does not have the value
attribute.
How can I pass a default value to <form:textarea>
tag?
Thank you in advance.
Maybe you can use placeholder.
The Spring form tags are for data binding (e.g. your model attributes are bind to the form via
path
attribute of the form). If you need to specify defaults, then set theContent
attribute of theModelYouArePassingToView
to the desired default value in the controller before it gets to the view.If your using Spring MVC and
@RequestMapping
, a really good place for this in your controller would be your@ModelAttribute
method. For example:In your form, make sure to include the
modelAttribute
tag attribute:I faced a similar situation. The below code:
Worked out for me.
You can use jquery to get the model attribute value and set the value in textarea
You can also set the defaul text in the Model as well. Your jsp would have:
and then in the Model, you'd have:
Not sure if this is a best practice or not, but it seems to work for me.
This works in Spring 4 where the form:textarea tag must be empty:
<form:textarea path="content" value="${Object.content}"/>