我正在尝试将一个托管bean注入另一个托管bean(我只需要在一个托管bean中执行操作,如果用户是loggen,并且该用户保存在另一个托管bean上)。但是返回NULL!每个托管bean链接到不同的XHTML页面,因此可能会创建一个新的HTTP会话?
第一个托管bean:
@Named(value = "travellerLoginManagedBean" )
@SessionScoped
public class travellerLoginManagedBean implements Serializable {
@EJB
private travellerUserEntityFacadeLocal travellerUserEntityFacade;
private String userName;
private String password;
private Boolean loggedIn;
/**
* Creates a new instance of travellerLoginManagedBean
*/
public travellerLoginManagedBean() {
}
public Boolean getLoggedIn() {
return loggedIn;
}
public void setLoggedIn(Boolean loggedIn) {
this.loggedIn = loggedIn;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Boolean validUser(){
loggedIn=true;
return travellerUserEntityFacade.checkLogin(this.userName, this.password)==true;
}
public String login() {
if (validUser()==true){
return "/travellerHome.xhtml";
}else{
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Usuario y/o password incorrecto!"));
return "/travellerLogin.xhtml";
}
}
}
第二个托管bean:
@Named(value = "travellerSearchFlightsManagedBean")
@RequestScoped
public class travellerSearchFlightsManagedBean{
@EJB
private itineraryTravellerFacadeLocal itineraryTravellerFacade;
@EJB
private itineraryFacadeLocal itineraryFacade;
@ManagedProperty(value="#{travellerLoginManagedBean}")
private travellerLoginManagedBean loginBean;
private String departureLocation;
private String arrivalLocation;
private Date departureDate;
private int departureHour;
private int departureMinute;
private int numPassengers;
private int maxStops;
private String economyOrBusiness;
private ArrayList<itineraryDTO> itinerariesList;
private ArrayList<itineraryTraveller> searchList;
private itineraryTraveller selectedItinerary;
private String stopPrint;
private static final Logger LOG = Logger.getLogger(travellerSearchFlightsManagedBean.class.getName());
( ......)
public void reserveFlight(){
if (loginBean.getLoggedIn()==true){
System.out.println("iiiiiiiiiiiiiiiiin");
//status reserved when traveller reserves an itinerary
selectedItinerary.setStatus('r');
//set the userName from the user who reserved this itinerary before persist it in a shared among all users db
selectedItinerary.setUserName(loginBean.getUserName());
//persist the reserved itinerary in the db
itineraryTravellerFacade.create(selectedItinerary);
}else{
System.out.println("not iiiiiiiiiiiiiiiiin");
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Must be logged in before reserving an itinerary"));
}
}
}
答案 0 :(得分:0)
为了工作,Bean注入需要:
添加@ManagedProperty
注释(您已做过)
在faces-config.xml
文件中添加其他Bean的引用。
来自Apache Documentation的faces-config.xml
示例应该澄清:
<managed-bean>
<managed-bean-name>neededBean</managed-bean-name>
<managed-bean-class>fqn.to.NeededBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-beaenter code heren-name>usingBean</managed-bean-name>
<managed-bean-class>fqn.to.UsingBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>neededBean</property-name>
<value>#{neededBean}</value>
</managed-property>
</managed-bean>
在您的具体案例中:
NeededBean
= travellerLoginManagedBean
UsingBean
= travellerSearchFlightsManagedBean
Apache文档还指出使用Bean注入的唯一约束是:
如果您需要更多信息,请查看Apache Documentation。
PS:我注意到你用小写字母写了类名,但是使用Java代码约定(CamelCase),每个类名都应该以大写字母开头。