我尝试编写我的第一个 JSF2.0 项目(使用 EJB3.1)。我不明白为什么我的 @ManagedBean 注释不起作用。
在 Glassfish v3 上运行应用程序时总是出错
例外
javax.servlet.ServletException: /login.xhtml @34,133 value="#{loginBean.login}": 目标无法到达,标识符“loginBean”解析为空
根本原因
javax.el.PropertyNotFoundException: /login.xhtml @34,133 value="#{loginBean.login}": 目标无法到达,标识符“loginBean”解析为空
如果我在 faces-config.xml 中定义一个托管 bean - 它会起作用。但我想使用注释。
可能是我在我的 poms 中使用了错误的库吗?
managedbean 示例(它将是一个传输对象):
package edu.tsystems.vmmail.web.core.domain;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import java.io.Serializable;
@ManagedBean
@ViewScoped
public class LoginBean implements Serializable {
private String login;
private String password;
public LoginBean() {}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
login.xhtml(我可以尝试使用它):
<!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://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<f:loadBundle var="common" basename="edu.tsystems.vmmail.web.ui.MessageResources" />
<h:head>
<title>Welcome to VMMail Web Interface</title>
<link type="text/css" href="#{request.contextPath}/css/style.css" rel="stylesheet" />
</h:head>
<h:body>
<f:view>
<h:form id="loginForm" method="post">
<p:panelGrid id="mainLogin" styleClass="noInnerBorderTable">
<f:facet name="header">
<p:row>
<p:column colspan="4">
<h:outputText value="#{common['login.welcome']}" /><br/>
<h:message for="loginBean" id="login1Error" />
</p:column>
</p:row>
</f:facet>
<p:row>
<p:column rowspan="2">
<div class="logoCell"></div>
</p:column>
<p:column>
<h:outputText value="#{common['field.login']}" for="loginBean" />
</p:column>
<p:column>
<p:inputText id="loginBean" required="true" value="#{loginBean.login}" requiredMessage="#{common['field.login.required']}" />
</p:column>
<p:column rowspan="2">
<div class="submitButtonCell">
<p:commandLink styleClass="loginAnchor" title="#{common['field.loginButton']}"
action="#{userController.loggingIn(login)}" ajax="false" />
</div>
</p:column>
</p:row>
<p:row>
<p:column>
<h:outputText for="password" value="#{common['field.password']}" />
</p:column>
<p:column>
<p:password id="password" required="true" value="#{loginBean.password}" requiredMessage="#{common['field.password.required']}" />
</p:column>
</p:row>
<f:facet name="footer">
<p:row>
<p:column colspan="4">
<h:outputText value="#{common['login.notHave']}" />
<a href="#{request.contextPath}/registration.xhtml">
<h:outputText value="#{common['login.registerNow']}" />
</a>
</p:column>
</p:row>
</f:facet>
</p:panelGrid>
</h:form>
</f:view>
</h:body>
</html>
用户控制器类:
package edu.tsystems.vmmail.web.core.controllers;
import edu.tsystems.vmmail.web.core.dao.UserDAO;
import edu.tsystems.vmmail.web.core.domain.LoginBean;
import edu.tsystems.vmmail.web.core.model.UserEntity;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
@Stateless
@ViewScoped
public class UserController {
@EJB
private UserDAO userDAO;
private UserEntity user;
public boolean isLoggedIn() {
return user != null;
}
public String loggingIn(LoginBean loginBean) {
FacesContext context = FacesContext.getCurrentInstance();
if(userDAO == null) {
context.addMessage("loginForm:login1Error", new FacesMessage("DAO IS NULL!"));
// return "/loginBean.xhtml?faces-redirect=true&error=1";
}
user = userDAO.getUserByLoginAndPassword(loginBean.getLogin(), loginBean.getPassword());
if (user != null) {
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
session.setAttribute("user", user.getId());
return "/mail/mail.xhtml?faces-redirect=true";
} else {
return "/loginBean.xhtml?faces-redirect=true";
}
}
public String logout() {
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
return "/login.xhmtl?faces-redirect=true";
}
}
我真的不明白为什么它不起作用:(我做错了什么?
UPD:堆栈跟踪: http: //pastebin.com/istJmmHr
源代码可以从我的谷歌驱动器下载:https ://docs.google.com/file/d/0B4Am7SXJwmtKNVc0LVhWVlEyMVk/view