springmvc的bind问题(一)initbinder的调用

springmvc的bind问题(一)initbinder的调用

以前使用spring都是用的集成子MultiActionController
遇到传递的参数bind的时候,就重写initBinder在自己的基类上
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception {
      System.out.println("init binder =======================");
SimpleDateFormat dateFormat = new
SimpleDateFormat(DateFormatUtil.COMMON_DATE_PATTERN);
      binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat,
   false));
      binder.registerCustomEditor(BigDecimal.class, new CustomNumberEditor     (BigDecimal.class, false));
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat,    false));
binder.registerCustomEditor(Integer.class, null, new
   CustomNumberEditor(Integer.class, null, true));
binder.registerCustomEditor(int.class, null, new
   CustomNumberEditor(Integer.class, null, true));
binder.registerCustomEditor(Long.class, null, new
   CustomNumberEditor(Long.class, null, true));
binder.registerCustomEditor(long.class, null, new
   CustomNumberEditor(Long.class, null, true));
binder.registerCustomEditor(Float.class, new
   CustomNumberEditor(Float.class, true));
binder.registerCustomEditor(Double.class, new
   CustomNumberEditor(Double.class, true));
binder.registerCustomEditor(BigInteger.class, new
   CustomNumberEditor(BigInteger.class, true));
super.initBinder(request, binder);
}
这样注册之后,当我们写的方法xxx.do调用的时候,如果有第三个参数,比如User user
那么就会调用invokeNamedMethod方法里面的代码段
if (paramTypes.length >= 3 &&
!paramTypes[paramTypes.length - 1].equals(HttpSession.class)) {
Object command = newCommandObject(paramTypes[paramTypes.length - 1]);
params.add(command);
bind(request, command);
}
其中的bind方法中会调用createBinder就会去调用到我们重写的
initBinder(request, binder);

但是我使用标注的时候我以为还是走的这条线路,但是发现不是。调用后出错如下:
[org.springframework.context.support.DefaultMessageSourceResolvable: codes [materialCategory.sluggishWarningTime,sluggishWarningTime]; arguments []; default message [sluggishWarningTime]]; default message [Failed to convert property value of type [java.lang.String] to required type [int] for property 'sluggishWarningTime'; nested exception is java.lang.NumberFormatException: For input string: ""]
at org.springframework.web.bind.ServletRequestDataBinder.closeNoCatch(ServletRequestDataBinder.java:121)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.doBind(AnnotationMethodHandlerAdapter.java:568)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:213)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:132)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:326)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:313)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:511)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at cn.sccl.cpmis.common.web.MessageFilter.doFilter(MessageFilter.java:27)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at cn.sccl.cpmis.common.web.CharactorEncodingFilter.doFilter(CharactorEncodingFilter.java:64)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

发现用了标注以后,根本就没有走MultiActionController这条线了。所以initbinder需要采用标注,修改我们自己的基类controller为
public class MAController {
private static final int HTTP_STATUS_500 = 500;
private static final int BUF_SIZE = 4096;
protected Log log = LogFactory.getLog(getClass());
@InitBinder
public void initBinder(WebDataBinder binder) {
   //System.out.println("init binder =======================");
   SimpleDateFormat dateFormat = new SimpleDateFormat(
     DateFormatUtil.COMMON_DATE_PATTERN);
   dateFormat.setLenient(false);
   binder.registerCustomEditor(Date.class, new CustomDateEditor(
     dateFormat, false));
   binder.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(
     BigDecimal.class, false));
   binder.registerCustomEditor(Integer.class, null,
     new CustomNumberEditor(Integer.class, null, true));
   binder.registerCustomEditor(int.class, null, new CustomNativeEditor(
     Integer.class, null , true));
   binder.registerCustomEditor(Long.class, null, new CustomNumberEditor(
     Long.class, null, true));
   binder.registerCustomEditor(long.class, null, new CustomNativeEditor(
     Long.class, null, true));
   binder.registerCustomEditor(Float.class, new CustomNumberEditor(
     Float.class, true));
   binder.registerCustomEditor(Double.class, new CustomNumberEditor(
     Double.class, true));
   binder.registerCustomEditor(BigInteger.class, new CustomNumberEditor(
     BigInteger.class, true));
}
使用标注@InitBinder来达到initBinder的目的。重新启动应用,在页面传入数字1时,可以自动绑定到我们对象User.age这个变量了,我们的变量定义的int类型。

你可能感兴趣的:(java,apache,mvc,Web,servlet)