To use data binding efficiently, Spring's form tag library is also required.
Data binding overview: Another benefit of data binding is that it regenerates an HTML form when input validation fails. When writing HTML by hand , you must repopulate the input fields with the user's previously entered values in mind.
With Spring data binding
and form tag library, they will do the work for you.
Form Tag Library: The form tag library contains tags that can be used to render HTML elements in JSP pages. In order to use these tags, the taglib directive must be declared at the beginning of the JSP page. <%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
#tags in the form tag library:
Label |
describe |
form |
Render form elements |
input |
Render the <input type="text"/> element |
password |
Render the <input type="password"/> element |
hidden |
Render the <input type="hidden"/> element |
textarea |
render textarea element |
checkbox |
Renders an <input type="checkbox"/> element |
checkboxes |
Rendering multiple <input type="checkbox"/> elements |
radiobutton |
Renders an <input type="radio"/> element |
radiobuttons |
Rendering multiple <input type="radio"/> elements |
select |
render a select element |
option |
render an optional element |
options |
renders a list of optional elements |
errors |
Error rendering field in span element |
1. Form tags: Form tags are used to render HTML forms. To use any other tag that renders a form input field, there must be a form tag.
Attributes |
describe |
acceptCharset |
Defines a list of character encodings accepted by the server |
commandName |
The name of the model property that exposes the form object, defaults to command |
cssClass |
Defines the CSS class to be applied to the rendered form element |
cssStyle |
Defines the CSS styles to be applied to the rendered form element |
htmlEscape |
Accepts true or false, indicating whether the rendered value should be HTML-escaped |
modelAttribute |
Expose the name of the model property of the form backing object, defaults to command |
Attributes |
describe |
cssClass |
Defines the CSS class to apply to the rendered input element |
cssStyle |
Defines CSS styles to be applied to rendered input elements |
cssErrorClass |
Defines the CSS class to apply to the rendered input element, overriding if the bound property contains errors cssClass property value |
htmlEscape |
Accepts true or false, indicating whether the rendered value should be HTML-escaped |
path |
property path to bind |
属性 |
描述 |
cssClass |
定义要应用到被渲染 input 元素的 CSS 类 |
cssStyle |
定义要应用到被渲染 input 元素的 CSS 样式 |
cssErrorClass |
定义要应用到被渲染 input 元素的 CSS 类,如果 bound 属性中包含错误,则覆盖 cssClass 属性值 |
htmlEscape |
接受 true 或者 false,表示是否应该对被渲染的值进行 HTML 转义 |
path |
要绑定的属性路径 |
showPassword |
表示应该显示或遮盖密码,默认值为 false |
属性 |
描述 |
htmlEscape |
接受 true 或者 false,表示是否应该对被渲染的值进行 HTML 转义 |
path |
要绑定的属性路径 |
属性 |
描述 |
cssClass |
定义要应用到被渲染 input 元素的 CSS 类 |
cssStyle |
定义要应用到被渲染 input 元素的 CSS 样式 |
cssErrorClass |
定义要应用到被渲染 input 元素的 CSS 类,如果 bound 属性中包含错误,则覆盖 cssClass 属性值 |
htmlEscape |
接受 true 或者 false,表示是否应该对被渲染的值进行 HTML 转义 |
path |
要绑定的属性路径 |
属性 |
描述 |
cssClass |
定义要应用到被渲染 input 元素的 CSS 类 |
cssStyle |
定义要应用到被渲染 input 元素的 CSS 样式 |
cssErrorClass |
定义要应用到被渲染 input 元素的 CSS 类,如果 bound 属性中包含错误,则覆盖 cssClass 属性值 |
htmlEscape |
接受 true 或者 false,表示是否应该对被渲染的(多个)值进行 HTML 转义 |
label |
要作为标签用于被渲染复选框的值 |
path |
要绑定的属性路径 |
属性 |
描述 |
cssClass |
定义要应用到被渲染 input 元素的 CSS 类 |
cssStyle |
定义要应用到被渲染 input 元素的 CSS 样式 |
cssErrorClass |
定义要应用到被渲染 input 元素的 CSS 类,如果 bound 属性中包含错误,则覆盖 cssClass 属性值 |
htmlEscape |
接受 true 或者 false,表示是否应该对被渲染的(多个)值进行 HTML 转义 |
label |
要作为标签用于被渲染复选框的值 |
path |
要绑定的属性路径 |
8.checkboxes标签:checkboxes 标签渲染多个<input type="checkbox"/>元素。
属性 |
描述 |
cssClass |
定义要应用到被渲染 input 元素的 CSS 类 |
cssStyle |
定义要应用到被渲染 input 元素的 CSS 样式 |
cssErrorClass |
定义要应用到被渲染 input 元素的 CSS 类,如果 bound 属性中包含错误,则覆盖 cssClass 属性值 |
delimiter |
定义两个 input 元素之间的分隔符,默认没有分隔符 |
element |
给每个被渲染的 input 元素都定义一个 HTML 元素,默认为“span” |
htmlEscape |
接受 true 或者 false,表示是否应该对被渲染的(多个)值进行 HTML 转义 |
items |
用于生成 input 元素的对象的 Collection、Map 或者 Array |
itemLabel |
item 属性中定义的 Collection、Map 或者 Array 中的对象属性,为每个 input 元素提供标签 |
itemValue |
item 属性中定义的 Collection、Map 或者 Array 中的对象属性,为每个 input 元素提供值 |
path |
要绑定的属性路径 |
属性 |
描述 |
cssClass |
定义要应用到被渲染 input 元素的 CSS 类 |
cssStyle |
定义要应用到被渲染 input 元素的 CSS 样式 |
cssErrorClass |
定义要应用到被渲染 input 元素的 CSS 类,如果 bound 属性中包含错误,则覆盖 cssClass 属性值 |
delimiter |
定义两个 input 元素之间的分隔符,默认没有分隔符 |
element |
给每一个被渲染的 input 元素都定义一个 HTML 元素,默认为“span” |
htmlEscape |
接受 true 或者 false,表示是否应该对被渲染的(多个)值进行 HTML 转义 |
items |
用于生成 input 元素的对象的 Collection、Map 或者 Array |
itemLabel |
item 属性中定义的 Collection、Map 或者 Array 中的对象属性,为每个 input 元素提供标签 |
itemValue |
item 属性中定义的 Collection、Map 或者 Array 中的对象属性,为每个 input 元素提供值 |
path |
要绑定的属性路径 |
属性 |
描述 |
cssClass |
定义要应用到被渲染 input 元素的 CSS 类 |
cssStyle |
定义要应用到被渲染 input 元素的 CSS 样式 |
cssErrorClass |
定义要应用到被渲染 input 元素的 CSS 类,如果 bound 属性中包含错误,则覆盖 cssClass 属性值 |
htmlEscape |
接受 true 或者 false,表示是否应该对被渲染的(多个)值进行 HTML 转义 |
items |
用于生成 input 元素的对象的 Collection、Map 或者 Array |
itemLabel |
item 属性中定义的 Collection、Map 或者 Array 中的对象属性,为每个 input 元素提供标签 |
itemValue |
item 属性中定义的 Collection、Map 或者 Array 中的对象属性,为每个 input 元素提供值 |
path |
要绑定的属性路径 |
属性 |
描述 |
cssClass |
定义要应用到被渲染 input 元素的 CSS 类 |
cssStyle |
定义要应用到被渲染 input 元素的 CSS 样式 |
cssErrorClass |
定义要应用到被渲染 input 元素的 CSS 类,如果 bound 属性中包含错误,则覆盖 cssClass 属性值 |
htmlEscape |
接受 true 或者 false,表示是否应该对被渲染的(多个)值进行 HTML 转义 |
属性 |
描述 |
cssClass |
定义要应用到被渲染 input 元素的 CSS 类 |
cssStyle |
定义要应用到被渲染 input 元素的 CSS 样式 |
cssErrorClass |
定义要应用到被渲染 input 元素的 CSS 类,如果 bound 属性中包含错误,则覆盖 cssClass 属性值 |
htmlEscape |
接受 true 或者 false,表示是否应该对被渲染的(多个)值进行 HTML 转义 |
items |
用于生成 input 元素的对象的 Collection、Map 或者 Array |
itemLabel |
item 属性中定义的 Collection、Map 或者 Array 中的对象属性,为每个 input 元素提供标签 |
itemValue |
item 属性中定义的 Collection、Map 或者 Array 中的对象属性,为每个 input 元素提供值 |
属性 |
描述 |
cssClass |
定义要应用到被渲染 input 元素的 CSS 类 |
cssStyle |
定义要应用到被渲染 input 元素的 CSS 样式 |
属性 |
描述 |
Delimiter |
分隔多个错误消息的分隔符 |
element |
定义一个包含错误消息的 HTML 元素 |
htmlEscape |
接受 true 或者 false,表示是否应该对被渲染的(多个)值进行 HTML 转义 |
path |
要绑定的错误对象路径 |