JSF复选框侦听器 [英] JSF checkbox listener

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

问题描述

<h:selectBooleanCheckbox id="deactivate" title="select to deactivate" />

我的JSF页面上有一个复选框和一个按钮.我希望仅在选中复选框后才能启用按钮.

I have a checkbox and a button on my JSF page. I want the button to be enabled only if checkbox is checked.

在我的Java类上没有手动javascript且没有侦听器方法的情况下,JSF的实现方式是什么?应该可以用类似这样的表达式来做到这一点;

What is the JSF way of doing this with no manual javascript and no listener method on my java class. There should be way of doing it maybe with an expression something like;

"panel group rendered=#{}" 

呈现一个按钮表示启用,一个按钮呈现或禁用.

one button rendered for enabled and one button rendered or disabled.

推荐答案

在我的Java类上没有JavaScript且没有侦听器方法的JSF方法是什么

这是绝对不可能的.我收集到的是您真正的意思,没有手动编写的JavaScript"(因此,您接受 <f:ajax> )和无手动编写的侦听器方法"(因此,您在组件树状态上接受JSF自己的魔力).

This is plain impossible. I gather that you actually meant, "no manually written JavaScript" (and thus you accept JSF-generated JavaScript as used by <f:ajax>) and "no manually written listener method" (and thus you accept JSF own magic on the component tree state).

在这种情况下,应该这样做:

In that case, this should do:

<h:form>
    <h:selectBooleanCheckbox binding="#{checkbox}">
        <f:ajax render="button" />
    </h:selectBooleanCheckbox>
    <h:commandButton id="button" value="submit" 
        action="#{bean.submit}" 
        disabled="#{not checkbox.value}" />
</h:form>

仅此而已.不需要其他的JS代码,JSF bean属性或侦听器方法.只是submit()操作方法.

That's all. No additional JS code nor JSF bean properties or listener methods needed. Just a submit() action method.

在此示例中,binding属性将把 #{checkbox.value} 返回组件的value属性,该属性表示 UISelectBoolean 组件已经是boolean,可以在命令组件的disabled属性中使用.

The binding attribute will in this example put the UIComponent reference of <h:selectBooleanCheckbox> in the Facelet scope under the variable name checkbox. The EL expression #{checkbox.value} returns the value attribute of the component which represents in case of an UISelectBoolean component already a boolean, ready for use in command component's disabled attribute.

注意:如果在较早的Eclipse版本中,在行disabled="#{not checkbox.value}"处遇到错误的EL错误,则需要按以下方式进行配置: Window> Preferences> Web> JavaServer Faces Tools>验证>类型强制问题>一元运算布尔强制问题将其设置为警告忽略而不是 Error .自Eclipse Mars以来,这不再是必需的.

Note: if you're facing an incorrect EL error at the line disabled="#{not checkbox.value}" in an older Eclipse version, then you'd need to configure it as follows: Window > Preferences > Web > JavaServer Faces Tools > Validation > Type Coercion Problems > Unary operation boolean coercion problems set it to Warning or Ignore instead of Error. This is not necessary anymore since Eclipse Mars.

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

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