Spring MVC's @InitBinder annotation uses

@ InitBinder is used to annotate methods in @Controller, indicating that the current controller registers a property editor or other, which is valid only for the current Controller. When using Spring MVC, we often encounter the conversion of Date string in form and Date type of JavaBean. Spring MVC does not support the conversion of this format by default, so we need to configure it manually and bind custom data to solve this problem. Use Spring MVC's annotation @initbinder and Spring's WebDateBinder class to operate in Controller that requires Date conversion.

WebDataBinder is used to bind request parameters to the specified property editor. Since the value passed to the controller by the foreground is String type, when the value of Set in Model is set, if the attribute of set is an object, Spring will find the corresponding editor for conversion, and then SET will go in.

The code is as follows:


package com.simple.database.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("test")
@Controller
public class TestController {

	@InitBinder
	public 	void InitBinder(WebDataBinder binder){
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		CustomDateEditor dateEditor = new CustomDateEditor(df, true);
		binder.registerCustomEditor(Date.class,dateEditor);
	}
	
	@RequestMapping(value="/param",method=RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> getFormatData(Date date) throws ParseException{
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("name", "zhangsan");
		map.put("age", 22);
		map.put("date",date);
		return map;
	}
}

Need to be added to the Spring MVC configuration file


<! - Parser Registration - >  

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">  
    <property name="messageConverters">  
        <list>  
            <ref bean="stringHttpMessageConverter"/>  
        </list>  
    </property>  
</bean>  
<!-- String Type parser, allowing direct return String Type of message -->  
<bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/>

 

Change of writing

	<! - Annotation Driven - >
	<mvc:annotation-driven>
		<! - If you customize message-converters, the default message-converters will fail - > If you customize message-converters, the default message-converters will fail.
		<mvc:message-converters>
			<! - Define a text converter - >
			<bean class="org.springframework.http.converter.StringHttpMessageConverter">
				<constructor-arg index="0" value="UTF-8" />
			</bean>
		</mvc:message-converters>
	</mvc:annotation-driven>

Expand:

spring mvc registers these editors before binding forms. Spring itself provides a large number of implementation classes, such as Custom Date Editor, Custom Boolean Editor, Custom Number Editor and many others, which are basically enough.

Call the registerCustomEditor method of WebDataBinder using time

RegiserCustomEditor source code:


public void registerCustomEditor(Class<?> requiredType, PropertyEditor propertyEditor) {

    getPropertyEditorRegistry().registerCustomEditor(requiredType, propertyEditor);

}

The first parameter requiredType is the type that needs to be transformed.

The second parameter, Property Editor, is the property editor. It is an interface. The properties Editor class mentioned above, such as CustomDateEditor, inherits the Property Editor Support class that implements this interface.

We can also not use these editor classes that they come with.

We can construct ourselves:

import org.springframework.beans.propertyeditors.PropertiesEditor;

public class DoubleEditor extends PropertyEditorSupport {
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        if (text == null || text.equals("")) {
            text = "0";
        }
        setValue(Double.parseDouble(text));
    }

    @Override
    public String getAsText() {
        return getValue().toString();
    }
}


Keywords: Java Spring Attribute Database

Added by warren on Fri, 05 Jul 2019 23:19:45 +0300