string @InitBinder use

In SpringMVC, Date, double and other types are defined in beans. If no processing is done, neither date nor double can be bound.

The solution is to use  the @InitBinder tag provided by spring mvc

 

 

@Controller

public class BoceController extends BaseController {

 

@InitBinder

public void initBinder(WebDataBinder binder) {

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

dateFormat.setLenient(false);

binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));

binder.registerCustomEditor(String.class,new StrToInteger());

}

}

 

 

package com.cloud.api.util;

 

import java.beans.PropertyEditorSupport;

 

public class StrToInteger extends PropertyEditorSupport{

 

@Override    

    public void setAsText(String text) throws IllegalArgumentException {    

        if (text == null || text.equals("")) {    

            text = "0";    

        }  

        //Get the request object in text

        //2. Convert the request object to the required object

        

        Integer value = Integer.parseInt(text);

        //3 Put the converted object into the setValue method

        setValue(value);    

    }    

    

    @Override    

    public String getAsText() { 

    System.out.println("=++++++++++++++++++++++++++++++++");

        return getValue().toString();    

    }   

    

}

 

 

StrToInteger This class is a method to convert the received string into an Integer object after receiving the fields submitted by the post.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326463846&siteId=291194637