Java single application - common framework - 07.Spring MVC - form tag library

Original address:
More tutorials: Beam cloud - free course

Form label Library

Serial number Chapter in text video
1 Declaration form label -
2 Form labels -
3 Textbox -
4 Password box -
5 Text domain -
6 check box -
7 Check box (multiple choices) -
8 radio button -
9 Radio button (multiple choice) -
10 Drop-down list -
11 Drop down list (multiple choices) -
12 Hidden domain -

Please refer to the navigation above for reading

1. Declaration form label

When using Spring MVC, we can use a series of form tags encapsulated by Spring, which can access the content in ModelMap.

We need to declare the tags used in JSP first, and add the following instructions at the top of JSP file:

<%@ taglib prefix="form" uri=""%>

2. Form label

&lt;form:form /&gt;

Using Spring MVC's form tag has two main functions:

  • The first is that it will automatically bind a property value from the Model to the entity object corresponding to the current form. The default is the command property, so we can easily use the property of the object in the form body.
  • The second is that it enables us to submit forms using other methods besides GET and POST, including DELETE and PUT.
<form:form action="formTag/" method="post">  
            <td>Name:</td><td><form:input path="name"/></td>  
            <td>Age:</td><td><form:input path="age"/></td>  
            <td colspan="2"><input type="submit" value="Submission"/></td>  

3. text box

&lt;form:input /&gt;

Use the & lt; form: input path = "name" / & gt; tag to render an HTML text box, which is equivalent to:

<input id="name" name="name" type="text" value="" />

4. password box

&lt;form:password /&gt;

Use the & lt; form: password path = "password" / & gt; tag to render an HTML password box, which is equivalent to:

<input id="password" name="password" type="password" value=""/>

5. Text Domain

&lt;form:textarea /&gt;

Use the & lt; form: textarea path = "address" rows = "5" cols = "30" / & gt; tag to render an HTML text field, which is equivalent to:

<textarea id="address" name="address" rows="5" cols="30">

6. check box

&lt;form:checkbox /&gt;

Use the & lt; form: checkbox path = "receivepaper" / & gt; tag to render an HTML checkbox, which is equivalent to:

<input id="receivePaper1" name="receivePaper" type="checkbox" value="true"/>
<input type="hidden" name="_receivePaper" value="on"/>

7. Check box (multiple choices)

&lt;form:checkboxes /&gt;

Use the & lt; form: checkboxes items = "${webframeworklist}" path = "favoriteframeworks" / & gt; tag to render an HTML multi select check box, which is equivalent to:

    <input id="favoriteFrameworks1" name="favoriteFrameworks" type="checkbox" value="Spring MVC" checked="checked"/>
    <label for="favoriteFrameworks1">Spring MVC</label>
    <input id="favoriteFrameworks2" name="favoriteFrameworks" type="checkbox" value="Struts 1"/>
    <label for="favoriteFrameworks2">Struts 1</label>
    <input id="favoriteFrameworks3" name="favoriteFrameworks" type="checkbox" value="Struts 2" checked="checked"/>
    <label for="favoriteFrameworks3">Struts 2</label>
    <input id="favoriteFrameworks4" name="favoriteFrameworks" type="checkbox" value="Apache Wicket"/>
    <label for="favoriteFrameworks4">Apache Wicket</label>
<input type="hidden" name="_favoriteFrameworks" value="on"/>

8. Radio button

&lt;form:radiobutton /&gt;

Use the & lt; form: RadioButton / & gt; tag to render an HTML radio button, which is equivalent to:

<form:radiobutton path="gender" value="M" label="male" />
<form:radiobutton path="gender" value="F" label="female" />
<input id="gender1" name="gender" type="radio" value="M" checked="checked"/><label for="gender1">male</label>
<input id="gender2" name="gender" type="radio" value="F"/><label for="gender2">female</label>

9. Radio button (multiple choice)

&lt;form:radiobuttons /&gt;

Use the & lt; form: RadioButtons path = "favoritenumber" items = "${numberslist}" / & gt; tag to render an HTML multi radio button, which is equivalent to:

    <input id="favoriteNumber1" name="favoriteNumber" type="radio" value="1"/>
    <label for="favoriteNumber1">1</label>
    <input id="favoriteNumber2" name="favoriteNumber" type="radio" value="2"/>
    <label for="favoriteNumber2">2</label>
    <input id="favoriteNumber3" name="favoriteNumber" type="radio" value="3"/>
    <label for="favoriteNumber3">3</label>
    <input id="favoriteNumber4" name="favoriteNumber" type="radio" value="4"/>
    <label for="favoriteNumber4">4</label>

10. Drop down list

Use the & lt; form: Select / & gt;, & lt; form: option / & gt;, & lt; form: options / & gt; tags to render an HTML drop-down list, which is equivalent to:

<form:select path="country">
   <form:option value="NONE" label="Select"/>
   <form:options items="${countryList}" />
<select id="country" name="country">
   <option value="NONE">Please choose...</option>
   <option value="US">United States</option>
   <option value="CH">China</option>
   <option value="MY">Malaysia</option>
   <option value="SG">Singapore</option>

11. Drop down list (multiple choices)

Use the & lt; form: Select / & gt; tag and its attribute multiple=true to render an HTML multi select drop-down list, which is equivalent to:

<form:select path="skills" items="${skillsList}" multiple="true" />
<select id="skills" name="skills" multiple="multiple">
   <option value="Struts">Struts</option>
   <option value="Hibernate">Hibernate</option>
   <option value="Apache Wicket">Apache Hadoop</option>
   <option value="Spring">Spring</option>
<input type="hidden" name="_skills" value="1"/>

12. hidden fields

&lt;form:hidden /&gt;

Use the & lt; form: hidden path = "Id" value = "1000" / & gt; tag to render an HTML hidden field, which is equivalent to:

<input id="id" name="id" type="hidden" value="1000"/>

Last article: Maven modular development

Next article: Other notes

If you are interested in the content of the course, you can scan the code to pay attention to our official account or QQ group, and pay attention to our curriculum updates in time.

Tags: Java Spring Struts Apache JSP

Posted on Sat, 21 Mar 2020 09:27:37 -0700 by ShaolinWood