JSF 2复选框和布尔getter [英] JSF 2 checkboxes and boolean getters

查看:55
本文介绍了JSF 2复选框和布尔getter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于webservice生成一个jaxws客户端。 Jaxb将使用java.lang.Boolean而不是基本类型生成布尔值。除此之外,它还将为bean生成is()命名约定。

I'm generating a jaxws client based on webservice. Jaxb will generate booleans using the java.lang.Boolean instead of the primitive type. In addition to this, it will generate the is() naming convention for beans.

但是,如果我尝试将布尔值(例如isOptional())链接到复选框,它将抛出以下异常:

However if I try to link the boolean (e.g. isOptional()) to a checkbox, it will throw the following exception:

value="#{property.optional}": Property 'optional' not readable on type java.lang.Boolean

我的谷歌技能告诉我jsf工作正常:

My google skills have informed me that jsf works fine with:

 boolean isOptional()
 boolean getOptional()
 Boolean getOptional()

但不是

Boolean isOptional()

然而,由于web服务的大小和数量,手动更新bean是不可行的,所以有没有办法让jsf正确使用java.lang.Boolean isOptional()?或者我可以在生成时以某种方式在jaxb绑定文件中定义一个属性,它会神奇地生成getOptional()?

However it is not feasible to update the beans manually due to the size and amount of the webservices, so is there any way to make jsf use the java.lang.Boolean isOptional() properly? Or can I somehow define a property in the jaxb bindings file at generation time which magically generates "getOptional()"?

在旁注中,以下内容确实有效:

On a sidenote, the following does work:

<h:selectBooleanCheckbox value="#{property.isOptional()}"/>

但是我实际上无法更新该值,因为它无法找到设置器。

However I can't actually update the value presumably because it can't find the setter.

编辑:我正在运行最新的jdk 7,输出java -version:

EDIT: I'm running the latest jdk 7, the output of "java -version":

java version "1.7.0_05"
Java(TM) SE Runtime Environment (build 1.7.0_05-b05)
Java HotSpot(TM) Client VM (build 23.1-b03, mixed mode, sharing)

wsimport -version的输出:

The output of "wsimport -version":

JAX-WS RI 2.2.4-b01

生成的代码:

public Boolean isOptional() {
    return optional;
}


推荐答案


Jaxb将使用java.lang.Boolean而不是基本类型生成布尔值。除此之外,它还将为bean生成is()命名约定。

使用 的getter前缀> java.lang.Boolean 是JAXB的一个已知重大错误。 已在2.1.13版修复,已于2010年4月发布。让您的图书馆保持最新状态。

Using the is getter prefix for java.lang.Boolean was a known major mistake of JAXB. It has been fixed in version 2.1.13 which was released April 2010 already. Keep your libraries up to date.

另见这篇博客文章了解一些背景信息。

See also this blog article for some background.


Great JAXB API错误



2006年9月15日

你必须把它交给Sun来搞砸这个。当文档与教科书一样厚时,编写不符合规范的软件是一回事。举个例子来说,就是W3C创造的任何东西。然而,当你自己的规范无法遵循时,它真的很糟糕,特别是当它是最知名的部分时。没错,在他们创建JAXB 2.0 API时,Sun在他们自己的规范上错过了一英里。在为java.lang.Boolean属性生成getter方法时,JAXB 2.0编译器(XJC)错误地使用前缀is而不是get。虽然JavaBean规范指出原始布尔值的读取方法可以使用备用is前缀,但这种灵活性并没有扩展到它的布尔包装器对应物。

You've got to hand it to Sun for screwing this one up. It's one thing to write software that doesn't adhere to a specification when the documentation is as thick as a textbook. Take, for example, just about anything created by the W3C. However, it's really bad when it is your own spec that you can't follow, especially when it is the most well known part of it. That's right, Sun missed by a mile on their own spec when they created the JAXB 2.0 API. The JAXB 2.0 compiler (XJC) incorrectly uses the prefix "is" rather than "get" when generating the getter method for a java.lang.Boolean property. While the JavaBean spec states that read methods for primitive booleans can use the alternate "is" prefix, this flexibility does not extend to its boolean wrapper counterpart.




8.3.2布尔属性

8.3.2 Boolean Properties

此外,对于布尔属性,我们允许使用getter方法匹配模式:

In addition, for boolean properties, we allow a getter method to match the pattern:

public boolean is();

可以提供is方法而不是get方法,或者可以提供除了获取方法。在任何一种情况下,如果布尔属性存在is方法,那么我们将使用is方法来读取属性值。

This "is" method may be provided instead of a "get" method, or it may be provided in addition to a "get" method. In either case, if the "is" method is present for a boolean property then we will use the "is" method to read the property value.

示例布尔属性可能是:

public boolean isMarsupial();
public void setMarsupial(boolean m);




鉴于JAXB是一个代码生成框架,代码生成框架背后的想法是代码将按原样使用,之后不再修改,这是一个非常大的oops。虽然已经报道了这个问题,但Sun的回复是抱歉,为时已晚。

Given that JAXB is a code generation framework, and the idea behind code generation frameworks is that the code is to be used "as is" and not modified thereafter, this is a pretty big "oops". While this issue has been reported, the response from Sun is "sorry, its too late".

此行为受规范控制,不幸的是,现在为时已晚现在要改变的规格。

This behavior is governed by the spec, and unfortunately it's just too late for the spec to change now.

就用户体验而言,多亏了自动拳击,我认为这对人们来说不是一个真正的问题。问题是您使用的是Introspector并且它缺少属性?
太晚了?不是真正的问题吗?这是破产。修理它!我也不喜欢它可能不会影响框架的天真声明。嗯,是的,考虑到其他项目确实碰巧遵守了规范(hibernate,spring,myfaces等)。

In terms of the user experience, thanks to auto-boxing, I don't think this will be a real issue for people. Is the problem that you are using Introspector and it's missing the property? Too late? Not a real issue? It's BROKEN. FIX IT! I also don't like the naive statement that it probably won't affect frameworks. Um, yes it will, considering other projects did happen to adhere to the spec (hibernate, spring, myfaces, etc.)

更新:Stevo Slavic告诉我,这有已在JAXB 2.1.13中修复。有关详细信息,请参见 JAXB-131 。是啊!

UPDATE: Stevo Slavic informed me that this has been fixed in JAXB 2.1.13. See JAXB-131 for details. Yeah!

JSF / EL在这里没有错。它的工作正确地符合 JavaBeans规范

JSF/EL is not at fault here. It's doing its job properly conform the JavaBeans spec.

这篇关于JSF 2复选框和布尔getter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆