JBoss 7.1(JBAS018210)无法加载带有Managed Bean的简单JSF

时间:2013-10-31 04:35:42

标签: eclipse jsf jboss

当我尝试通过Eclipse做一个非常简单的JSF应用程序时,我的JBoss日志中出现了一个奇怪的错误。所以我创建了一个简单的场景来询问,如果我在JSF示例中遗漏了一些基本内容。或者,我怀疑在我的特定工具或环境中可能存在错误配置或错误。

我创建了一个非常简单的JSF应用程序。它只是一个JSF html页面(hello.jsp),它通过ManagedBean收集名称并转换到第二个JSF html页面(ok.jsp)。该bean有1个getter,1个setter和1个action方法(addContact)。

我没有使用注释,只是一个简单的faces-config.xml文件,其中包含一个ManagedBean条目和一个导航规则。 bean名为“contact”,类为“com.mbeans.ContactBean”。我认为映射是正确的。

我通过Eclipse Juno SR2使用JBoss 7.1。这是一个JSF 2.0应用程序。我已经为Juno加载了最新的JBoss Tools 4.0。

该应用仅包含以下文件:

web.xml
faces-config.xml
ContactBean.java
hello.jsp
ok.jsp

文件内容如下。但是我会添加一些颜色评论,说明它们是如何通过Eclipse创建的(你可能想要浏览或忽略它们):

我进入Eclipse并创建一个新项目。我使用以下设置选择“Dynamic Web Project”:

  

目标运行时= JBoss 7.1运行时

     

配置= JBoss 7.1运行时的动态配置

     

生成web.xml = CHECKED

我右键单击WebContent并创建一个新的JSP文件(Filename = hello.jsp)。我使用JSP Template = New JSF Page(html)。

我添加了一些代表访问名为“contact”的ManagedBean的简单表单的行,以便hello.jsp看起来像这样:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Add Contact</title>
</head>
<body>
    <f:view>
        <h2>Add Contact</h2>
        <h:form>
            Name:<br>
            <h:inputText value="#{contact.name}" />
            <h:commandButton value="Add Contact"
                action="#{contact.addContact}" type="submit" />
        </h:form>
    </f:view>
</body>
</html>

同样,我添加了以下JSP文件(ok.jsp):

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>OK - Added New Contact</title>
</head>
<body>
    <f:view>
        <p>
            The address for
            <h:outputText value="#{contact.name}" />
            was successfully added.
        </p>
    </f:view>
</body>
</html>

我右键单击项目(JSFTest)并添加一个具有以下设置的类:

  

Package = com.mbeans

     

Name = ContactBean

我编辑的类看起来像这样:

package com.mbeans;

public class ContactBean {

    public ContactBean() {super();}

    String name;

    public String getName() {return name;}

    public void setName(String name) {this.name = name;}

    public String addContact() {
        System.out.println("Name: " + getName());
        return "success";
    }  
}

然后我右键单击WEB-INF文件夹并使用以下设置选择New->Other->JBoss Tools Web->JSF->Faces Config

  

文件夹:* = / JSFTest

     

名称:* = faces-config

     

版本:2.0

     

在web.xml中注册= CHECKED

在创建的新faces-config.xml中,我右键单击Managed Beans->New->Managed Bean...并设置以下内容:

  

范围=申请

     

Class:* = com.mbeans.ContactBean

     

姓名:* = contactBean

然后我右键点击Navigation Rules->New->Rule ...并将对话框留空并点击Finish

我右键点击"[any]"->New->Case...并设置以下内容:

  

从结果:=成功

     

来自行动:=#{contact.addContact}

     

ToView ID:* = /ok.jsp

最后,在WEB-INF文件夹中生成以下faces-config.xml文件:

<?xml version="1.0"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xi="http://www.w3.org/2001/XInclude"
 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/web-facesconfig_2_0.xsd">
 <managed-bean>
  <managed-bean-name>contact</managed-bean-name>
  <managed-bean-class>com.mbeans.ContactBean</managed-bean-class>
  <managed-bean-scope>application</managed-bean-scope>
 </managed-bean>
 <navigation-rule>
  <navigation-case>
   <from-action>#{contact.addContact}</from-action>
   <from-outcome>success</from-outcome>
   <to-view-id>/ok.jsp</to-view-id>
  </navigation-case>
 </navigation-rule>
</faces-config>

然后我右键单击hello.jsp并选择Run As->Run On Server

我选择了JBoss 7.1运行时服务器(它是唯一的)。服务器启动成功。 (我知道这个JBoss服务器在Eclipse之外正确运行其他东西。)

我收到500错误,特别是,我在日志中看到以下消息,我认为这是问题的根源:

23:33:08,612 ERROR [org.jboss.web] (MSC service thread 1-3) JBAS018211: Could not load JSF managed bean class: com.mbeans.ContactBean

我认为NullPointerException可能是由我做错的其他事情引起的,与上面的错误信息无关。我最担心的是上述错误。但是,如果任何人都可以指出我的错误清除了这个NullPointerException,我会很感激,所以我可以消除它。以前我在没有NPE的情况下得到了不同的500错误,但在日志中仍然有JBAS018211。换句话说,我认为NPE很烦人但不相关。

完整的日志是:

23:33:06,543 INFO  [org.jboss.modules] JBoss Modules version 1.1.1.GA
23:33:06,761 INFO  [org.jboss.msc] JBoss MSC version 1.0.2.GA
23:33:06,824 INFO  [org.jboss.as] JBAS015899: JBoss AS 7.1.1.Final "Brontes" starting
23:33:07,636 INFO  [org.xnio] XNIO Version 3.0.3.GA
23:33:07,638 INFO  [org.jboss.as.server] JBAS015888: Creating http management service using socket-binding (management-http)
23:33:07,647 INFO  [org.xnio.nio] XNIO NIO Implementation Version 3.0.3.GA
23:33:07,655 INFO  [org.jboss.remoting] JBoss Remoting version 3.2.3.GA
23:33:07,678 INFO  [org.jboss.as.logging] JBAS011502: Removing bootstrap log handlers
23:33:07,686 INFO  [org.jboss.as.configadmin] (ServerService Thread Pool -- 26) JBAS016200: Activating ConfigAdmin Subsystem
23:33:07,689 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 31) JBAS010280: Activating Infinispan subsystem.
23:33:07,717 INFO  [org.jboss.as.security] (ServerService Thread Pool -- 44) JBAS013101: Activating Security Subsystem
23:33:07,725 INFO  [org.jboss.as.naming] (ServerService Thread Pool -- 38) JBAS011800: Activating Naming Subsystem
23:33:07,749 INFO  [org.jboss.as.security] (MSC service thread 1-10) JBAS013100: Current PicketBox version=4.0.7.Final
23:33:07,749 INFO  [org.jboss.as.webservices] (ServerService Thread Pool -- 48) JBAS015537: Activating WebServices Extension
23:33:07,752 INFO  [org.jboss.as.osgi] (ServerService Thread Pool -- 39) JBAS011940: Activating OSGi Subsystem
23:33:07,789 INFO  [org.jboss.as.connector] (MSC service thread 1-7) JBAS010408: Starting JCA Subsystem (JBoss IronJacamar 1.0.9.Final)
23:33:07,797 INFO  [org.jboss.as.naming] (MSC service thread 1-4) JBAS011802: Starting Naming Service
23:33:07,825 INFO  [org.jboss.as.mail.extension] (MSC service thread 1-12) JBAS015400: Bound mail session [java:jboss/mail/Default]
23:33:07,844 INFO  [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 27) JBAS010403: Deploying JDBC-compliant driver class org.h2.Driver (version 1.3)
23:33:07,971 INFO  [org.jboss.ws.common.management.AbstractServerConfig] (MSC service thread 1-7) JBoss Web Services - Stack CXF Server 4.0.2.GA
23:33:07,982 INFO  [org.apache.coyote.http11.Http11Protocol] (MSC service thread 1-5) Starting Coyote HTTP/1.1 on http--127.0.0.1-7070
23:33:08,341 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-1) JBAS010400: Bound data source [java:jboss/datasources/ExampleDS]
23:33:08,352 INFO  [org.jboss.as.server.deployment.scanner] (MSC service thread 1-4) JBAS015012: Started FileSystemDeploymentService for directory /usr/local/jboss-as-7.1.1.Final/standalone/deployments
23:33:08,358 INFO  [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) JBAS015003: Found JSFTest.war in deployment directory. To trigger deployment create a file called JSFTest.war.dodeploy
23:33:08,360 INFO  [org.jboss.as.remoting] (MSC service thread 1-10) JBAS017100: Listening on /127.0.0.1:9999
23:33:08,362 INFO  [org.jboss.as.remoting] (MSC service thread 1-3) JBAS017100: Listening on /127.0.0.1:4447
23:33:08,475 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-12) JBAS015876: Starting deployment of "JSFTest.war"
23:33:08,612 ERROR [org.jboss.web] (MSC service thread 1-3) JBAS018211: Could not load JSF managed bean class: com.mbeans.ContactBean
23:33:08,792 INFO  [javax.enterprise.resource.webcontainer.jsf.config] (MSC service thread 1-10) Initializing Mojarra 2.1.7-jbossorg-1 (20120227-1401) for context '/JSFTest'
23:33:09,663 INFO  [org.hibernate.validator.util.Version] (MSC service thread 1-10) Hibernate Validator 4.2.0.Final
23:33:09,805 INFO  [org.jboss.web] (MSC service thread 1-10) JBAS018210: Registering web context: /JSFTest
23:33:09,818 INFO  [org.jboss.as] (MSC service thread 1-2) JBAS015951: Admin console listening on http://127.0.0.1:9990
23:33:09,819 INFO  [org.jboss.as] (MSC service thread 1-2) JBAS015874: JBoss AS 7.1.1.Final "Brontes" started in 3541ms - Started 172 of 249 services (76 services are passive or on-demand)
23:33:09,873 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "JSFTest.war"
23:33:29,683 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/JSFTest].[jsp]] (http--127.0.0.1-7070-1) Servlet.service() for servlet jsp threw exception: java.lang.NullPointerException
    at javax.faces.webapp.UIComponentClassicTagBase.setJspId(UIComponentClassicTagBase.java:1858) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
    at org.apache.jsp.hello_jsp._jspx_meth_f_005fview_005f0(hello_jsp.java:103)
    at org.apache.jsp.hello_jsp._jspService(hello_jsp.java:78)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) [jbossweb-7.0.13.Final.jar:]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369) [jbossweb-7.0.13.Final.jar:]
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:326) [jbossweb-7.0.13.Final.jar:]
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:253) [jbossweb-7.0.13.Final.jar:]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
    at java.lang.Thread.run(Unknown Source) [rt.jar:1.7.0_17]

就是这样。这就是问题所在。

(另外,Eclipse也没有显示JSF的悬停标记描述.HTML标记没问题。这让我想知道是否存在一些更深层次的JBoss Tools配置错误。)

感谢您花时间查看我的问题。

1 个答案:

答案 0 :(得分:2)

除了您所做的之外,请尝试执行以下操作:

  • 右键单击项目→“属性”→“项目构面”
  • 使用正确的版本添加Java Server Faces构面(您的版本为2.0)
  • 单击底部的其他配置链接,选择“服务器运行时提供的库”(默认),然后选中配置web.xml的选项。在/faces/*处映射Faces Servlet(为了这个答案;您可以稍后更改它,但请记住相应地更改URL)
  • (可选,但首先尝试这种方式)我更喜欢通过打开服务器选项卡来部署我的应用程序,右键单击服务器→添加/删除...→选择您的应用程序并部署
  • 成功部署应用程序后(检查控制台)获取URL:http://localhost:8080/JSFTest/faces/hello.jsp

还记得Faces Servlet映射 - /faces/*吗?您必须将其包含在URL中。

这适合我。

顺便说一下,优先选择Facelets(XHTML选项)而不是JSP。