Springmvc Form Labels and Introduction to form Labels

Source: Internet
Author: User


Introduction to springmvc<from:form> form labels and <input> form labels

When using SPRINGMVC we can use a series of form tags that spring encapsulates that can access content in Modelmap. These tags are described below.

Before we formally introduce SPRINGMVC's form labels, we need to declare the labels used in the JSP, by adding the following instructions at the top of the JSP file:

JSP code <% @taglib uri= "http://www.springframework.org/tags/form" prefix= "form"%>

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

1.1 Form Label

The spring's form label has two main functions, the first of which is that it automatically binds an entity object from a property value in model to the current form, which by default is the command property, so that we can easily use the object's properties in the form table monomer. Second, it supports the submission of forms by using other methods other than get and post, including delete and put. 1.1.1 Supports binding form objects

Let's take a look at an example of using the form label as follows:

  JSP code   <form:form action= "Formtag/form.do"  method= "POST" >        <table>           <tr>               <td>Name:</td><td> <form:input path= "Name"/></td>            </tr>           <tr>                <td>age:</td><td><form:input  path= "Age"/></td>           </tr>            <tr>                <td colspan= "2" ><input type= "Submit"  value= " Submit "/></td>           </tr>       < /table>   </form:form>  

<form:form action= "formtag/form.do" method= "POST" >
    <table>
        <tr>
            <td>name: </td><td><form:input path= "name"/></td>
        </tr>
        <tr>
            <td> Age:</td><td><form:input path= "Age"/></td>
        </tr>
        <tr>
            <td Colspan= "2" ><input type= "Submit" value= "submitted"/></td>
        </tr>
    </table>
</ Form:form>

This time if there is a javabean in model with a property name of command, and the JavaBean owns the attribute name and age, the corresponding property value of the command is assigned to the corresponding label value when the above code is rendered. As in the above code, assuming that there is a javabean in model with a property name of command, and its name and age properties are "Zhangsan" and "36" respectively, it generates the following code when rendering: HTML code<formid= "command" action= "formtag/form.do" method= "POST"><table><tr><td>Name:</td><td><inputId= "name" name= "name" type= "text" value= "Zhangsan"/></td> </tr><tr><td>Age:</td><td><inputId= "Age" name= "age" type= "text" value= "36"/></td></tr><tr><TDColspan= "2"><inputType= "Submit" value= "submitted"/></td></tr></table></form>

<form id= "command" action= "formtag/form.do" method= "POST" >
    <table>
        <tr>
            <td> Name:</td><td><input id= "name" name= "name" type= "text" value= "Zhangsan"/></td> </tr
        >
        <tr>
            <td>age:</td><td><input id= "age" name= "age" type= "text" value= "36"/ ></td>
        </tr>
        <tr>
            <td colspan= "2" ><input type= "Submit" value= "submitted"/> </td>
        </tr>
    </table>
</form>

From the code generated above, we can see that when you do not specify the ID of the form label, it automatically gets the corresponding property name in the model of the form label binding as the ID, and for the input label, without specifying an ID, it automatically gets the property specified by path as ID and name.

       we specify that form defaults to automatically bind to the Command property value of model, so what happens when my form object's corresponding property name is not command. In this case, Spring gives us a CommandName property that allows us to specify which property in model we will use as the command object that the form needs to bind to. In addition to the CommandName property, specifying the Modelattribute property can achieve the same effect. This assumes that the above code we are storing in the model is the user object rather than the default command object, then our code can be defined as follows: JSP code   <form:form action= "formtag/ Form.do " method=" POST " commandname=" user ">       <table>            <tr>                <td>name:</td><td><form:input path= "Name"/ ></td>           </tr>            <tr>                <td>age:</td><td><form:input path= "Age"/></td>           </tr>            <tr>                <td colspan= "2" ><input type= "Submit"  value= "submitted"/></td>            </tr>       </ table>   </form:form>  

<form:form action= "formtag/form.do" method= "POST" commandname= "user" >
    <table>
        <tr>
            <td>name:</td><td><form:input path= "Name"/></td>
        </tr>
        <tr>
            <td>age:</td><td><form:input path= "age"/></td>
        </tr>
        <tr >
            <td colspan= "2" ><input type= "Submit" value= "submitted"/></td>
        </tr>
    </table >
</form:form>

1.1.2 Support for all HTTP request Methods JSP code   <form:form action= "formtag/form.do"  method= "delete"  modelattribute= "user" >        <table>            <tr>               <td>name: </td><td><form:input path= "Name"/></td>            </tr>           <tr>    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&LT;TD&GT;AGE:&LT;/TD&GT;&LT;TD ><form:input path= "Age"/></td>            </tr>           <tr>                <td colspan= "2" ><input type= " Submit " valuE= "Submit"/></td>           </tr>       </table>   </form:form>  

<form:form action= "formtag/form.do" method= "delete" modelattribute= "user" >
    <table>
        <tr>
            <td>name:</td><td><form:input path= "Name"/></td>
        </tr>
        <tr >
            <td>age:</td><td><form:input path= "age"/></td>
        </tr>
        < tr>
            <td colspan= "2" ><input type= "Submit" value= "submitted"/></td>
        </tr>
    </ Table>
</form:form>

In the code above we set the form's submission method to be delete, so that in the background we can give the corresponding request method's Requestmapping plus methods for the requestmethod.delete limit. Let's take a look at what HTML code is generated when the above code is rendered, and the resulting code looks like this: HTML code<formid= "User" action= "formtag/form.do" method= "POST"><inputType= "hidden" name= "_method" value= "Delete"/><table><tr><td>Name:</td><td><inputId= "name" name= "name" type= "text" value= "Zhangsan"/></td></tr> <tr><TD

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.