springMVC form tag

Through the form tags of SpringMVC, the attributes in the model data can be bound to the HTML form elements, so as to realize more convenient editing of form data and echo of form values.

 

1. First introduce the jsp form tag

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

 

2. Start writing examples

<from:form action="emp" method="post" modelAttribute="employee">
		LastName:<from:input path="lastName" /> <br>
		Email:<from:input path="email" /> <br>
		<%
			Map<String, String> genders = new HashMap();
				genders.put("0", "female");
				genders.put("1", "male");
				request.setAttribute("genders", genders);
		%>
		Genders:<from:radiobuttons path="gender" items="${genders }" />  <br>
		Department:<from:select items="${demps }" path="department.id"  
			itemLabel="departmentName" itemValue="id">
		</from:select>  <br>
		<input type="submit" value="submit">
	</from:form>

 

modelAttribute="employee" is used to specify that the bound model attribute employee matches the value in the controller below

 

	@RequestMapping(value="emp",method=RequestMethod.GET)
	public String getDemp(Map<String,Object> map){
		map.put("demps", departmentDao.getDepartments());
		map.put("employee",new Employee());
		
		return "save";
	}

 

<form:input/>, <form:select/>, etc., are used to bind the
attribute value of the form field. Their common attributes are as follows: --path: form field, corresponding to the name attribute   of
  the html element, supporting cascade attributes-
-htmlEscape: Whether to convert the HTML special characters of the form value, the default value is true
  --cssClass: The CSS style class name corresponding to the form component
  --cssErrorClass: When there is an error in the data of the form component, the CSS style adopted

form:input, form:password, form:hidden, form:textarea: corresponding to the text, password, hidden, textarea tags of the HTML form
form:radiobutton: radio button component tag, when the
attribute value corresponding to the form bean is equal to the value value , the radio button is selected
form:radiobuttons: radio button group label, used to construct multiple radio buttons
  –items: can be a List, String[] or Map

  --itemValue: Specifies the value of the radio. Can be an attribute value of a bean in the collection
  --itemLabel: specify the label of the radio-value
  --delimiter: multiple radio boxes can specify the separator through delimiter

form:checkbox: • Checkbox component. Used to construct a single checkbox
form:checkboxs: Used to construct multiple checkboxes. The usage is the same as the
form:radiobuttons label
form:select: used to construct the drop-down box component. The use method is the same as the
form:radiobuttons label
form:option: the drop-down box option component label. Use the same way as the
form:radiobuttons tag
form:errors: Display the errors corresponding to form components or data validation
  –<form:errors path= “*” /> : Display all errors in the 
  form –<form:errors path= “user* ” /> : show errors for all attributes prefixed with user
  – <form:errors path=“username” /> : show errors for a specific form object attribute

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326981866&siteId=291194637