spring mvc: How Can I give the <form:textarea /

2020-04-07 05:48发布

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.

7条回答
Explosion°爆炸
2楼-- · 2020-04-07 06:01

Maybe you can use placeholder.

            <form:textarea
                            id="id"
                            class="class"
                            path="path"
                            placeholder="test"/>
查看更多
乱世女痞
3楼-- · 2020-04-07 06:09

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 the Content attribute of the ModelYouArePassingToView 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:

@ModelAttribute("modelYouArePassingToView")
public ModelYouArePassingToView createDefault() {
   //construct it with default values for "Content" 
   //attribute, and it will show up in textarea after the bindind
   ModelYouArePassingToView myapv = new ModelYouArePassingToView(); 
   myapv.setContent(..); //default value
   return myapv;
}

In your form, make sure to include the modelAttribute tag attribute:

<form:form modelAttribute="modelYouArePassingToView" ...>
  <form:textarea path="content" ..> 
查看更多
混吃等死
4楼-- · 2020-04-07 06:09

I faced a similar situation. The below code:

<form:textarea path="Content" id="my-text-box">${content}</form:textarea>

Worked out for me.

查看更多
等我变得足够好
5楼-- · 2020-04-07 06:11

You can use jquery to get the model attribute value and set the value in textarea

 $( function() {
         var example = parseInt('${example}');       
         $("#bot").val(example); 
    } );
查看更多
家丑人穷心不美
6楼-- · 2020-04-07 06:13

You can also set the defaul text in the Model as well. Your jsp would have:

<form:textarea path="my-text-box" rows="20" cols="100" cssClass="rounded"/>

and then in the Model, you'd have:

private static final String DEFAULT_textareacontent = "Hello World";
private String my-text-box = DEFAULT_textareacontent;

Not sure if this is a best practice or not, but it seems to work for me.

查看更多
姐就是有狂的资本
7楼-- · 2020-04-07 06:24

This works in Spring 4 where the form:textarea tag must be empty:

<form:textarea path="content" value="${Object.content}"/>

查看更多
登录 后发表回答