Spring MVC — detailed explanation of data binding and form tags

Data binding and form labels

Data binding

Data binding is a feature that binds user input to the domain model. In the controller and view data transfer of spring MVC, based on the HTTP request feature, the types of all HTTP request parameters are strings. If the model domain needs to be bound with double or int, it needs to be manually converted. After data binding, There is no need to manually convert the string type in the HTTP request to the type required by the model. Another advantage of data binding is that when the input verification fails, an HTML form will be regenerated without filling in the input field again.

Form label 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.

Input, password, hidden, textarea check@R_233_2419 @、 check@R_233_2419 @es、radiobutton、radiobuttons、select、option、options、errors。 The form tag has attributes such as acceptcharset, commandname, cssclass, cssstyle, htmlescape, modelattribute, etc.

1. Input tag: the input tag renders the < input type = "text" / > element. The most important attribute of this tag is the path attribute, which binds the input to an attribute of the form backing object. As shown below, the input tag is bound to the username attribute of the user object

2. Password tag: render the < input type = "password" / > element. The password tag is similar to the input tag, except that it has a showpassword attribute.

3. Textarea tag: render an HTML textarea element. Textarea is basically an input element that supports multi line input.

four check@R_233_2419 @Labels: rendering < input type=“ check@R_233_2419 @"/ > the element also implements data binding through the path attribute, and check@R_233_2419 @Es rendering multiple check@R_233_2419 @Element.

5. RadioButton label renders < input type = "radio" / > elements, and RadioButtons renders multiple radio elements.

6. Select tag: render an HTML select element. The options of the rendered element may come from the collection, map, array assigned to its items attribute, or from a nested option or options tag.

Data binding example

The definition and advantages of data binding and some form tags are introduced respectively. In order to let you further understand the above contents, the example implements the binding of user class attributes and forms in JSP. At the same time, input, password, textarea check@R_233_2419 @, select tag.

Let's first take a look at the user class, which contains the user's attribute and the get and set methods for the attribute field:

Our controller class defines methods for mapping requests, including inputuser method for processing userinput request and addUser method for usersave request. Redirection is used in addUser method. The @ Autowired annotation is used to actively inject the userservice object into the productcontroller object to save and query the user object; Pass the user class object, the hobbys object of HashMap type and the carrers object of string [] type to view (JSP) through the addAttribute method of model. The code is as follows:

At the same time, in order to avoid the problem of Chinese garbled code, it is necessary to An encoding filter is added to the XML file, and the JSP page encoding is set to UTF-8. The form submission method must be post. Let's look at the web Configuration information of XML file:

The format of useradd JSP file is as follows, in which the hobbys of map type is bound to check@R_233_2419 @Es, the carrer of string [] type is bound to the select to add options to the select through the option tag, and the method method needs to be specified as post to avoid the problem of Chinese garbled code.

UserList. JSP file realizes the traversal display of the saved user information.

Because this project was added in the previous project, other configuration files are the same as those in the previous project. I won't repeat them here. I hope readers will forgive me.

Run the program and enter in the browser http://localhost:8081/SpringMVC/userInput You can see the left side, fill out the form and click the Add User button to see the output information of the right picture. If you do not know whether the reader has found any problems in the output information, the interest is [1], the registered residence is 1, the output is the index location selected in the form, and there seems to be a gender in the warranty list which has forgotten to specify User, which leads to the gender gap in the input. These problems are caused by the negligence of this article. Interested readers can make more perfect corrections to this blog.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>