JSF/JAVA布尔型切换器(对于JSF是私有的,对于Beans是公共的静态) [英] JSF/JAVA boolean switchers (private for JSF, public static for Beans)

查看:52
本文介绍了JSF/JAVA布尔型切换器(对于JSF是私有的,对于Beans是公共的静态)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用布尔切换器来解决应用程序的选定行为,例如SAVEACCEPTED启用表格的SAVE按钮.

I'm using boolean switchers to resolve a choosed behaviour of application, for example SAVEACCEPTED enables SAVE button of form.

<h:commandButton action="#{bean.save}" disabled="#{!bean.saveaccepted}">

JSF需要专用的布尔值及其getter和setter,但是如果我想解析应用程序服务器中的某些内部逻辑,则必须将其定义为静态.例如

JSF need private boolean and its getters and setters, but if I want to resolve some internal logic in application server, it must be defined static. For example

IF (USERFOUND) SAVEACCEPTED = true;

因此,我正在使用 settings类和定义了公共静态布尔值.在bean中,有指向设置的吸气剂和吸气剂.VARIABLE

So, I'm using settings class and there are public static booleans defined. And in the beans there are getters and setters pointing to the Settings.VARIABLE

Settings.java

Settings.java

public static boolean SAVEACCEPTED = false;

Bean.java

Bean.java

public static boolean isSaveAccepted() {
  return Settings.SAVEACCEPTED;
}

问题是,公共布尔值只是一个,如果有一个用户使用一个应用程序,则当第一个切换变量时,它将影响第二个用户形式.

Problem is, that the public boolean is only one and if more then one users using an application, when the first switch the variable, it affects second user form.

如何解决此问题,有一些标准的解决方案吗?

How can I solve this issue, is there some standard solution?

推荐答案

不要使用static变量.使用 @SessionScoped @ViewScoped bean,用于分别存储设置每个用户.

Don't use a static variable. Use a @SessionScoped or @ViewScoped bean to store the settings separately for each user.

@Named
@SessionScoped
public class Settings
{
    private boolean saveAccepted = false;
    
    public boolean isSaveAccepted()
    {
        return saveAccepted;
    }
    
    public void setSaveAccepted(boolean saveAccepted)
    {
        this.saveAccepted = saveAccepted;
    }
}

<h:commandButton action="#{bean.save}" disabled="#{!settings.saveaccepted}">


如果我需要在另一个bean中(而不是在JSF中)设置saveAccepted = true怎么办?它不起作用,因为在那种情况下,saveAccepted必须是静态的.

What if i'd need to set the saveAccepted = true in another bean (not in JSF)? It doesn't work, because in that case, saveAccepted have to be static.

不要不要使用静态变量.

如果需要在另一个bean中设置值,则可以

Do not use a static variable.

If you need to set the value in another bean, you can @Inject an instance:

@Named
@RequestScoped
public class SomeOtherBean
{
    @Inject
    private Settings settings;
    
    public boolean getSaveAccepted()
    {
        return settings.getSaveAccepted();
    }
    
    public void setSaveAccepted(boolean saveAccepted)
    {
        settings.setSaveAccepted(saveAccepted);
    }
}

CDI 将为您提供正确的实例Settings.

and CDI will give you the right instance of Settings.

BalusC 注释:

根据问题历史记录,OP使用的是Tomcat,它只是一个简单的servlet容器.

Based on the question history, OP is using Tomcat which is just a simple servletcontainer.

由于您似乎没有使用完整的Java EE 6容器,因此可以使用@ManagedBean代替@Named@ManagedProperty代替@Inject.

Since it looks like you're not using a full Java EE 6 container, you can use @ManagedBean instead of @Named and @ManagedProperty instead of @Inject.

@ManagedBean
@RequestScoped
public class SomeOtherBean
{
    @ManagedProperty
    private Settings settings;
    
    public boolean getSaveAccepted()
    {
        return settings.getSaveAccepted();
    }
    
    public void setSaveAccepted(boolean saveAccepted)
    {
        settings.setSaveAccepted(saveAccepted);
    }
}

很抱歉向您发送更复杂的信息!

My apologies for sending you down a more complicated path!

这篇关于JSF/JAVA布尔型切换器(对于JSF是私有的,对于Beans是公共的静态)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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