【发布时间】:2016-04-21 15:59:03
【问题描述】:
首先,我想让您知道,我已经在 SO 上浏览相同的问题很长时间了,但没有任何帮助。我有一个项目,我想让AuthBean 处理身份验证,但是我不断收到带有已知错误消息的 HTTP 500 错误
javax.servlet.ServletException: /login.xhtml @14,45 value="#{authBean.login}": Target Unreachable, identifier 'authBean' resolved to null
我使用 GlassFish 4 服务器作为 Java EE 运行时环境。
到目前为止,我已经尝试过
- 重新启动 GlassFish 服务器,从服务器中删除应用程序并重新部署。
- 在
javax.faces.bean.SessionScoped和javax.enterprise.context.SessionScoped之间切换。 - 检查
AuthBean.class是否在class文件夹中。 - 明确命名我的 bean(如下所示)
这是我的文件。任何帮助表示赞赏。
AuthBean.java
package pis.back;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import pis.data.Person;
import pis.service.PersonManager;
@ManagedBean(name = "authBean")
@SessionScoped
public class AuthBean {
private boolean loggedIn;
private String login;
private String password;
@EJB
private PersonManager personMgr;
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;
}
/*public boolean isLoggedIn() {
return this.loggedIn;
}*/
public String performLogin() {
if (loggedIn) {
return "error";
}
Person person = personMgr.findByLogin(this.login);
if (person == null || !this.password.equals(person.getPassword())) {
return "error";
}
loggedIn = true;
return "ok";
}
public String performLogout() {
if (!loggedIn) {
return "error";
}
loggedIn = false;
return "ok";
}
}
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
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/web-facesconfig_2_2.xsd"
version="2.2">
</faces-config>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>pis</display-name>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.xhtml</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<filter>
<filter-name>AuthFilter</filter-name>
<filter-class>pis.back.AuthFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AuthFilter</filter-name>
<url-pattern>*.xhtml</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
</web-app>
glassfish-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app>
<context-root>/pis</context-root>
</glassfish-web-app>
login.xhtml
<?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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<ui:composition template="template.xhtml">
<ui:define name="title">Login</ui:define>
<ui:define name="content">
<h:form>
<h:panelGrid columns="2">
<h:outputLabel value="Login: "/>
<h:inputText value="#{authBean.login}"/>
<h:outputLabel value="Password: "/>
<h:inputSecret value="#{authBean.password}"/>
<h:commandButton action="#{authBean.performLogin}" value="Login"/>
</h:panelGrid>
</h:form>
</ui:define>
</ui:composition>
</html>
template.xhtml
<?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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title><ui:insert name="title"/></title>
</h:head>
<h:body>
<div id="header">
Here comes header
</div>
<div id="menu">
Here comes menu
</div>
<div id="content">
<h2><ui:insert name="title"/></h2>
<ui:insert name="content"/>
</div>
<div id="footer">
Here comes footer
</div>
</h:body>
</html>
【问题讨论】:
-
当您构建/导出 WAR 时,看起来一切正常吗?另见stackoverflow.com/q/30128395的第 1c 点
-
我看过这张票,查了WAR的内容。
AuthBean.class位于WEB-INF/classes/pis/back,我想应该是这样的。 -
而
WEB-INF/lib没有服务器提供的库? (Java EE、JSF 等) -
啊 GF 陈旧文件。当然要清理它的工作文件夹。如有必要,扔掉它并重新解压缩。
-
嗯,这已在 1c 中的 “确保您已正确执行项目和服务器的完全清理、重建、重新部署和重新启动” 短语中涵盖。我猜你使用的是一个糟糕的 IDE 服务器插件(已知 GF Eclipse 插件在正确清理 GF 工作文件夹方面存在问题)
标签: jsf jakarta-ee glassfish