ModelAttribute 可以是原始的吗?

发布于 2024-11-24 04:17:30 字数 2383 浏览 2 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

皓月长歌 2024-12-01 04:18:22

我只是提出一些想法,希望能有所帮助。

堆栈跟踪显示它确实尝试长时间调用构造函数方法:

java.lang.NoSuchMethodException: long.<init>()

如果将使用的类型从 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:

java.lang.NoSuchMethodException: long.<init>()

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文