2
我想從一個託管bean到另一個託管bean的值。我有一個loginbean獲取用戶名,我需要把這個值放到另一個託管bean sidebarbean中。我正在使用JSF,並認爲我可以去像 #{sidebarbean.setUserName(loginbean.username)}
但這不起作用。有什麼建議麼?如何將java bean的屬性傳遞給另一個java bean?
按照你的建議
@ManagedBean(name = "sidebarbean")
@SessionScoped
public class SideBarBean {
private ArrayList myContacts = new ArrayList();
private String user;
@PersistenceContext
private EntityManager em;
@Resource
private UserTransaction utx;
public SideBarBean() {
}
public ArrayList getMyContacts() {
myContacts.clear();
List<Contacts> list = em.createNamedQuery("Contacts.findByUsername")
.setParameter("username", user).getResultList();
for (Contacts c : list) {
myContacts.add(c.getContact());
}
return myContacts;
}
public void setMyContacts(ArrayList myContacts) {
this.myContacts = myContacts;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
}
@ManagedBean(name = "loginbean")
@SessionScoped
public class LoginBean {
@PersistenceContext
private EntityManager em;
@Resource
private UserTransaction utx;
@ManagedProperty(value="#{sidebarbean}")
private SideBarBean sbb;
private String username;
private String password;
private Boolean verified = false;
private Boolean authFail = false;
public LoginBean() {
}
public void update(){
sbb.setUser(username);
}
我得到「無法創建託管bean loginbean結果發現以下問題: - 屬性SBB爲託管bean loginbean不存在檢查適當的getter和/或setter方法。存在。」