开发者

disabling and rendering components using by private boolean - other way

i have开发者_运维百科 a cosmetic problem. I'm using

private boolean production = true;

(+ getters and setters)

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?

Thank you


I'm looking for more elegant solution. Because JSF wants it private + getters and setters, and the other JAVA class wants it private STATIC + get, sets. So I sometimes need to use two booleans for one thing and it makes a bit of mess...

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

public boolean isProduction() {
    return 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']}".

E.g.

@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);
        // ...
    }

}

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>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜