通过私有布尔禁用和渲染组件-其他方式 [英] disabling and rendering components using by private boolean - other way

查看:46
本文介绍了通过私有布尔禁用和渲染组件-其他方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个化妆上的问题.我正在使用

i have a cosmetic problem. I'm using

private boolean production = true;

(+ getter和setters)

(+ getters and setters)

了解应用程序的内部逻辑.在JSF中,我称其为呈现或禁用组件,例如

for inner logic of application. And in the JSF I'm calling it for rendering or disabling components, for example

<h:commandButton id="f1" action="#{bean.save}" type="submit" disabled="#{bean.production}"/>

它有效.但是在我看来,这有点丑陋.

And it works. But it seems to me a bit ugly.

还有其他方法可以实现相同的功能吗?

Is there some other way to achieve the same functionality?

谢谢

推荐答案

我正在寻找更优雅的解决方案.因为JSF希望将其设为private + getters和setters,而另一个JAVA类希望将其设为private STATIC + get,set.所以有时候我需要对两个对象使用两个布尔值,这会使事情变得一团糟...

只需让JSF bean getter委托即可.例如

Just let the JSF bean getter delegate. E.g.

public boolean isProduction() {
    return Settings.PRODUCTION;
}

这样,您最终只能得到一个包含值的变量,即Settings.PRODUCTION.您甚至可以将其与请求/视图范围的Bean分离,并将其包装在应用程序范围的托管Bean中.甚至,如果让它扩展Map并将其值放在构造上,那么您将能够使用类似disabled="#{settings['PRODUCTION']}"的东西.

This way you end up with only one variable containing the value, namely Settings.PRODUCTION. You can even decouple it from the request/view scoped bean and wrap it in an application scoped managed bean. Even more, if you let it extend Map and put the values upon construction, then you'll be able to use something like disabled="#{settings['PRODUCTION']}".

例如

@ManagedBean(name="settings", eager=true)
@ApplicationScoped
public class SettingsManager extends HashMap<String, Object> {

    public SettingsManager() {
        put("PRODUCTION", Settings.PRODUCTION);
        put("DEBUG", Settings.DEBUG);
        put("HOSTNAME", Settings.HOSTNAME);
        // ...
    }

}

如果您仍在使用JSF 1.x,请删除这些注释并按照faces-config.xml中的说明进行映射.

If you're still on JSF 1.x, remove those annotations and map it as follows in faces-config.xml.

<managed-bean>
    <managed-bean-name>settings</managed-bean-name>
    <managed-bean-class>com.example.SettingsManager</managed-bean-class>
    <managed-bean-scope>application</managed-bean-scope>
</managed-bean>

这篇关于通过私有布尔禁用和渲染组件-其他方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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