JSP file of Spring MVC and form tag provided by it

Keywords: Attribute Java Spring JSP

Form: Form is the form label provided by MVC, and also provides the corresponding input label.

Use needs to import:

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

Note: The actual development action path requires a global path: ${pageContext.request.contextPath}/@RequestMapping annotates the value request value

form tags using Spring MVC
Function: Forms can be developed quickly and form value echoes can be easily performed
Note: Form tags need to match Bean class attributes. The command class is read from the request domain object by default and can be specified using the modelAttribute attribute.
form:form: form:form label
action: Request URL
Method: Request method
modelAttribute: Specifies the bound Bean (default form bean reads command from the request domain object if command does not report an error)
2. form:input, form:select: input, drop-down box
path: Form fields, corresponding to the name attribute of HTML elements, support cascading attributes-
HTML Escape: Whether to convert HTML special characters of form values, default value is true
cssClass: CSS - Style Class Name for Form Components
cssErrorClass: CSS-style used when data of form components is incorrect
3. form:input, form:password, form:hidden, form:textarea:: text, password, hidden, textarea tags corresponding to HTML forms
4. form:radiobutton:... radio button label. When the property value and value value of the form bean are equal, the radio box is selected.
5. form:radiobuttons: Radio button group label, used to construct multiple radio boxes
Items: Set the set of input items displayed, which can be a List, String [] or Map, often used in conjunction with EL expressions
path: Form fields, corresponding to the name attribute of HTML elements, support cascading attributes-
itemValue: Sets the value of the input item. If the collection contains a Bean type, you can know a specific attribute of the Bean.
itemLabel: Set the label value of the input item, which is the value displayed on the Web page
Delimiter: Set the delimiter
6. form:checkbox:. Checkbox component. Used to construct a single check box
form:checkboxs: Used to construct multiple check boxes. Use the same form:radiobuttons tag
8. form:select: Used to construct drop-down box components. Use the same way as form:radiobuttons label
9. form:option: drop-down box option component label. Use the same form:radiobuttons tag
10. form:errors: Errors corresponding to displaying form components or data validation ___________.
<form:errors path="*"/>: Display all errors in the form-
<form:errors path="user*"/>: Displays all errors with user prefixed attributes corresponding to ____________.
<form:errors path="username"/>: Error in displaying properties of a particular form object

<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<!-- Use SpringMVC Of form Label
		//Function: Form can be developed quickly and form value can be displayed easily.
		//Note: Form tags need to match Bean class attributes. The command class is read from the request domain object by default and can be specified using the modelAttribute attribute.
	form:form:Form labels 
	        action: request URL
	        method: Request mode
	        modelAttribute: Designated binding Bean(Default from request Read Domain Objects command Form bean,if command No error was reported.
	form:input,form:select : Input items, drop-down boxes
	        path: Form fields, corresponding HTML Elemental name Attributes, supporting cascading attributes –
	        htmlEscape: Is it for form values? HTML Special character conversion, default value true
	        cssClass: Form components correspond to CSS – Style class name
	        cssErrorClass: When there is an error in the data of the form component, the CSS – style
	form:input,form:password,form:hidden,form:textarea •: Corresponding HTML Form text,password,hidden,textarea Label
	form:radiobutton: • Radio button label, when form bean Corresponding attribute values and value When the value is equal, the radio box is selected
	form:radiobuttons: Radio Button Group Label for Constructing Multiple Radios •frame
	        items: Set the set of input items displayed, which can be a List,String[] or Map,Often cooperate EL Expression usage
	        path: Form fields, corresponding HTML Elemental name Attributes, supporting cascading attributes –
	        itemValue: Setting the input value Value. If the set contains Bean Type, you can know Bean A specific attribute
	        itemLabel: Setting the input label value,That is, the value displayed on the web page.
	        delimiter: Setting delimiters
	form:checkbox: • Check box component. Used to construct a single check box
	form:checkboxs: Used to construct multiple check boxes. The same way to use it form:radiobuttons Label
	form:select: Used to construct drop-down box components. The same way to use it form:radiobuttons Label
	form:option: Drop-down box option component label. The same way to use it form:radiobuttons Label
	form:errors: Display errors associated with form components or data validation •
		<form:errors path= " *" /> : Display all errors in the form –
		<form:errors path= " user*" /> : Display all with user Property correspondence for prefix –Mistake
		<form:errors path= "username" /> : Errors in displaying attributes of specific form objects
 -->
 	<!-- Actual development action Paths need to use global paths: ${pageContext.request.contextPath}/annotation value Request value -->
 	<form:form action="${pageContext.request.contextPath}/emp" method="post" modelAttribute="emp">
 		<c:if test="${emp.id == null }"><!-- If id Empty, indicating add operation -->
 			LastName:<form:input path="lastName"/><br/>
 		</c:if>
 		<c:if test="${emp.id != null }"><!-- If id Not empty, instructions are editing operations -->
 			<form:hidden path="id"/>	<!-- Using hidden tags will id As hidden input -->
 			<input type="hidden" name="_method" value="PUT"/><!-- take POST Request turned to PUT Request
 			                                                      //Cannot use form:hidden reason: form:form tags require bean objects with modelAttribute attributes
 			                                                                              //One-to-one correspondence, at this time the bean object does not have the _method attribute - >.
 		</c:if>
 		Email:<form:input path="email"/><br/>
 		<%
 			Map<String,String> map=new HashMap();
 			map.put("1", "Male");
 			map.put("0", "Female");
 			request.setAttribute("genders", map);
 		%>
 		Gender:<br/><form:radiobuttons path="gender" items="${genders }" delimiter="<br/>"/><br/>
 		Department:<form:select path="department.id" items="${departments}" itemLabel="departmentName" itemValue="id"></form:select><br/>
 		<input type="submit" value="Submit"/>
 	</form:form>
</body>
</html>

 

Posted by mjurmann on Sat, 21 Sep 2019 02:36:40 -0700