SpringMVC data binding and form tag library

Data binding is a feature that binds user input to the domain model. With data binding, HTTP request parameters, always of type String, can be used to populate object properties of different types. Data binding makes form beans (such as the ProductForm instance in the previous chapters) redundant.

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 of the 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

2.input tag: The input tag renders the <input type="text"/> element.
Attributes of the #input tag:

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

3.password 标签:password 标签渲染<input type="password"/>元素,password 标签与 input标签相似,只不过它有一个 showPassword 属性。
#password 标签的属性:

属性

描述

cssClass

定义要应用到被渲染 input 元素的 CSS 类

cssStyle

定义要应用到被渲染 input 元素的 CSS 样式

cssErrorClass

定义要应用到被渲染 input 元素的 CSS 类,如果 bound 属性中包含错误,则覆盖 cssClass

属性值

htmlEscape

接受 true 或者 false,表示是否应该对被渲染的值进行 HTML 转义

path

要绑定的属性路径

showPassword

表示应该显示或遮盖密码,默认值为 false

4.hidden标签:hidden 标签渲染<input type="hidden"/>元素。hidden 标签与 input 标签相似,只不过它没有可视的外观,因此不支持cssClass 和 cssStyle 属性
#hidden 标签的属性

属性

描述

htmlEscape

接受 true 或者 false,表示是否应该对被渲染的值进行 HTML 转义

path

要绑定的属性路径

5.textarea标签:textarea 标签渲染一个 HTML 的 textarea 元素。Textarea 实际上就是支持多行输入的一个input 元素。
#textarea 标签的属性

属性

描述

cssClass

定义要应用到被渲染 input 元素的 CSS 类

cssStyle

定义要应用到被渲染 input 元素的 CSS 样式

cssErrorClass

定义要应用到被渲染 input 元素的 CSS 类,如果 bound 属性中包含错误,则覆盖 cssClass

属性值

htmlEscape

接受 true 或者 false,表示是否应该对被渲染的值进行 HTML 转义

path

要绑定的属性路径

6.checkbox 标签:checkbox 标签渲染<input type="checkbox"/>元素。
#checkbox标签的属性

属性

描述

cssClass

定义要应用到被渲染 input 元素的 CSS 类

cssStyle

定义要应用到被渲染 input 元素的 CSS 样式

cssErrorClass

定义要应用到被渲染 input 元素的 CSS 类,如果 bound 属性中包含错误,则覆盖 cssClass

属性值

htmlEscape

接受 true 或者 false,表示是否应该对被渲染的(多个)值进行 HTML 转义

label

要作为标签用于被渲染复选框的值

path

要绑定的属性路径

7.radiobutton标签:radiobutton 标签渲染<input type="radio"/>元素。
#radiobutton标签的属性

属性

描述

cssClass

定义要应用到被渲染 input 元素的 CSS 类

cssStyle

定义要应用到被渲染 input 元素的 CSS 样式

cssErrorClass

定义要应用到被渲染 input 元素的 CSS 类,如果 bound 属性中包含错误,则覆盖 cssClass

属性值

htmlEscape

接受 true 或者 false,表示是否应该对被渲染的(多个)值进行 HTML 转义

label

要作为标签用于被渲染复选框的值

path

要绑定的属性路径

8.checkboxes标签:checkboxes 标签渲染多个<input type="checkbox"/>元素。

#checkboxes 标签的属性

属性

描述

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

要绑定的属性路径

9.radiobuttons 标签:radiobuttons 标签渲染多个<input type="radio"/>元素。
#radiobuttons 标签的属性

属性

描述

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

要绑定的属性路径

10.select 标签:select 标签渲染一个HTML 的 select 元素。被渲染元素的选项可能来自赋予其 items 属性的一个 Collection、Map、Array,或者来自一个嵌套的 option 或者 options 标签。
#select 标签的属性

属性

描述

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

要绑定的属性路径

11.option 标签:option 标签渲染 select 元素中使用的一个HTML 的 option 元素。
#option 标签的属性

属性

描述

cssClass

定义要应用到被渲染 input 元素的 CSS 类

cssStyle

定义要应用到被渲染 input 元素的 CSS 样式

cssErrorClass

定义要应用到被渲染 input 元素的 CSS 类,如果 bound 属性中包含错误,则覆盖 cssClass

属性值

htmlEscape

接受 true 或者 false,表示是否应该对被渲染的(多个)值进行 HTML 转义

12.options 标签:options 标签生成一个 HTML 的 option 元素列表。
#options 标签的属性

属性

描述

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 元素提供值

13.errors 标签:errors 标签渲染一个或者多个 HTML 的span 元素,每个 span 元素中都包含一个字段错误。这个标签可以用于显示一个特定的字段错误,或者所有字段错误。
#errors 标签的属性

属性

描述

cssClass

定义要应用到被渲染 input 元素的 CSS 类

cssStyle

定义要应用到被渲染 input 元素的 CSS 样式

属性

描述

Delimiter

分隔多个错误消息的分隔符

element

定义一个包含错误消息的 HTML 元素

htmlEscape

接受 true 或者 false,表示是否应该对被渲染的(多个)值进行 HTML 转义

path

要绑定的错误对象路径

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324581416&siteId=291194637