SPRINGMVC parameter Bindings

Source: Internet
Author: User

1 parameter Bindings

The processor adapter needs to bind the key/value data of the HTTP request to the Handler method shape parameter before executing the Handler.

1.1 Default supported parameter types

Adding the following type of parameter processing adapter to the processor parameters is recognized and assigned by default.

1.1.1.1 HttpServletRequest

Obtaining request information through the Requests object

1.1.1.2 HttpServletResponse

Handling response information through response

1.1.1.3 HttpSession

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

1.1.1.4 Model/modelmap

Modelmap is the implementation class of the model interface, passing data to the page through model or Modelmap , as follows:

// Call Service Query product information Items item = Itemservice.finditembyid (ID); Model.addattribute ("item", item);

Page through ${item. XXXX} Gets the property value of the item object.

As with the model and Modelmap effect, Modelmap is instantiated if you use MODEL,SPRINGMVC directly.

1.1.1 Parameter Binding Introduction

The annotation adapter requestmapping The method of labeling, and binds the shape participants in the method, and the early SPRINGMVC uses propertyeditor The parameter binding (property editor) binds the request parameter to the method parameter, and Springmvc starts using Converter to bind the parameters after 3.X.

1.1.2 Simple Type

The request parameter is bound to the formal parameter when the requested parameter name and the handler formal parameter name are the same.

1.1.2.1 Integral type

Public String EditItem (Model model,integer id) throws exception{

1.1.2.2 string

Example slightly

1.1.2.3 single-precision/double-precision

Example slightly

1.1.2.4 Boolean type

Processor method:

Public String EditItem (Model model,integer id,boolean status) throws Exception

Request URL:

Http://localhost:8080/springmvc_mybatis/item/editItem.action?id=2&status=false

Description: For a Boolean type parameter, the requested parameter value is TRUE or false.

1.1.2.5 @RequestParam

Using @requestparam is commonly used to handle simple types of bindings.

Value: The parameter name, that is, the parameter name of the incoming parameter, such as value= "item_id" means that the value of the argument in the requested parameter area will be passed in with the name item_id;

Required: Whether it is necessary, the default is true, indicating that the request must have corresponding parameters, otherwise it will be reported;

TTP Status 400-required Integer parameter ' XXXX ' is not present

DefaultValue: Default value, indicating the default value if there is no parameter with the same name in the request

Defined as follows:

public string EditItem (@RequestParam (value= "item_id", required=true) string id) {    }

The parameter name is ID, but the argument named value= "item_id" qualifies the request with the name item_id, so the name of the page pass parameter must be item_id.

Note: If no item_id in the request parameter will run out of the exception:

HTTP Status 500-required Integer parameter ' item_id ' is not present

Here, the item_id parameter is required to pass by Required=true, and if you do not pass the 400 error, you can use DefaultValue to set the default value, even if required=true can not pass the item_id parameter value

1.3.1 pojo1.3.1.1 Simple Pojo

The property name in the Pojo object corresponds to the property name passed in, and the parameter value is set in the Pojo object if the parameter name passed in is consistent with the property name in the object

The page definition is as follows;

<input type= "text" name= "name"/><input type= "text" name= "Price"/>

The Contrller method is defined as follows:

@RequestMapping ("/edititemsubmit")    public String edititemsubmit (items items)throws  exception{    System.out.println (items);
}

The requested parameter name is consistent with the Pojo property name, and the request parameter is automatically assigned to the Pojo property.

1.3.1.2 Packaging Pojo

If you name a property like an object in struts, you need to use the Pojo object as the property of a wrapper object, with the wrapper object as the parameter in the action.

The wrapper object is defined as follows:

 

class Queryvo {private  items items;}

Page Definition:

 

<type= "text"  name= "Items.name"/><  type= "text"  name= "Items.price"/>

The Controller method is defined as follows:

 Public String useraddsubmit (Model model,queryvo Queryvo)throws  exception{system.out.println ( Queryvo.getitems ());
1.1.1 Custom parameter Binding 1.1.1.1 requirements

Custom date formats for parameter binding based on business requirements.

1.1.1.2 Converter1.1.1.2.1 Custom Converter
 Public classCustomdateconverterImplementsConverter<string, date>{@Override PublicDate Convert (String source) {Try{SimpleDateFormat SimpleDateFormat=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); returnsimpledateformat.parse (source); } Catch(Exception e) {e.printstacktrace (); }        return NULL; }}

Configuration Mode 1

<Mvc:annotation-drivenConversion-service= "Conversionservice"></Mvc:annotation-driven><!--Conversionservice -    <BeanID= "Conversionservice"class= "Org.springframework.format.support.FormattingConversionServiceFactoryBean">        <!--Converters -        < Propertyname= "Converters">            <List>                <Beanclass= "Cn.itcast.ssm.controller.converter.CustomDateConverter"/>            </List>        </ Property>    </Bean>

Configuration Mode 2
<!--Note Adapter -    <Beanclass= "Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">         < Propertyname= "Webbindinginitializer"ref= "Custombinder"></ Property>     </Bean>        <!--Custom Webbinder -    <BeanID= "Custombinder"class= "Org.springframework.web.bind.support.ConfigurableWebBindingInitializer">        < Propertyname= "Conversionservice"ref= "Conversionservice" />    </Bean>    <!--Conversionservice -    <BeanID= "Conversionservice"class= "Org.springframework.format.support.FormattingConversionServiceFactoryBean">        <!--Converters -        < Propertyname= "Converters">            <List>                <Beanclass= "Cn.itcast.ssm.controller.converter.CustomDateConverter"/>            </List>        </ Property>    </Bean>

1.1.1 Collection Class 1.1.1.1 String array

The page is defined as follows:

Page select multiple checkboxes to pass to the Controller method

<inputtype= "checkbox"name= "item_id"value= "001"/><inputtype= "checkbox"name= "item_id"value= "002"/><inputtype= "checkbox"name= "item_id"value= "002"/>

The format passed into the controller method is: 001,002,003

The Controller method can be received with string[], defined as follows:

 Public String DeleteItem (string[] item_id)throws  exception{        System.out.println (item_id);}

SPRINGMVC parameter Bindings

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.