Retrieving updated value via @ManagedProperty / notifying other beans of updated value of a property
I have a SessionScoped bean say UserSession which holds a String property token which acts as a authenticated token for the logged in user. This token is injected into other SessionScoped and ViewScoped beans so that they can consume this token and perform stuff. I have no problems injecting the property.
However, there's a use case wherein the token in the UserSession itself is replaced by a开发者_如何学编程 different String. And once I do this, the other beans still refer to the old value of the token which results in invalid access.
How can I notify the other beans of this value change? or do I retrieve bean instance through
FacesContext context = FacesContext.getCurrentInstance();
Application app = context.getApplication();
UserSession session = (UserSession) app.evaluateExpressionGet(context, "#{userSession}", UserSession.class);
And then retrieve token as session.getToken()?
I'm using Mojarra 2.0.4
I don't see why that is a problem. This more sounds like as if you're copying the property like as:
@ManagedBean
@SessionScoped
private OtherBean {
@ManagedProperty(value="#{userSession}")
private UserSession userSession;
private String token;
@PostConstruct
public void init() {
this.token = userSession.getToken();
}
public void someAction() {
doSomethingWith(token);
}
public void otherAction() {
doSomethingElseWith(token);
}
// ...
}
while you should rather be accessing it directly:
// ...
public void someAction() {
doSomethingWith(userSession.getToken());
}
public void otherAction() {
doSomethingElseWith(userSession.getToken());
}
// ...
Fix your beans accordingly to get rid of the private token property which contains the copy and just let all methods get it from the injected bean directly.
加载中,请稍侯......
精彩评论