JSF2.0 Passing Value objects between managed beans
I've already written a small JSF2.0 app utilising Weblogic 10.3.4, PrimeFaces and JQuery. I'm now looking at converting our Main Web App to JSF2.0. This is currently uses Weblogic 8.1, Java 1.4 and JSP's. The question I have at the moment is what is best way to pass objects from one managed bean to another. Our app consists of many screens but the general pattern is a reference is entered on the first screen and on submit this is looked up from the Database and a Value Object is populated (standard java bean). Screen 2 is then returned which is generally a form consisting of the Value Object's vari开发者_Go百科ables ready for edit.
Currently all required objects are saved as an attribute in an HTTPServletRequest object in the 1st screen (within a custom written controller class) and then retrieved from this in the subsequent screen.
Is this still the way to do it or is there a new "JSF" way that I've missed. I did also think about storing these Value Objects in a user session bean (which we will have anyway) and then retrieving from there when needed. I assume a Map containing Value Objects would be the best way to go in this case?
You can inject a managed bean in another managed bean by @ManagedProperty
.
Assuming that you've a session scoped bean like this
@ManagedBean
@SessionScoped
public class User {
// ...
}
And a request scoped bean like this
@ManagedBean
@RequestScoped
public class Profile {
@ManagedProperty(value="#{user}") // #{user} is the managed bean name
private User user;
@PostConstruct
public void init() {
// User is available here for the case you'd like to work with it
// directly after bean's construction.
}
public String save() {
// User is available here as well, during action methods.
userDAO.save(user);
}
// +getter +setter
}
精彩评论