我的JSF2 @ManagedProperty注释有问题。我有一个创建用户的托管bean,然后我想将此用户传递给另一个bean以显示相对于该用户的其他信息。所以我的代码如下:
@Named("userController")
@SessionScoped
public class UserController implements Serializable {
@EJB
private session.UserFacade ejbFacade;
private List<User> items = null;
private User selected;
private User utente;
private String user;
private String pass;
----getter and setter----
在Schedule Bean中,我在@PostConstruct中使用userController bean:
@Named(value = "scheduleController")
@SessionScoped
public class ScheduleController implements Serializable {
private ScheduleModel eventModel;
private ScheduleEvent event = new DefaultScheduleEvent();
@EJB
ActivityFacade ejbFacade;
@ManagedProperty(value="#{userController}")
private UserController credentials;
public ScheduleController() {
}
@PostConstruct
public void init(){
eventModel = new DefaultScheduleModel();
List<Activity> daFare =ejbFacade.findForUser(credentials.getUtente().getIdUser());
for(int i=0;i<daFare.size();i++){
eventModel.addEvent(new DefaultScheduleEvent(daFare.get(i).getDescrizione(),daFare.get(i).getDalleOre(),daFare.get(i).getAlleOre() ));
}
}
public void setCredentials(UserController credentials) {
this.credentials = credentials;
}
当我调试代码时,我看到UserController凭据始终为null ....出了什么问题?我需要在faces-config.xml中指定一些东西? 非常感谢你的提示。
编辑:
使用@Inject更改@ManagedProperty后,在WEB-INF文件夹中添加以下beans.xml后,问题仍然存在。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
应用程序以这种方式工作: 用户可以通过此xhtml页面登录:
<body>
<h:form>
<p:growl id="msgs" showDetail="true" />
<p:panel header="Inserisci i tuoi dati" style="margin: 0 auto; text-align: center" >
<h:panelGrid columns="2" cellpadding="5" style="margin: 0 auto; text-align: center">
<p:outputLabel value="Username"/>
<p:inputText value="#{userController.user}" required="true" label="Username" size="40"/>
<p:outputLabel value="Password" />
<p:password value="#{userController.pass}" required="true" label="Password" size="40"/>
<p:commandButton action="#{userController.validateLogin}" value="Login" update="msgs" ajax="false" style="text-align: center"/>
</h:panelGrid>
</p:panel>
</h:form>
</body>
提交表单后,userController验证此特定用户是否存在于DB表中,然后实例化User utente(id,username,password,email as field)。在此之后,应用程序重定向到home.xhtml,显示多个用户的信息,并选择用户ID选择的数据库表的多个信息。 这是我设置User utente的代码:
public String validateLogin(){
String output="home";
boolean exist=false;
exist=ejbFacade.logValid(user,pass);
if(!exist){
//FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Username o Password errata","Contattare l'amministratore"));
return "index";
}else{
utente=ejbFacade.setUtente(user);
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("Accesso eseguito con successo.","Bentornato " + user));
return output;
}
}
我控制用户的existece并设置utente的代码序列是:
@Stateless
public class UserFacade extends AbstractFacade<User> {
@PersistenceContext(unitName = "ImmobiliareWebPU")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public UserFacade() {
super(User.class);
}
public boolean logValid(String user, String pass) {
List<User> utente ;
boolean autorizzato;
String find = "User.findByUsername&Password";
Query query = em.createNamedQuery(find, User.class);
query.setParameter("username", user);
query.setParameter("password", pass);
utente =query.getResultList();
if (utente.isEmpty()) {
autorizzato=false;
}
else{
autorizzato=true;
}
return autorizzato;
}
public User setUtente(String user) {
//SETTO L'UTENTE PER LA SESSIONE DEL PROGRAMMA
User utente;
String find = "User.findByUsername";
Query query = em.createNamedQuery(find, User.class);
query.setParameter("username", user);
utente =(User) query.getSingleResult();
return utente;
}
}
我需要用户信息的代码是:
@Named(value = "scheduleController")
@RequestScoped
public class ScheduleController implements Serializable {
private ScheduleModel eventModel;
private ScheduleEvent event = new DefaultScheduleEvent();
@EJB
ActivityFacade ejbFacade;
@Inject
private UserController credentials;
public ScheduleController() {
}
@PostConstruct
public void init(){
eventModel = new DefaultScheduleModel();
List<Activity> daFare =ejbFacade.findForUser(credentials.getUtente().getIdUser());
for(int i=0;i<daFare.size();i++){
eventModel.addEvent(new DefaultScheduleEvent(daFare.get(i).getDescrizione(),daFare.get(i).getDalleOre(),daFare.get(i).getAlleOre() ));
}
}
在ejbFacade中我做了一个简单的查询:
@Stateless
public class ActivityFacade extends AbstractFacade<Activity> {
@PersistenceContext(unitName = "ImmobiliareWebPU")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public ActivityFacade() {
super(Activity.class);
}
public List<Activity> findForUser(int utenteId) {
//RICHIEDO LE ATTIVITA' DEL SINGOLO UTENTE
List<Activity> daFare ;
String find = "Activity.findByUserId";
Query query = em.createNamedQuery(find, Activity.class);
query.setParameter("idUser", utenteId);
daFare =query.getResultList();
return daFare;
}
}
在调试模式下,我看到UserController的所有空字段的istance;它似乎是UserController的新功能。我还使用“import javax.enterprise.context.SessionScoped”。我的错在哪里?
答案 0 :(得分:3)
当我调试代码时,我看到UserController凭据是 总是空的......什么错了?
请注意,您正在将CDI (Context and Dependency Injection)与托管属性混合,这可能会导致所描述的行为。您的bean使用@Named
而不是@ManagedBean
进行注释,因此:
@ManagedProperty(value="#{userController}")
private UserController credentials;
应该替换为:
@Inject
private UserController credentials;
有关CDI和支持bean之间差异的非常好的解释,请参阅本主题中的@BalusC答案:Backing beans (@ManagedBean) or CDI Beans (@Named)?。
我需要在faces-config.xml中指定一些内容吗?
不,但你应该在这里举例说明一个beans.xml
:Packaging CDI applications。据我所知,无论发现模式如何,此文件都是必需的。以下是您可以在NetBeans中自动生成的beans.xml
:
新文件 - &gt; 上下文和依赖注入 - &gt; beans.xml(CDI配置文件)。
这是我使CDI工作所需的一切:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="annotated">
</beans>