Spring MVC: Common tag Library (text box, password box, text field, check box, radio button, drop-down box hidden, upload file, etc.)

Source: Internet
Author: User
Tags error handling

The JSP page needs to be introduced:<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>标签.

Text box:

<td><form:label path= "Name" > Name: </form:label></td><td><form:input path= "Name"/ ></td>

Password box:

<td><form:label path= "username" > User name: </form:label></td><td><form:input path= " Username "/></td>

Text fields:

<td><form:label path= "Address" > Add: </form:label></td><td><form:textarea path= " Address "rows=" 5 "cols="/></td>

check box:

<td><form:label path= "Receivepaper" > Subscribe to News? </form:label></td><td><form:checkbox path= "Receivepaper"/></td>

check boxes (multiple selections) need to be in use with the backend Java:

@ModelAttribute ("webframeworklist") public   list<string> getwebframeworklist ()   {      list<string > webframeworklist = new arraylist<string> ();      Webframeworklist.add ("Spring MVC");      Webframeworklist.add ("Spring Boot");      Webframeworklist.add ("Struts 2");      Webframeworklist.add ("Apache Hadoop");      return webframeworklist;   }

  

<form:checkboxes items= "${webframeworklist}" path= "Favoriteframeworks"/>

radio button:

<Form:radiobuttonPath="Gender"Value="M"Label= "male " /><< Span class= "token namespace" >form:radiobutton path< Span class= "token punctuation" >= "Gender" value=" F "label = "female " />         

Multiple radio buttons (need to be in use with back-end Java):

@ModelAttribute ("numberslist") public   list<string> getnumberslist ()   {      list<string> Numberslist = new arraylist<string> ();      Numberslist.add ("1");      Numberslist.add ("2");      Numberslist.add ("3");      Numberslist.add ("4");      return numberslist;   }

  

<form:radiobuttons path= "Favoritenumber" items= "${numberslist}"/>

Drop-down list: You need to use the backend code:

@ModelAttribute ("countrylist") public   map<string, string> getcountrylist ()   {      map<string, string> countrylist = new hashmap<string, string> ();      Countrylist.put ("US", "states");      Countrylist.put ("CH", "China");      Countrylist.put ("SG", "Singapore");      Countrylist.put ("MY", "Malaysia");      return countrylist;   }

  

<form:select path= "Country" >   <form:option value= "NONE" label= "select"/>   <form:options items = "${countrylist}"/></form:select>

Multiple list check box:

@ModelAttribute ("skillslist") public   map<string, string> getskillslist ()   {      map<string, String > skilllist = new hashmap<string, string> ();      Skilllist.put ("Hibernate", "Hibernate");      Skilllist.put ("Spring", "Spring");      Skilllist.put ("Apache Hadoop", "Apache Hadoop");      Skilllist.put ("struts", "struts");      return skilllist;   }

  

<form:select path= "Skills" items= "${skillslist}" multiple= "true"/>

Hidden fields:

<form:hidden path= "id" value= "/>"

Spring MVC error Handling:

<form:errors path= "*" cssclass= "errorblock" element= "div"/>

Upload file:

<form:form method= "POST" modelattribute= "fileUpload"        enctype= "Multipart/form-data" >      Please select a file upload:       <input type= "file" name= "file"/>        <input type= "Submit" value= "Submit upload"/>    </form:form>

This is used value =“fileUpload” modelAttribute to map the file with the property to upload the control with the server model.

Spring MVC: Common tag Library (text box, password box, text field, check box, radio button, drop-down box hidden, upload file, etc.)

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.