ModelAttribute 可以是原始的吗?
我在 Spring MVC 3.0 中的 ModelAttribute 上遇到了一个奇怪的问题。当我在本地主机部署应用程序时,它工作正常。但是,当我在远程服务器上部署该应用程序时,每次用户访问特定操作时它都会失败,并出现错误:
ERROR: my.package.application.web.filter.ExceptionFilter - long.<init>()
java.lang.NoSuchMethodException: long.<init>()
at java.lang.Class.getConstructor0(Class.java:2706)
at java.lang.Class.getDeclaredConstructor(Class.java:1985)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:104)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveModelAttribute(HandlerMethodInvoker.java:762)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:356)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:153)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
我用户在该控制器中初始化 ModelAttribute 的代码是:
@ModelAttribute("id")
public long getUserId(HttpSession session) {
UserInfoHolder userHolder = (UserInfoHolder) session
.getAttribute("userHolder");
long userId = userHolder.getUserId();
return userId;
}
据我所知,该错误无法在我的本地工作站上复制。它发生在调用操作之前。
查看 HandlerMethodInvoker.java(第 762 行),我们看到这一行:
bindObject = BeanUtils.instantiateClass(paramType);
我的一位经验丰富的同行认为这一行会导致问题,因为原始类型 ModelAttribute(long) 没有构造函数。我认为这个理由也许是对的,但是如何解释该 Web 应用程序在我的本地服务器上运行良好呢?
我尝试搜索 ModelAttribute 是否支持原始数据类型,但没有好的结果。有人对这个问题有经验吗?
I am having a strange problem with ModelAttribute in Spring MVC 3.0. When I deploy the app at localhost, it works fine. But when I deploy the app on a remote server, it fails everytime user access a specific action, with the errors:
ERROR: my.package.application.web.filter.ExceptionFilter - long.<init>()
java.lang.NoSuchMethodException: long.<init>()
at java.lang.Class.getConstructor0(Class.java:2706)
at java.lang.Class.getDeclaredConstructor(Class.java:1985)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:104)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveModelAttribute(HandlerMethodInvoker.java:762)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:356)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:153)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
The code that I user to init the ModelAttribute in that controller is:
@ModelAttribute("id")
public long getUserId(HttpSession session) {
UserInfoHolder userHolder = (UserInfoHolder) session
.getAttribute("userHolder");
long userId = userHolder.getUserId();
return userId;
}
As far as I can tell, the bug can't be reproduced at my local workstation. And it happens before the action get called.
Looking in HandlerMethodInvoker.java (line 762), we see this line:
bindObject = BeanUtils.instantiateClass(paramType);
An experienced peer of mine believes that this line causes problem, because that a primitive-type ModelAttribute(long) doesn't have a constructor. I think that reason maybe right, but how can it explain that the web application works fine on my local server?
I tried to search to know if ModelAttribute support primitive data type, but with no good results. Does anyone have experience about this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只是提出一些想法,希望能有所帮助。
堆栈跟踪显示它确实尝试长时间调用构造函数方法:
如果将使用的类型从 long 更改为 Long ,它会起作用吗?
至于在本地工作但不在客户端工作的事情。 Java版本一样吗?
I'm just throwing in some thoughts, hoping it might help a bit.
The stack trace shows that it is indeed trying to call the constructor method for long:
Does it work if you change the used type from long to Long?
As for things working locally but not on the client. Is the Java version the same?