10. Simple parameter binding for SpringMVC annotation development

Spring parameter binding process

In SpringMVC, key/value data is requested from the client and bound to the parameter of the controller method through parameter binding.Instead of defining member variable reception in the controller class.

Default supported types

Objects of the lower type can be used by defining them directly on the controller method parameter.

  • HttpServletRequest: Get request information through the request object
  • HttpServletResponse: Processing response information through response
  • HttpSession: Get the object stored in session from the session object
  • Model/ModelMap:model is an interface and modelMap is an interface implementation.Role: Fill the request field with model data.

Simple type

Simple type parameters are bound through @RequestParam without restricting the parameter names passed in by request to match those of the controller method.If @RequestParam is not used, the request incoming parameter name is required to match the parameter name of the controller method, or the binding is successful.

The required property specifies whether the parameter must be passed in, and if set to true, if no parameter is passed in, an error is reported.

Default values can be set through defaultValue, and if the id parameter is not passed in, the default values are bound to the parameter.

@RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})
public String editItems(Model model,@RequestParam(value="id",required=true,defaultValue =0) Integer items_id) {

pojo binding

The name of the input in the page matches the name of the property in the POJO parameter of the controller, binding the data in the page to the pojo.

Note: It is only the name that is required to match the property name of the parameter, not the name of the parameter. This should not be confused. The framework will automatically match the property name of the pojo class inside the parameter.(Should be achieved with reflection)

Page Definition:

<table width="100%" border=1>
<tr>
	<td>Commodity Name</td>
	<td><input type="text" name="name" value="${itemsCustom.name}"/></td>
</tr>
<tr>
	<td>commodity price</td>
	<td><input type="text" name="price" value="${itemsCustom.price}"/></td>
</tr>

Definition of pojo parameter for controller:

public class Items {
    private Integer id;

    private String name;

    private Float price;

    private String pic;

    private Date createtime;

    private String detail;
}

Custom parameter binding implements date type binding

For pojo objects in controller s, custom parameter bindings are required if the property has a date type.

Converts the request date data string to a date type, which is consistent with the type of date attribute in pojo.In this example, a custom parameter binding converts a date string to a java.util.Date type.A custom parameter binding component needs to be injected into the processor adapter.

  • Custom Date Type Binding
public class CustomDateConverter implements Converter<String,Date>{
    public Date convert(String s) {
        //Implements the conversion of date strings to date types (format is yyyy-MM-dd HH:mm:ss)
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            //Convert to direct return
            return simpleDateFormat.parse(s);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //Return null if parameter binding fails
        return null;
    }
}
  • collocation method
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
<!-- Custom parameter binding -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <!-- Converter -->
    <property name="converters">
        <list>
            <!-- Date type conversion -->
            <bean class="com.iot.learnssm.firstssm.controller.converter.CustomDateConverter"/>
        </list>
    </property>
</bean>

Keywords: Programming Session Spring Attribute Java

Added by MysticFallout on Tue, 12 Nov 2019 04:52:25 +0200