@Valid Date 日期全局的格式化转换

通过InitBinder注解,做到全局的格式化转换

首先自定义一个格式转换类(我们以Date格式为例)DateFormatEditor继承自PropertiesEditor:

 

import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.springframework.beans.propertyeditors.PropertiesEditor;
 
public class DateFormatEditor extends PropertiesEditor {
    // 日期是否允许为空
    private boolean isAllowEmpty;
    // 日期格式
    private String pattern;
 
    public DateFormatEditor() {
        super();
        this.isAllowEmpty = true;
        this.pattern = "yyyy-MM-dd";
    }
 
    public DateFormatEditor(String pattern, boolean isAllowEmpty) {
        super();
        this.isAllowEmpty = isAllowEmpty;
        this.pattern = pattern;
    }
 
    public boolean isAllowEmpty() {
        return isAllowEmpty;
    }
 
    public void setAllowEmpty(boolean isAllowEmpty) {
        this.isAllowEmpty = isAllowEmpty;
    }
 
    public String getPattern() {
        return pattern;
    }
 
    public void setPattern(String pattern) {
        this.pattern = pattern;
    }
 
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        try {
            if (text != null && !text.equals("")) {
                SimpleDateFormat format = new SimpleDateFormat(pattern);
                setValue(format.parse(text));
            } else {
                if (isAllowEmpty) {
                    setValue(null);
                }
            }
        } catch (ParseException e) {
            System.out.println("格式转换失败!");
            setValue(null);
        }
    }
}

定义完了还是不能用,这里需要我们注册:

由于需要全局的格式化转换所以我们定义一个BaseController类:

import java.util.Date;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import spring.util.DateFormatEditor;
 
@Controller
public class BaseController {
    @Autowired
    protected HttpServletRequest request;
    @Autowired
    protected HttpServletResponse response;
    @Autowired
    protected HttpSession session;
    @Autowired
    protected ServletContext application;
 
    @InitBinder
    protected void initDate(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new DateFormatEditor("yyyy-MM-dd hh:mm:ss", true));
    }
}

 

 

 

首先自定义一个格式转换类(我们以Date格式为例)DateFormatEditor继承自PropertiesEditor:

 

  1.   import java.text.ParseException;
  2.   import java.text.SimpleDateFormat;
  3.   import org.springframework.beans.propertyeditors.PropertiesEditor;
  4.    
  5.   public class DateFormatEditor extends PropertiesEditor {
  6.   // 日期是否允许为空
  7.   private boolean isAllowEmpty;
  8.   // 日期格式
  9.   private String pattern;
  10.    
  11.   public DateFormatEditor() {
  12.   super();
  13.   this.isAllowEmpty = true;
  14.   this.pattern = "yyyy-MM-dd";
  15.   }
  16.    
  17.   public DateFormatEditor(String pattern, boolean isAllowEmpty) {
  18.   super();
  19.   this.isAllowEmpty = isAllowEmpty;
  20.   this.pattern = pattern;
  21.   }
  22.    
  23.   public boolean isAllowEmpty() {
  24.   return isAllowEmpty;
  25.   }
  26.    
  27.   public void setAllowEmpty(boolean isAllowEmpty) {
  28.   this.isAllowEmpty = isAllowEmpty;
  29.   }
  30.    
  31.   public String getPattern() {
  32.   return pattern;
  33.   }
  34.    
  35.   public void setPattern(String pattern) {
  36.   this.pattern = pattern;
  37.   }
  38.    
  39.   @Override
  40.   public void setAsText(String text) throws IllegalArgumentException {
  41.   try {
  42.   if (text != null && !text.equals("")) {
  43.   SimpleDateFormat format = new SimpleDateFormat(pattern);
  44.   setValue(format.parse(text));
  45.   } else {
  46.   if (isAllowEmpty) {
  47.   setValue(null);
  48.   }
  49.   }
  50.   } catch (ParseException e) {
  51.   System.out.println("格式转换失败!");
  52.   setValue(null);
  53.   }
  54.   }
  55.   }

定义完了还是不能用,这里需要我们注册:

由于需要全局的格式化转换所以我们定义一个BaseController类:

 

  1.   import java.util.Date;
  2.   import javax.servlet.ServletContext;
  3.   import javax.servlet.http.HttpServletRequest;
  4.   import javax.servlet.http.HttpServletResponse;
  5.   import javax.servlet.http.HttpSession;
  6.   import org.springframework.beans.factory.annotation.Autowired;
  7.   import org.springframework.stereotype.Controller;
  8.   import org.springframework.web.bind.WebDataBinder;
  9.   import org.springframework.web.bind.annotation.InitBinder;
  10.   import spring.util.DateFormatEditor;
  11.    
  12.   @Controller
  13.   public class BaseController {
  14.   @Autowired
  15.   protected HttpServletRequest request;
  16.   @Autowired
  17.   protected HttpServletResponse response;
  18.   @Autowired
  19.   protected HttpSession session;
  20.   @Autowired
  21.   protected ServletContext application;
  22.    
  23.   @InitBinder
  24.   protected void initDate(WebDataBinder binder) {
  25.   binder.registerCustomEditor(Date.class, new DateFormatEditor("yyyy-MM-dd hh:mm:ss", true));
  26.   }
  27.   }

这样就把所有Date类型的参数全部拦截下来进行判断和格式化。

上一篇:《动手学深度学习》Task04 :机器翻译


下一篇:刷题32. Longest Valid Parentheses