About the use of the form tag library in spring MVC

Source: Internet
Author: User
Tags html form

In the normal MVC design pattern, M stands for the model layer, V for the view layer, and C for the controller, and SPRINGMVC is a typical MVC setup pattern framework, with many optimizations for views and controllers, including a form tag library for the JSP that is combined with the controller.

Let's start by writing a JSP file, using the normal HTML form:

1<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"2Pageencoding="UTF-8"%>3<!DOCTYPE html>4<Html>5<Head>6<MetaHttp-equiv= "Content-type"Content= "text/html; Charset=utf-8 ">7<Title>insert Title here</Title>8</Head>9<Body>10<FormAction= "Action"Method= "POST">Username:<InputType= "Text"Name= "username" />12 Password:<input = "password"  Name= "Password" /> 
<input type= "Submit" value= "GO"/> 13 </form>14 </body< Span style= "color: #0000ff;" >>15 </html< Span style= "color: #0000ff;" >>

This shows the effect:

UsernamePassword

In practical use, Spring provides a form tag library to render the form, so that you can bind the label directly to the object, and now join me with a model layer

User.java

Public user{    String password;  String username; // code omitted here ... 6}      

If we use the label of the form tag library to render, we can bind the label directly to the password or username of the user object.

When using the form tag library, declare it first:

1 <% @taglib prefix= "form" uri= "Http://www.springframework.org/tags/form"%>

However, the form label cannot be manipulated without data binding, and will error when running the program because the form label is dependent on the data binding operation.

We first need to create a new user reference here in the controller, that is, to have a user object to bind the password and username of the user object to the input tag, so we should do the following first:

1 @RequestMapping (value= "/action")// address of action here 2 public      String inputname (model model) {  3 Logger.info ("InputName called");  New User ()); The  object that provides the data to the view layer is used for data binding return "Jspfile";  returns the file name of the view 6}             

Next we can use the form tag library:

1<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"2Pageencoding="UTF-8"%>3<%@taglib Prefix="Form"Uri="Http://www.springframework.org/tags/form"%>4<!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd">5<Html>6<Head>7<MetaHttp-equiv= "Content-type"Content= "text/html; Charset=utf-8 ">8<Title>insert Title here</Title>9</Head>10<Body>13<Form:formCommandName= "User"Action= "Action"Method= "POST">Username:<Form:inputPath= "username"/><!--Input type= "text" name= "username"/><br/><br/-Password:<Form:passwordPath= "Password"/><!-- input type= "password" name= "password"/-->16 <input Span style= "color: #ff0000;" >type= "Submit"  Value= "GO" />17 </< Span style= "color: #800000;" >form:form>18 < /body>19 </html>       

Note that the label of the form tag library is used here, the CommandName property is the most important attribute, which defines the name of the model property, the

Model.addattribute ("User", new user ());

The user is passed in, so we use the user, and the subsequent form returns the properties of the model.

Then the path property in the input tag:

The attribute value in CommandName is user, and the property of path is username, then the input tag of username is bound to the username attribute of user.

It is important to note that we are not able to use the form tag library when we use it directly without data binding.

About the use of the form tag library in spring MVC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.