SPRINGMVC Learning 8-parameter binding

Source: Internet
Author: User

1.spring parameter binding process

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!!!!

The processor adapter calls SPRINGMVC to provide the parameter binding component to convert the Key/value data to a controller method's formal parameter

Parameter binding components: Using PropertyEditor (which can only be passed to Java objects) in earlier versions of SPIRNGMVC, and later using converter (for any type of transfer), SPIRNGMVC provides a lot of converter (converters), Custom converter are required in special cases and custom converter are required for date data binding

2. Default Supported Types

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.

HttpServletRequest

Obtaining request information through the Requests object

HttpServletResponse

Handling response information through response

HttpSession

To get the objects stored in the session by the Session object

Model/modelmap

Model is an interface, MODELMAP is an interface implementation.

Function: Populates the model data with the request domain.

3. 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, an error is reported if no parameters are passed in.

@RequestMapping (value= "/edititems", Method={requestmethod.post,requestmethod.get})
Inside the @RequestParam specify request to pass in the parameter name and the formal parameter to bind.
Specifies whether the parameter must be passed through the required property
The default value can be set by DefaultValue, and if the ID parameter is not passed in, the default value and the parameter are bound.
Public String Edititems (model model, @RequestParam (value= "id", required=true,defaultvalue= "1") Integer items_id) Throws Exception {

4.pojo Bindings

The name of input in the page is identical to the property names in the controller's Pojo parameter, binding the data in the page to Pojo.

Product Information Modification Submission
@RequestMapping ("/edititemssubmit")
Public String edititemssubmit (httpservletrequest request,integer ID,itemscustom itemscustom) throws Exception {

Call service to update the product information, the page needs to upload the product information to this method
Itemsservice.updateitems (ID, itemscustom);
Return "Success";
}

The solution of garbled problem:

Add post garbled filter in Web. xml

Add to Web. xml:

<filter>

<filter-name>CharacterEncodingFilter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>UTF-8</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>CharacterEncodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

The above can solve the POST request garbled problem.

There are two garbled methods for GET request Chinese parameter:

Modify the Tomcat profile add code to match the project code as follows:

<connector uriencoding= "Utf-8" connectiontimeout= "20000" port= "8080" protocol= "http/1.1" redirectport= "8443"/ >

Another way to re-encode parameters:

String UserName New

String (Request.getparamter ("UserName"). GetBytes ("Iso8859-1"), "Utf-8")

Iso8859-1 is the tomcat default encoding, and the Tomcat encoded content needs to be encoded by utf-8

5. Custom parameter bindings implement 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.

Private Date Createtime;

So the custom parameter binding turns the date string into the Java.util.Date type.

<tr>
<td> Product Production Date </td>
<td><input type= "text" name= "Createtime" value= "<fmt:formatdate value=" ${itemscustom.createtime} " pattern= "Yyyy-mm-dd HH:mm:ss"/> "/></td>
</tr>

A custom parameter binding component needs to be injected into the processor adapter.

<mvc:annotation-driven conversion-service= "Conversionservice" ></mvc:annotation-driven>

<!--custom parameter bindings--
<bean id= "Conversionservice" class= "Org.springframework.format.support.FormattingConversionServiceFactoryBean ">
<!--Converters--
<property name= "Converters" >
<list>
<!--date type conversion--
<bean class= "Controller.converter.CustomDateConverter"/>
</list>
</property>
</bean>

Package 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) {
SimpleDateFormat SimpleDateFormat = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
try {
return Simpledateformat.parse (source);
} catch (ParseException e) {
E.printstacktrace ();
}
return null;
}

}

SPRINGMVC Learning 8-parameter binding

Related Article

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.