`
gogomarine
  • 浏览: 101264 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring mvc -- 部分异常解决小记

阅读更多

有段时间没搞java web编程了,很多错误都比较低级,不应该出现,小记,提醒自己应该细心。

 

 

  1.  关于用spring upload文件的错误:

 

    写了测试页面,然后进行文件上传测试,忽然发现之前能够用curl调试成功的接口,居然报错了。错误信息如下:

    

2010-09-27 11:12:44,926 DEBUG [org.springframework.beans.BeanUtils] - No property editor [org.springframework.web.multipart.MultipartFileEditor] found for type org.springframework.web.multipart.MultipartFile according to 'Editor' suffix convention
2010-09-27 11:12:44,927 DEBUG [org.springframework.web.servlet.handler.SimpleMappingExceptionResolver] - Resolving exception from handler [com.roosher.web.mvc.SpaceController@15f6479]: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.MultipartFile'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.MultipartFile]: no matching editors or conversion strategy found
2010-09-27 11:12:44,928 DEBUG [org.springframework.web.servlet.handler.SimpleMappingExceptionResolver] - Resolving to view 'uncaughtException' for exception of type [org.springframework.beans.ConversionNotSupportedException], based on exception mapping [.lang.Exception]
2010-09-27 11:12:44,928 DEBUG [org.springframework.web.servlet.handler.SimpleMappingExceptionResolver] - Exposing Exception as model attribute 'exception'
2010-09-27 11:12:44,932 DEBUG [org.springframework.web.servlet.DispatcherServlet] - Handler execution resulted in exception - forwarding to resolved error view: ModelAndView: reference to view with name 'uncaughtException'; model is {exception=org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.MultipartFile'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.MultipartFile]: no matching editors or conversion strategy found}
org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.MultipartFile'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.MultipartFile]: no matching editors or conversion strategy found

    主要错误就是

    

org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.MultipartFile';

    怎么看都不知道错在哪里,看异常提示以为是编写PropertiesEditor的错误,或者跟Convert相关,但找寻代码没有看着痕迹。最后在调试页面看到低级的错误:

 

 

<form action="/roosher-backend/space/image" method="POST" enctype="application/x-www-form-urlencoded">   
   ......
</form>

     原来是enctype错了,应该是 enctype="multipart/form-data"

 


     2.   关于BindingResult
     Spring mvc2.5,3.0中,有关于 @ModelAttribute的注解,它可以帮你自动绑定表单的值,填充到被注解的属性当中。如 @ModelAttribute User user,该属性会在ModelMap中,添加以user为key的 User对象,然后绑定的时候会有一些绑定结果,就是BindingResult。但是BindingResult跟@ModelAttribute的相对位置比较重要,也就说跟方法参数,方法签名有关系。
BindingResult必须紧跟@ModelAttribute的后面,如

@Controller
public class Payer {
   
   @RequestMapping(...)
   public String pay(@ModelAttribute User user,BindingResult result) {
        
   }
}
 

如果是类似下面的情况就可能有"java.lang.IllegalStateException: Errors/BindingResult argument declared without preceding model attribute. Check your handler method signature!"抛出来,具体可以查看org.springframework.web.bind.annotation.support.HandlerMethodInvoker

@Controller
public class Payer {
   
   @RequestMapping(...)
   public String pay(@ModelAttribute User user,@RequestParam Hello hello,
	BindingResult result) {
        
   }
}
 

 

分享到:
评论

相关推荐

    spring 小记

    这篇小记将深入探讨Spring的主要组件和关键特性,带你一步步走进Spring的世界。 首先,我们要理解Spring的核心——依赖注入。在传统的Java程序中,对象之间的依赖关系通常是硬编码的,这使得代码难以测试和维护。...

    java小记.rar

    Spring MVC或Struts2等框架简化了这种模式的实现。作者可能记录了如何配置这些框架、创建控制器、模型和视图,以及如何进行单元测试。 除此之外,企业级Java组件如EJB(Enterprise JavaBeans)、JMS(Java Message ...

    J2EE开发之常用开源项目小记

    - SpringMVC是Spring框架的一部分,用于构建Web应用程序的模型-视图-控制器架构。配置文件通常是`WEB-INF/xxxx-servlet.xml`,其中`xxxx`对应于`web.xml`中的`DispatcherServlet`名称。SpringMVC的工作原理是根据...

    财经面试知识点.zip

    目錄Java基础篇基础鎖多线程并发对象JVMJVM内存结构堆和差Java内存模型垃圾回收...小记源码迈巴提斯春天Spring MVCJava基础篇基础Java的面向对象Java语言的三大特征封装、继承和多态Java 语言类型Java的自动类型转换...

    Dislay分页

    5. **兼容性**:DisplayTag与主流的Java Web框架如Struts、Spring MVC等良好集成,可以无缝地融入到现有项目中。 `displaytag.properties`是DisplayTag的配置文件,用于设置标签库的全局属性,如默认的导出格式、...

    Big-Data-Interview:大数据面试知识点

    目录Linux、IOLinux基础IO分布式分布式理论数据结构与算法RedisRedis基础redis内部数据结构Git操作系统数据库MeavnHBaseHiveSpark面试计算机网络Spring面试RedisElasticsearch分布式JVM设计模式多线程JDK数据结构...

Global site tag (gtag.js) - Google Analytics