SPRINGMVC Learning series of form labels

Source: Internet
Author: User

Http://www.cnblogs.com/liukemng/p/3754211.html

In this article, we'll learn about the use of spring MVC form labels, and the form tags provided by spring MVC make it easier to show the data in Webmodel on a view.

First, let's start with a simple example to make a general impression on the use of the Spring MVC form form label, and then combine the examples to describe how to use the individual tags.

1. First, add a model Tagsmodel the contents of the Com.demo.web.models package as follows:

Package Com.demo.web.models;import Java.util.list;import Java.util.map;public class tagsmodel{private String Usern    Ame    private String password;    Private Boolean Testboolean;    Private string[] Selectarray;    Private string[] Testarray;    Private Integer Radiobuttonid;    Private Integer Selectid;        Private list<integer> Selectids;    Private map<integer,string> TestMap;        Private String remark;    public void Setusername (String username) {this.username=username;    } public void SetPassword (String password) {This.password=password;    } public void Settestboolean (Boolean testboolean) {This.testboolean=testboolean;    } public void Setselectarray (string[] selectarray) {this.selectarray=selectarray;    } public void Settestarray (string[] testarray) {this.testarray=testarray;    } public void Setradiobuttonid (Integer radiobuttonid) {This.radiobuttonid=radiobuttonid; } public void SetselectId (Integer selectid) {this.selectid=selectid;    } public void Setselectids (list<integer> selectids) {this.selectids=selectids;    } public void Settestmap (map<integer,string> testmap) {this.testmap=testmap;    } public void Setremark (String remark) {This.remark=remark;    } public String GetUserName () {return this.username;    } public String GetPassword () {return this.password;    } public boolean Gettestboolean () {return this.testboolean;    } public string[] Getselectarray () {return this.selectarray;    } public string[] Gettestarray () {return this.testarray;    } public Integer Getradiobuttonid () {return this.radiobuttonid;    } public Integer Getselectid () {return this.selectid;    } public list<integer> Getselectids () {return this.selectids;    } public map<integer,string> Gettestmap () {return this.testmap; } public StriNg Getremark () {return this.remark; }    }

2. Next, add a Tagscontroller content to the package com.demo.web.controllers as follows:

Package Com.demo.web.controllers;import Java.util.arrays;import Java.util.hashmap;import java.util.Map;import Org.springframework.stereotype.controller;import Org.springframework.ui.model;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestmethod;import com.demo.web.models.tagsmodel;@ Controller@requestmapping (value = "/tags") public class Tagscontroller {@RequestMapping (value= "/test", method = {Re                                Questmethod.get}) Public String test (model model) {if (!model.containsattribute ("Contentmodel")) {                        Tagsmodel tagsmodel=new Tagsmodel ();            Tagsmodel.setusername ("AAA");            Tagsmodel.setpassword ("BBB");            Tagsmodel.settestboolean (TRUE);            Tagsmodel.setselectarray (new string[] {"Arrayitem Passers-by"});            Tagsmodel.settestarray (new string[] {"Arrayitem passers-by", "Arrayitem passerby B", "Arrayitem Passers-by"});        Tagsmodel.setradiobuttonid (1);    Tagsmodel.setselectid (2);            Tagsmodel.setselectids (arrays.aslist);            Map<integer,string> map=new hashmap<integer,string> ();            Map.put (1, "Mapitem passers-by");            Map.put (2, "Mapitem passers-by B");            Map.put (3, "Mapitem passers-by C");            Tagsmodel.settestmap (map);                        Tagsmodel.setremark ("Remarks ...");        Model.addattribute ("Contentmodel", Tagsmodel);    } return "Tagstest"; }    }

3. Finally, add the view under the Views folder tagstest.jsp content as follows:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" ><%@ taglib prefix= "Form" uri= "Http://www.springframework.org/tags/form"%>

  

4. Run the test:

Two. Let's describe how each label is used.
1. To use the form labels provided by spring MVC, you first need to add the following on the view page:

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

2.form Tags:

<form:form modelattribute= "Contentmodel" method= "POST" >

  

The Modelattribute property specifies which model the form is bound to, and when the corresponding model is specified, it is possible to bind the data in model by specifying the name of the model property for path on the other form labels inside the form label. The method property specifies how the form is submitted, such as Get, post, and so on.

3.input Tags:

<form:input path= "username"/>

  

Generates an HTML input label of type text that specifies the value in the model to bind by using the Path property.

4.password Tags:

<form:password path= "Password"/>

  

Generates an HTML input tag of type password, specifying the value in the model to bind by using the Path property.

5.checkbox Tags:

Generates an HTML input tag of type checkbox that supports binding Boolean, array, list, or set types of data.

Binding Boolean data generates a check box when the Boolean is true for the check box is selected, and False is not selected.

<form:checkbox path= "Testboolean"/>

  

6.checkboxs Tags:

Generates a set of HTML input tags of type checkbox based on the bound items data, which can be an array, a collection, or a map, where the path property of Checkboxs is also specified. When the data in path has the same value as the data in items, the checkbox is selected, and the other is the unselected state.

Binding collection data (as an array for demonstration):

Bind the Checkboxs tag of the array: <br/><form:checkboxes path= "Selectarray" items= "${contentmodel.testarray}"/>

  

It is important to note that when using the El expression binding, it is necessary to specify the name of the model together with an ${contentmodel.testarray} instead of specifying only the model's property name as path.

But usually what we need is a checkbox to display the name, but after the selection is submitted by the corresponding name of the value, such as ID, we can bind map to achieve this function:

Bind map's Checkboxs tag: <br/><form:checkboxes path= "Selectids" items= "${contentmodel.testmap}"/>

  

The HTML code for one of the checkboxes in the generated set of checkboxes:

<span><input id= "selectIds1" name= "Selectids" type= "checkbox" value= "1" checked= "checked"/><label for = "SELECTIDS1" >mapitem </label></span> passers-by

  

7.radiobutton Tags:

Generates an HTML input label of type radio that is selected if the value of the bound data corresponds to RadioButton specified value, and vice versa:

RadioButton tag for binding integer: <br/><form:radiobutton path= "Radiobuttonid" value= "0"/>0<form:radiobutton Path= "Radiobuttonid" value= "1"/>1<form:radiobutton path= "Radiobuttonid" value= "2"/>2

  

8.radiobuttons Tags:

Generates a set of HTML input tags of type radio based on the bound items data, which can be an array, a collection, or a map, where the path property of RadioButtons is also specified. When the value of path is the same as a data value in items, the corresponding radio is selected, and vice versa, the usage and checkboxs are similar. Note, however, that Checkboxs's path is bound to a single value for the path binding of the collection RadioButtons:

Bind map's RadioButtons tag: <br/><form:radiobuttons path= "Selectid" items= "${contentmodel.testmap}"/>

  

9.select Tags:

An HTML select tag is generated, and the bound items data can be an array, a collection, or a map that generates the option option in a select based on the contents of the items, and option is selected when the value of path is the same as a data value in items , in contrast to unselected states, usage is similar to RadioButtons:

Select tag for bind map: <br/><form:select path= "Selectid" items= "${contentmodel.testmap}"/>

  

Above is an option option that is automatically generated from the specified items, but we can also specify the option to add select manually without specifying items:

Do not bind the items data directly to the Select tag added in form:option: <br/><form:select path= "Selectid" >     <option> Please select people </ option>   <form:option value= "1" > Passers-by </form:option>   <form:option value= "2" > Passers-by B </ form:option>   <form:option value= "3" > Pedestrian C </form:option></form:select>

  

Add <option> Select people </option> to let you not specify any default values without making a selection.

Here's a look at the difference between form:option and option:

Do not bind the items data directly to the Select tag added in form:option: <br/><form:select path= "Selectid" >     <option> Please select people </ option>   <form:option value= "1" > Passers-by </form:option>   <form:option value= "2" > Passers-by B </ form:option>   <form:option value= "3" > Pedestrian C </form:option></form:select><br/> Do not bind the items data directly in the HTML option of the SELECT tag: <br/><form:select path= "Selectid" >     <option> Please select people </ option>    <option value= "1" > Passers-by </option>   <option value= "2" > Passers-by B </option>   <option value= "3" > Pedestrian C </option>  </form:select><br/>

  

The result can be seen that form:option correctly selected the Selectid specified in path and option does not, stating that Form:option has the data binding feature option.

In addition, we can not specify items for select, and the items are assigned to the form:option the two effects are basically the same, The difference is that specifying items for select and adding option in the Select is not working, it will be overridden by the option generated by the items, and the items specified on Form:option can be added to the Select option:

Bind the Select tag of items with form:option: <br/><form:select path= "Selectid" >      <option/> Select People    < Form:options items= "${contentmodel.testmap}"/>  </form:select>

  

10.textarea Tags:

TEXTAREA Tags: <form:textarea path= "Remark"/>

  

Generates an HTML TEXTAREA tag that specifies the value in the model to bind by using the Path property.

11.errors Tags:

The use of the errors tag has been explained in the Data Validation series (6), which is not described here.

The content of the Spring MVC form label ends here.

Code Download: Http://pan.baidu.com/s/1gdyzY3D

Note: I did not pay attention to the first 11 sample code, do not know why the package was uploaded on the No. project file, resulting in the download can not directly import eclipse run, the virtual machine was deleted by me, these sample code is not backed up, but the code files are still there, So you can create a new dynamic Web project to the corresponding configuration file and controller and view import can be, to everyone caused inconvenience to say sorry.

SPRINGMVC Learning series of form labels

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.