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 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;
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
public static boolean SAVEACCEPTED = false;
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?
Don't use a
static
variable. Use a@SessionScoped
or@ViewScoped
bean to store the settings separately for each user.and
Do not use a static variable.
If you need to set the value in another bean, you can
@Inject
an instance:and CDI will give you the right instance of
Settings
.BalusC comments:
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
.My apologies for sending you down a more complicated path!
Using static variable in this scenario is not ideal. Static fields by definition are shared across all instances of the class. So what is happening is the value you are storing are getting shared for all instances of your managed bean.
I recommend you save it at the request scope with
@ViewScoped
or define it in the faces-config.xml with<managed-bean-scope>
.