一个springmvc的@RequestParam异常解决

firefox报错:

纯文本文件的字符编码未声明。如果该文件包含 US-ASCII 范围之外的字符,该文件将在某些浏览器配置中呈现为乱码。该文件的字符编码需要在传输协议层声明,或者在文件中加入一个 BOM(字节顺序标记)

截个图:


,看了下后台,也报错:


大概意思就是参数找不到,无法匹配。

由于这个异常时软件发布(jre运行时环境)时出现的,而开发环境没事,挺意外的。就对比看了下firefox的2种情况:

开发环境下,访问正常,response的反馈解决没问题:


而真实的部署环境,访问测试异常,尽管也是200,但conten-type头文件类型和编码都没有:



后来测试返现,字符串的ajax情况都可以,只有跳转页面的情况这个样子,而且这在真实的部署环境才这样!好诡异。。。


后来,使用后台异常 的关键字 百度,找到类似异常,解决了问题:

https://www.cnblogs.com/ginkgo5198/p/7248451.html

原因出在,

在使用SpringMVC绑定基本类型(如String,Integer等)参数时,应通过@RequestParam注解指定具体的参数名称,否则,当源代码在非debug模式下编译后,运行时会引发HandlerMethodInvocationException异常,这是因为只有在debug模式下编译,其参数名称才存储在编译好的代码中。


虽然异常名称不同,但处理方式相同。添加@RequestParam(name="",required=true)即可,使用name或value是一样的,且默认requred为true,即不指定时为true


之所以到我这里问题变得这么复杂,原因就多了:

第一:页面为何如此,原来在于springmvc的@RequestParam,由于默认是required=true,当没有这个参数时,框架自动反馈200后客户端(框架自动处理),所以才出现刚刚上图的都是200的情况

第二:为什么开发环境的访问测试没问题,而真实部署才出现。

          这是因为真实环境的编译代码是debug=false(ant编译时指定生成class文件,然后和jsp、lib拼凑成安装包);

          而开发环境的IDE是eclipse,默认是debug=true自动编译的(没有ant啥事)


教训:让你再不看spring官方文档!!!被@RequestParam耍了吧。



下面是刚刚那个参考文件的备份:

https://www.cnblogs.com/ginkgo5198/p/7248451.html

No parameter name specified for argument of type

在使用SpringMVC绑定基本类型(如String,Integer等)参数时,应通过@RequestParam注解指定具体的参数名称,否则,当源代码在非debug模式下编译后,运行时会引发HandlerMethodInvocationException异常,这是因为只有在debug模式下编译,其参数名称才存储在编译好的代码中。

譬如下面的代码会引发异常:

Java代码   收藏代码
  1. @RequestMapping(value = "/security/login", method = RequestMethod.POST)  
  2. public ModelAndView login(@RequestParam String userName, @RequestParam String password,   
  3.     HttpServletRequest request) {  
  4.     ......................  



如果使用Eclipse编译不会在运行时出现异常,这是因为Eclipse默认是采用debug模式编译的,但是如果使用Ant通过javac任务编译的话就会出现异常,除非指定debug=”true”。出现的异常如同:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public org.springframework.web.servlet.ModelAndView com.mypackage.security.controller.LoginController.login(java.lang.String,java.lang.String,javax.servlet.http.HttpServletRequest)]; nested exception is java.lang.IllegalStateException: No parameter name specified for argument of type [java.lang.String], and no parameter name information found in class file either.
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:659)
..........

org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public org.springframework.web.servlet.ModelAndView com.mypackage.security.controller.LoginController.login(java.lang.String,java.lang.String,javax.servlet.http.HttpServletRequest)]; nested exception is java.lang.IllegalStateException: No parameter name specified for argument of type [java.lang.String], and no parameter name information found in class file either.
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
..........


java.lang.IllegalStateException: No parameter name specified for argument of type [java.lang.String], and no parameter name information found in class file either.
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.getRequiredParameterName(HandlerMethodInvoker.java:618)
..........

最好的做法是通过@RequestParam注解指定具体的参数名称,如,上面的代码应该如此编写(注意@RequestParam部分):

Java代码   收藏代码
  1. @RequestMapping(value = "/security/login", method = RequestMethod.POST)   
  2. public ModelAndView login(@RequestParam("userName") String userName,   
  3.     @RequestParam("password") String password,    
  4.     HttpServletRequest request) {   
  5.     ......................  


参考:http://stackoverflow.com/questions/2622018/compile-classfile-issue-in-spring-3


猜你喜欢

转载自blog.csdn.net/rainyspring4540/article/details/78874596