春季MVC:如何提供< form:textarea/>标记默认值? [英] spring mvc: How Can I give the <form:textarea /> tag a default value?

查看:111
本文介绍了春季MVC:如何提供< form:textarea/>标记默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<form:textarea />标记提供默认值时遇到问题.
当我如下创建.jsp文件时,

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解析器将上面的行翻译为:

jsp parser translate the above line to:

<textarea id="my-text-box" name="Content"></textarea>third hello world!

此外,赋予value属性无效.

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

jsp给我作为html输出:

jsp gives me as html output:

<textarea id="my-text-box" name="Content" value="third hello world!"></textarea>

您会看到<textarea>标签没有value属性.

you can see the <textarea> tag does not have the value attribute.

如何将默认值传递给<form:textarea>标签? 预先谢谢你.

How can I pass a default value to <form:textarea> tag? Thank you in advance.

推荐答案

Spring表单标签用于数据绑定(例如,您的模型属性通过表单的path属性绑定到表单).如果需要指定默认值,则在进入视图之前,在控制器中将ModelYouArePassingToViewContent属性设置为所需的默认值.

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.

如果您使用Spring MVC和@RequestMapping,则在您的控制器中真正适合此操作的地方是您的

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;
}

在表单中,请确保包含modelAttribute标签属性:

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

<form:form modelAttribute="modelYouArePassingToView" ...>
  <form:textarea path="content" ..> 

这篇关于春季MVC:如何提供&lt; form:textarea/&gt;标记默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆