jsf 2 best one managed bean multiple views

喜夏-厌秋 提交于 2019-12-13 19:09:34

问题


I`m kind of noob to JSF and I'm trying to figure out which would be the most elegant solution for the following scenario:
Let's say that I have a user managed bean called UserMB:

@ManagedBean
public class UserMB {

private User user;
private List<User> users;

// getters and setters here

public void addUser(User user){
    // do add user logic here
}


public List<User> listAllUsers(){
    // do list All users logic here
}


@PostConstruct
private void init(){

    // populate List<user> users - for the listAllUsers scenario

    }   
}

Let`s assume that i do not have a form to submit directly to listAllUsers() method, but instead i want to see all users when I open the page list-all-users.xhtml. When I hit the managed bean from addUser.xhtml a query will be performed to DB to load all users because the bean will not know if i want to use listAllUsers() method or addUser() method.
Should i split this functionality in 2 managed beans ?

Because if so I would have to create several managed beans to deal with "User" business (ie. in Struts2 i would have only one Action that would take care of all user interactions).
P.S. I know that there is the solution to populate List in getter method but I read one article of BalusC that advise us not to do this...


回答1:


Should i split this functionality in 2 managed beans ?

Yes. Use one bean per view/form. Keep the backing bean class as slick as possible. Don't give it too much responsibilities.



来源:https://stackoverflow.com/questions/18663861/jsf-2-best-one-managed-bean-multiple-views

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!