简体   繁体   English

原始包装类型与Spring MVC的绑定性质

[英]Binding Properties of Primitive Wrapper Types with Spring MVC

It appears to me that Spring MVC cannot bind properties of primitive wrapper types(eg Integer, Boolean, etc). 在我看来,Spring MVC不能绑定原始包装类型的属性(例如Integer,Boolean等)。 When it tries to bind such properties, it throws the following exception. 当它尝试绑定这些属性时,它会抛出以下异常。

javax.servlet.ServletException: javax.servlet.jsp.JspException: javax.servlet.jsp.JspException: org.springframework.beans.NotReadablePropertyException: Invalid property 'assigned' of bean class [model.domain.Employee]: Bean property 'assigned' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:858)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)
    org.apache.jsp.WEB_002dINF.jsp.configuration.form.defaultForm_jsp._jspService(defaultForm_jsp.java:87)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1063)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:801)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.tiles.web.startup.TilesFilter.doFilter(TilesFilter.java:75)

As soon as I changed the type of the 'assigned' property(getter, setter and instance variable) from Boolean to boolean, it worked fine. 一旦我将'assigned'属性(getter,setter和instance变量)的类型从Boolean更改为boolean,它就可以正常工作。

Can anyone confirm Spring MVC cannot bind properties of primitive wrapper types? 任何人都可以确认Spring MVC无法绑定原始包装类型的属性吗? If so, is there any workaround? 如果是这样,有没有解决方法? I can't change the property type to boolean because a null value in this property means something for my application. 我无法将属性类型更改为boolean,因为此属性中的null值意味着我的应用程序。

Thanks. 谢谢。

Spring MVC works fine with Boolean . Spring MVC与Boolean工作正常。

This error may appear if your getter for Boolean is named isAssigned() . 如果Boolean的getter命名为isAssigned()则可能会出现此错误。 According to JavaBeans naming conventions , only boolean may have this form of getter name, Boolean should have getAssigned() . 根据JavaBeans命名约定 ,只有boolean可能具有这种形式的getter名称, Boolean应该具有getAssigned()

On workaround would be to create / register your own property editors. 解决方法是创建/注册您自己的属性编辑器。

Another workaround would be to add a second setter that allows you to set the property as a 'boolean'. 另一种解决方法是添加第二个setter,允许您将属性设置为“boolean”。 (You may need to fiddle around with your APIs to subvert Spring's "helpful" insistence that the getter and setter types parameter / result types match. If I'm doing this kind of thing, I typically end up with two setters with different names for the same logical property.) (你可能需要摆弄你的API来破坏Spring的“有用”的坚持,即getter和setter类型的参数/结果类型匹配。如果我正在做这种事情,我通常会得到两个不同名字的setter相同的逻辑属性。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM