`
gogomarine
  • 浏览: 99677 次
  • 性别: 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) {
        
   }
}
 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics