论坛首页 Java企业应用论坛

Spring3 Web MVC下的数据类型转换(第一篇)——《跟我学Spring3 Web MVC》抢先看

浏览 54674 次
该帖已经被评为精华帖
作者 正文
   发表时间:2012-05-05  
view层标签方面我一直觉得spring mvc的标签远远比不上struts2的标签好用,不知道大家有没有这种感觉
0 请登录后投票
   发表时间:2012-05-05  
guofengcn 写道
view层标签方面我一直觉得spring mvc的标签远远比不上struts2的标签好用,不知道大家有没有这种感觉

表达式支持上:在没有spring:eval标签时 spring标签的表达式运算全靠EL表达式确实很弱,但有了spring:eval标签,就能解析任意的SpEL表达式,和struts2的OGNL类似功能。
0 请登录后投票
   发表时间:2012-05-06  
一直关注着  这个Spring3 Web MVC系列  ,比 关注 跟我学 spring 还上心

为何迄今为止 仅仅3篇文章

准备出书吗?
0 请登录后投票
   发表时间:2012-05-06  
飞天奔月 写道
一直关注着  这个Spring3 Web MVC系列  ,比 关注 跟我学 spring 还上心

为何迄今为止 仅仅3篇文章

准备出书吗?


正在努力写,最近忙着找房子没时间写

而且现在放出的都是外边没有的,看看反应 好调整思路
0 请登录后投票
   发表时间:2012-07-15  
顶啊,这么好的文章不定天理不容。如果有个参数封装类,这个类里有个user,我想在spring默认设置user参数时,我自动去实例化一个user对象,然后重新覆盖当前user对象,不知道行不行
0 请登录后投票
   发表时间:2012-07-16  
object_object 写道
顶啊,这么好的文章不定天理不容。如果有个参数封装类,这个类里有个user,我想在spring默认设置user参数时,我自动去实例化一个user对象,然后重新覆盖当前user对象,不知道行不行

谢谢,你说的我没太明白,能否详细说说。
  • 大小: 17.9 KB
0 请登录后投票
   发表时间:2012-07-24  
downpour 写道
基本上完全就是把相关的内容从reference中抽出来再贴点代码,感觉不到有作者自身的体会和任何最佳实践。

这种帖子就不应该被评精华。

期待downpour大师的springMVC
0 请登录后投票
   发表时间:2012-11-29   最后修改:2012-11-29
根据lz “使用WebBindingInitializer批量注册PropertyEditor”的方法在我的项目里也加了个日期转换PropertyEditor,但没转换成功,提示:
org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.Date]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException


代码如下:
public class MyWebBinding implements WebBindingInitializer {

	public void initBinder(WebDataBinder binder, WebRequest request) {
		 DateFormat df = new SimpleDateFormat("yyyy-MM-dd");   
	     CustomDateEditor dateEditor = new CustomDateEditor(df, true);   
        //表示如果命令对象有Date类型的属性,将使用该属性编辑器进行类型转换   
        binder.registerCustomEditor(Date.class, dateEditor);   
	}

}


springMVC-servlet.xml配置如下:
<bean id="myWebBinding" class="cn.myshop.platform.common.util.MyWebBinding"/>
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
	    <!-- 自定义属性编辑器 -->
	    <property name="webBindingInitializer" ref="myWebBinding">
	     </property>
	    		<property name="messageConverters">
			<list>
				<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
			</list>
		</property>
	</bean>


转换的字段为
 private Date invalidDate;
,前台提交值为:2012-11-27,不知道为啥转换不成功呢?
0 请登录后投票
   发表时间:2012-11-29  
caizi12 写道
根据lz “使用WebBindingInitializer批量注册PropertyEditor”的方法在我的项目里也加了个日期转换PropertyEditor,但没转换成功,提示:
org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.Date]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException


代码如下:
public class MyWebBinding implements WebBindingInitializer {

	public void initBinder(WebDataBinder binder, WebRequest request) {
		 DateFormat df = new SimpleDateFormat("yyyy-MM-dd");   
	     CustomDateEditor dateEditor = new CustomDateEditor(df, true);   
        //表示如果命令对象有Date类型的属性,将使用该属性编辑器进行类型转换   
        binder.registerCustomEditor(Date.class, dateEditor);   
	}

}


springMVC-servlet.xml配置如下:
<bean id="myWebBinding" class="cn.myshop.platform.common.util.MyWebBinding"/>
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
	    <!-- 自定义属性编辑器 -->
	    <property name="webBindingInitializer" ref="myWebBinding">
	     </property>
	    		<property name="messageConverters">
			<list>
				<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
			</list>
		</property>
	</bean>


转换的字段为
 private Date invalidDate;
,前台提交值为:2012-11-27,不知道为啥转换不成功呢?

能否把异常贴全点
0 请登录后投票
   发表时间:2012-11-29  
jinnianshilongnian 写道
caizi12 写道
根据lz “使用WebBindingInitializer批量注册PropertyEditor”的方法在我的项目里也加了个日期转换PropertyEditor,但没转换成功,提示:
org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.Date]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException


代码如下:
public class MyWebBinding implements WebBindingInitializer {

	public void initBinder(WebDataBinder binder, WebRequest request) {
		 DateFormat df = new SimpleDateFormat("yyyy-MM-dd");   
	     CustomDateEditor dateEditor = new CustomDateEditor(df, true);   
        //表示如果命令对象有Date类型的属性,将使用该属性编辑器进行类型转换   
        binder.registerCustomEditor(Date.class, dateEditor);   
	}

}


springMVC-servlet.xml配置如下:
<bean id="myWebBinding" class="cn.myshop.platform.common.util.MyWebBinding"/>
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
	    <!-- 自定义属性编辑器 -->
	    <property name="webBindingInitializer" ref="myWebBinding">
	     </property>
	    		<property name="messageConverters">
			<list>
				<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
			</list>
		</property>
	</bean>


转换的字段为
 private Date invalidDate;
,前台提交值为:2012-11-27,不知道为啥转换不成功呢?

能否把异常贴全点




org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.Date]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException
	at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:162)
	at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:198)
	at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:470)
	at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:516)
	at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1119)
	at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:924)
	at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:76)
	at org.springframework.validation.DataBinder.applyPropertyValues(DataBinder.java:692)
	at org.springframework.validation.DataBinder.doBind(DataBinder.java:588)
	at org.springframework.web.bind.WebDataBinder.doBind(WebDataBinder.java:191)
	at org.springframework.web.bind.ServletRequestDataBinder.bind(ServletRequestDataBinder.java:112)
	at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.bindRequestParameters(ServletModelAttributeMethodProcessor.java:153)
	at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:106)
	at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:74)
	at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:155)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:117)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
	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 org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
	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)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
	at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.IllegalArgumentException
	at java.util.Date.parse(Date.java:598)
	at java.util.Date.<init>(Date.java:255)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
	at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
	... 41 more
2012-11-29 11:41:20,561 DEBUG [org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver] - Resolving exception from handler [public java.util.Map<java.lang.String, java.lang.String> cn.myshop.platform.auth.control.SysUserControl.updateSysUser(cn.myshop.platform.entitys.SysUser) throws javax.servlet.ServletException,java.io.IOException]: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'sysUser' on field 'invalidDate': rejected value [2012-11-27]; codes [typeMismatch.sysUser.invalidDate,typeMismatch.invalidDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [sysUser.invalidDate,invalidDate]; arguments []; default message [invalidDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'invalidDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @org.codehaus.jackson.map.annotate.JsonSerialize java.util.Date for value '2012-11-27'; nested exception is java.lang.IllegalArgumentException]
2012-11-29 11:41:20,561 DEBUG [org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver] - Resolving exception from handler [public java.util.Map<java.lang.String, java.lang.String> cn.myshop.platform.auth.control.SysUserControl.updateSysUser(cn.myshop.platform.entitys.SysUser) throws javax.servlet.ServletException,java.io.IOException]: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'sysUser' on field 'invalidDate': rejected value [2012-11-27]; codes [typeMismatch.sysUser.invalidDate,typeMismatch.invalidDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [sysUser.invalidDate,invalidDate]; arguments []; default message [invalidDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'invalidDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @org.codehaus.jackson.map.annotate.JsonSerialize java.util.Date for value '2012-11-27'; nested exception is java.lang.IllegalArgumentException]
2012-11-29 11:41:20,576 DEBUG [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] - Resolving exception from handler [public java.util.Map<java.lang.String, java.lang.String> cn.myshop.platform.auth.control.SysUserControl.updateSysUser(cn.myshop.platform.entitys.SysUser) throws javax.servlet.ServletException,java.io.IOException]: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'sysUser' on field 'invalidDate': rejected value [2012-11-27]; codes [typeMismatch.sysUser.invalidDate,typeMismatch.invalidDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [sysUser.invalidDate,invalidDate]; arguments []; default message [invalidDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'invalidDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @org.codehaus.jackson.map.annotate.JsonSerialize java.util.Date for value '2012-11-27'; nested exception is java.lang.IllegalArgumentException]
2012-11-29 11:41:20,576 DEBUG [org.springframework.web.servlet.DispatcherServlet] - Could not complete request
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'sysUser' on field 'invalidDate': rejected value [2012-11-27]; codes [typeMismatch.sysUser.invalidDate,typeMismatch.invalidDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [sysUser.invalidDate,invalidDate]; arguments []; default message [invalidDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'invalidDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @org.codehaus.jackson.map.annotate.JsonSerialize java.util.Date for value '2012-11-27'; nested exception is java.lang.IllegalArgumentException]
	at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:110)
	at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:74)
	at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:155)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:117)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics