我在这个项目中遇到了JSF / Bean的问题。
错误就是
/index.xhtml @32,73 value="#{huMBean.func.login}": Target Unreachable, identifier 'huMBean' resolved to null
我的课程是这些
public class Funcionarios implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "CODF")
private Integer codf;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50)
@Column(name = "LOGIN")
private String login;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 20)
@Column(name = "SENHA")
private String senha;
@Size(max = 50)
@Column(name = "NOME")
private String nome;
public Funcionarios() {
}
public Funcionarios(Integer codf) {
this.codf = codf;
}
public Funcionarios(Integer codf, String login, String senha) {
this.codf = codf;
this.login = login;
this.senha = senha;
}
public Integer getCodf() {
return codf;
}
public void setCodf(Integer codf) {
this.codf = codf;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getSenha() {
return senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
@Override
public int hashCode() {
int hash = 0;
hash += (codf != null ? codf.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are `enter code here`not set
if (!(object instanceof Funcionarios)) {
return false;
}
Funcionarios other = (Funcionarios) object;
if ((this.codf == null && other.codf != null) || (this.codf != null && !this.codf.equals(other.codf))) {
return false;
}
return true;
}
@Override
public String toString() {
return "entidades.Funcionarios[ codf=" + codf + " ]";
}
Object setParameter(String login, String login0) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
豆
@Stateless
public class HUBean implements HUBeanLocal {
@PersistenceContext(unitName = "SistemaHU-ejbPU")
private EntityManager em;
public void persist(Object object) {
em.persist(object);
}
/**
*
* @param login
* @param senha
* @return
*/
@Override
public Funcionarios loginFuncionario(String login, String senha) {
try {
TypedQuery<Funcionarios> retorno = (TypedQuery<Funcionarios>) `enter code here`em.createQuery("select f from Funcionarios f where f.login = `enter code here`:login and f.senha = :senha");
return retorno.setParameter("login", `enter code here`login).setParameter("senha", senha).getSingleResult();
} catch (NoResultException e) {
return null;
}
}
豆
@Named(value = "huMBean")
@RequestScoped
public class HuMBean {
@EJB
private HUBean hUBean;
private Funcionarios func = new Funcionarios();
/**
* Creates a new instance of HuMBean
*/
public HuMBean() {
}
public String autenticarFuncionario() {
hUBean.loginFuncionario(func.getLogin(), func.getSenha());
return "logado";
}
public void setFunc(Funcionarios func) {
this.func = func;
}
public Funcionarios getFunc() {
return func;
}
我的xhtml调用这个bean
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Login</title>
</h:head>
<h:body>
<h:form>
<p:panelGrid columns="2">
<p:outputLabel for="login" value="Login:" />
<p:inputText id="login" value="#{huMBean.func.login}" />
<p:outputLabel for="senha" value="Senha:" />
<h:inputSecret id="senha" value="#{huMBean.func.senha}"/>
<p:commandButton value="Enviar" icon="ui-icon-star" action="#{huMBean.autenticarFuncionario}" ajax="false">
</p:commandButton>
</p:panelGrid>
</h:form>
</h:body>
</html>
我用变量和Beans改变了很多东西,比如@ManagedBeans,但是一直没有工作。