Parameter bindings for SPRINGMVC

Source: Internet
Author: User

The process of 1.SPRINGMVC parameter binding

Request Key/value data from the client, pass the parameter binding, bind the Key/value data to the parameter of the Controller method.

In Springmvc, the data submitted by the receiving page is received through a method parameter. Instead of the Controller class, define member Change receive!!!! (a single case of OH)



You can use these objects directly by defining objects of the type below on the controller method parameters. During parameter binding, if the type below is encountered, it is bound directly.

1) HttpServletRequest: Get request information by requesting object

2)HttpServletResponse: Process the relevant information through response

3) HttpSession: Gets the object stored in the session by the Session object

4) Model/modelmap:model is an interface, MODELMAP is an interface implementation. function: Populates the model data with the request domain.

5) Simple Type:

Binds the parameters of a simple type by @requestparam.

If you do not use @requestparam, you require the request to pass in the parameter name consistent with the Controller method's formal parameter name before binding succeeds.

If you use @requestparam, you do not have to restrict the request to pass in parameter names consistent with the Controller method's formal parameter names.

The Required property specifies whether the parameter must be passed in, and if it is set to true, there is no incoming parameter to report the bottom error:



6) parameter bindings for the Pojo type: the name of input in the page is the same as the property names in the Pojo parameter of the controller, and the data in the page is bound to Pojo.


7) Custom parameter type binding implements date type binding

For a Pojo object in a controller parameter, a custom parameter binding is required if there is a date type in the attribute.

The request date data string is passed to a date type, and the type of the date to be converted is consistent with the types of date attributes in the Pojo.


Custom type converter classes:

Package Cn.itcast.ssm.controller.converter;import Java.text.parseexception;import Java.text.SimpleDateFormat; Import Java.util.date;import Org.springframework.core.convert.converter.converter;public class CustomDateConverter Implements Converter<string,date>{public date Convert (string source) {//implementation converts date string to date type (format is YYYY-MM-DD HH:mm:ss) SimpleDateFormat SimpleDateFormat = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); try {//turn to return directly Simpledateformat.parse (source);} catch (ParseException e) {//TODO auto-generated catch Blocke.printstacktrace ();} Returns Nullreturn NULL if the parameter binding fails;}}

A custom parameter binding component needs to be injected into the processor adapter because it is annotated, so it needs to be configured in SPRINGMVC:

<mvc:annotation-driven conversion-service= "Conversionservice" ></mvc:annotation-driven><!--processor Mapper --><!--Processor Adapter--><!--Controller handler--><!--view resolver--><!--config type converter--><!--validator registered to the processor adapter--& Gt;<mvc:annotation-driven conversion-service= "Conversionservice" validator= "Validator" ></MVC: annotation-driven><!--Custom parameter bindings--><bean id= "Conversionservice" class= " Org.springframework.format.support.FormattingConversionServiceFactoryBean "><!--converter--><property Name = "Converters" ><list><!--Date type conversion--><bean class= " Cn.itcast.ssm.controller.converter.CustomDateConverter "/></list></property></bean>
Attention:





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Parameter bindings for SPRINGMVC

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.