开发者

Can ModelAttribute be primitive?

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 i开发者_开发技巧ssue?


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜