我嘗試編寫我的第一個JSF2.0項目(使用EJB3.1)。我不明白爲什麼我的@ManagedBean註釋不起作用。JSF2.0註釋@ManagedBean不起作用
我總是得到一個錯誤,當我在Glassfish v3上運行的應用程序
例外
javax.servlet.ServletException:/login.xhtml @ 34133 值=「#{} loginBean.login 「:目標不可達,識別符 'loginBean' 解析爲空
根源
javax.el.PropertyNotFoundException:/login.xh TML @ 34133 值= 「#{} 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(其中,i可以嘗試使用它):
<!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>
UserController類:
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
你能否以更多程序員的方式定義「不工作」,而不是用戶? – partlov 2013-03-12 15:21:58
哦,sry,我忘了描述一個錯誤:(更新後。 – beowulf13th 2013-03-12 15:38:15
@ beowulf13th你可以刪除控制器並重新創建嗎?你有同樣的錯誤嗎?我也懷疑容器問題可以嘗試tomcat嗎? – berkay 2013-03-12 16:13:43