Using the Spring MVC form label

Source: Internet
Author: User
Tags bind html form html tags

Overview

In a lower version of spring, you have to bind a Form object to an HTML form page by Jstl or <spring:bind>, and the performance of Spring MVC is truly disappointing for developers accustomed to the struts form tag. But this is no longer the case, starting with Spring 2.0, Spring MVC is beginning to fully support the form label, and through the Spring MVC form tag, we can easily bind the controller-related form objects to HTML form elements.

In the previous article, "Spring MVC's Form Controller" (http://tech.it168.com/j/2007-07-26/200707261434046.shtml) we've used some of the spring MVC form tags. In this article we will give a comprehensive introduction to the Spring MVC form label, so let's start with the <form:form> tag.

Form label

As with any JSP extension label, before using the Spring form label, you must add a row to the JSP page that references the Spring form label, as follows:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> ①引入标签的声明
… ②声明后,在页面中就可以使用任意Spring表单标签了
In general, we use "form" as the prefix for the Spring MVC form label, and you can, of course, adjust to the other prefix names if you want. After you declare a good label reference, you can use all of the spring MVC form labels in the JSP file. Here is an example of using the <form:form> form label, which will eventually generate an HTML form form:<form:form>
用户名:<form:input path="userName" /> <br>
密 码:<form:password path="password" /><br>
Email:<form:input path="email" /><br>
<input type="submit" value="注册" name="testSubmit"/>
<input type="reset" value="重置" />
</form:form>

Recall the user Registration form controller that we introduced in the Spring MVC form Controller (http://tech.it168.com/j/2007-07-26/200707261434046.shtml) article. When a user invokes a form controller through a GET request, the form controller generates a new form object and then redirects to the form input page. Because the form page is guided by Access to the form controller, the <form:form> tag itself can achieve the following two goals without having to make additional settings:

1 It does not need to specify the address of the form submission through the action attribute, like the HTML <form> tag or Struts form label. Assuming the URL of the controller corresponding to the <form:form> label is "/registeruser.html" and the Application Deployment directory is "Baobaotao", the resulting HTML code automatically includes the form submission address:

<form id= "command" method= "POST" action= "/baobaotao//registeruser.html" >...</form>

2) <form:form> tags inside the component tags (such as <form:input>, <form:password>, etc.) can be directly with the form controller corresponding to the form object for value binding.

By default, the form controller places the form object in PageContext with the name "command", and you can use a different name (if set to "user") by setting the form Controller CommandName property, and you must pass the <form:form Commandname= "User" > explicitly specifies the name of the form object for the binding.

In addition to the CommandName property, the Spring form label has a rich set of properties that are mostly mirrors of HTML form label properties, such as OnClick, ondblclick, TabIndex, and so on. The point to note is that these properties are lowercase, and the corresponding HTML tag properties do not have this restriction. However, there are several attributes that differ from the HTML tags, as we explained in table 1:

Table 1 Form element label Special properties

Directory Description
CssClass Use this property to specify the table cell element CSS style name, which is equivalent to the class attribute of the HTML element. Example: <form:input path= "UserName" cssclass= "Inputstyle"/>.
CSSStyle Specify the style directly through this property, which is equivalent to the style attribute of the HTML element. Example:

<form:input path= "UserName" cssstyle= "width:100px"/>.
Csserrorclass CssClass represents the style that corresponds to a table cell element without an error, while Csserrorclass represents the style that corresponds to a table cell element error, Example:

<form:input path= "UserName" cssclass= "sty1" csserrorclass= "Sty2"/>

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.