JSF托管bean在jar中没有实例化 [英] JSF Managed bean in jar not instantiated

查看:129
本文介绍了JSF托管bean在jar中没有实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JSF中创建了两个jsf项目。其中一个是具有单个会话bean的基础项目。此基础项目打包到.jar文件(带有/META-INF/faces-config.xml文件)中,并包含在另一个项目(clientproj)中。问题是,当我运行客户端项目时,基础项目中的会话bean没有实例化,我得到一个NullPointerException。

I have created two jsf projects in JSF. One of them is a base project that has a single session bean. This base project is packaged into a .jar file (with a /META-INF/faces-config.xml file) and included in the other project (the clientproj). The problem is that when I run the client project, the session bean in the base project is not instantiated, and I get a NullPointerException.

详情如下:

基础项目 - 会话Bean

Base Project - Session Bean

package sbeans;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class SessionBean {

private String myName;

public String getMyName()
{
    return this.myName;
}

public void setMyName(String newName)
{
    this.myName = newName;
}

public String getResult()
{
    return "result";
}

}

META-INF / faces-此项目中的config.xml文件如下:

The META-INF/faces-config.xml file in this project is as follows:

<?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">

(META-INF文件夹在WebContent文件夹下。)

(The META-INF folder is under the WebContent folder).

通过右键单击项目 - > export - 将该项目导出为名为sbeans.jar的.jar文件(使用eclipse) - - > export - > jar文件。

This project is exported as a .jar file named sbeans.jar (using eclipse) by right clicking on th project --> export --> export --> jar file.

现在有一个客户端项目,其中sbeans.jar放在WEB-INF / lib文件夹中。客户端项目有一个bean,如下所示:

Now there is a client project where sbeans.jar is placed in the WEB-INF/lib folder. The client project has a single bean as follows:

package clientproj;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

import sbeans.SessionBean;

@ManagedBean
public class ClientBean {

@ManagedProperty("#{sessionBean}") private SessionBean sessionBean;

private String name;

public String getName()
{
    System.out.println("clientBean.getName() - this.sessionBean = " + this.sessionBean);
    this.name = this.sessionBean.getMyName();
    return this.name;
}

public void setName(String newName)
{
    this.name = newName;
    System.out.println("clientBean.setName() - this.sessionBean = " + this.sessionBean);
    this.sessionBean.setMyName(this.name);
}

public void setSessionBean(SessionBean sBean)
{
    this.sessionBean = sBean;
}

}

客户端项目有一个测试。 xhtml如下:

The client project has a test.xhtml as follows:

<!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">

<h:head>
<title>Some Title</title>
</h:head>

<h:body>

<h:form>
<h:outputLabel value="Enter Your Name" for="name"></h:outputLabel>
<br></br>
<h:inputText value="#{clientBean.name}"></h:inputText>
<h:commandButton value="Go" action="#{sessionBean.getResult}">        </h:commandButton>
</h:form>

</h:body>
</html>

当我尝试运行test.xhtml(在tomcat上,在eclipse中)时,我得到一个例外如下(可能是因为sessionBean为null(请参阅粗体文本)):

When I try to run test.xhtml (on tomcat, within eclipse), I get an exception as follows (presumably due the sessionBean being null (see text in bold)):

Feb 22, 2015 12:50:02 AM com.sun.faces.renderkit.html_basic.HtmlBasicRenderer getForComponent
WARNING: Unable to find component with ID name in view.
**clientBean.getName() - this.sessionBean = null**
Feb 22, 2015 12:50:02 AM   com.sun.faces.application.view.FaceletViewHandlingStrategy handleRenderException
SEVERE: Error Rendering View[/test.xhtml]
javax.el.ELException: /test.xhtml @18,41 value="#{clientBean.name}": Error   reading 'name' on type clientproj.ClientBean
at   com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:114)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
at javax.faces.component.UIOutput.getValue(UIOutput.java:174)
at javax.faces.component.UIInput.getValue(UIInput.java:291)
at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
at javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:894)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:443)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Caused by: javax.el.ELException: Error reading 'name' on type   clientproj.ClientBean
at javax.el.BeanELResolver.getValue(BeanELResolver.java:110)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
at org.apache.el.parser.AstValue.getValue(AstValue.java:183)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
... 40 more
Caused by: java.lang.NullPointerException
at clientproj.ClientBean.getName(ClientBean.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at javax.el.BeanELResolver.getValue(BeanELResolver.java:99)
... 45 more

Feb 22, 2015 12:50:02 AM org.apache.catalina.core.StandardWrapperValve   invoke
SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path     [/clientproj] threw exception [null] with root cause
java.lang.NullPointerException
at clientproj.ClientBean.getName(ClientBean.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at javax.el.BeanELResolver.getValue(BeanELResolver.java:99)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
at org.apache.el.parser.AstValue.getValue(AstValue.java:183)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
at javax.faces.component.UIOutput.getValue(UIOutput.java:174)
at javax.faces.component.UIInput.getValue(UIInput.java:291)
at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
at javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:894)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:443)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

BTW,客户端项目中还有一个简单的result.xhtml如下:

BTW, there is also a simple result.xhtml in the client project as follows:

<!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">
<h:head>
<title>Result</title></h:head>

<h:body>
<h1>Result</h1>
<h2>
<h:outputText value="#{clientBean.name}"></h:outputText>
</h2>
</h:body>
</html>

我需要这项工作的原始项目要比这复杂得多。我在这里发布的是我面临的问题的症结所在。我已经挣扎了好几个小时,并尝试了许多不同的方法来实现这个目标。任何帮助将不胜感激。

The original project where I need this working is a lot more complicated than this. What I have posted here is the crux of the issue I am facing. I have struggled with this for quite a few hours, and have tried many different methods to get this working. Any help will be much appreciated.

谢谢
Mahendra

Thanks Mahendra

推荐答案

看起来像Base项目 - 没有正确导出会话bean的项目。 JSF要求将 faces-config.xml 直接放在 META-中的 sbeans.jar 中INF 目录。但是,根据您的描述,Base项目是一个Web项目。当我创建这样的项目并使用你的方式导出它时,我得到了以下结构:

Looks like Base project - the one with session bean is not correctly exported. JSF requires faces-config.xml to be placed in sbeans.jar directly in META-INF directory. However, according to your description Base project is a web project. When I created such project and exported it using the way you did I got the following structure:

sbeans.jar
  |--META-INF (empty)
  |--WebContent
       |--META-INF
            |--faces-config.xml

如您所见 faces-config.xml 放在错误的位置。

As you see faces-config.xml is placed in wrong place.

要解决此问题:


  • 将项目类型更改为普通java - 无论如何将其导出为JAR不是WAR,不是吗? (看起来唯一的方法是从头开始创建新项目)。然后,您可以使用以下提示 https://stackoverflow.com/a/11027541/1071508 生成所有WAR只需点击几下鼠标即可实现依赖。

  • 如果无法更改项目类型,请考虑将项目转换为maven,以便利用插件。然后,您可以在创建期间重命名WAR:重命名WAR中的Maven依赖关系WEB-INF / lib文件夹

  • 如果由于某种原因无法使用先前的解决方案


    • create META-INF Java Resources / src 中并放置 faces-config.xml 那里

    • 或将项目导出为WAR,并在导出后将其重命名为 .jar

    • change project type to plain java - you anyway export it as JAR not WAR, don't you? (looks like the only way to do it is to create new project from scratch). Then you can then use the following hint https://stackoverflow.com/a/11027541/1071508 to generate WAR with all dependencies with only few mouse clicks.
    • if changing project type is not possible consider converting projects to maven so that you take advantage of plugins. Then you can rename the WAR during creation: Renaming Maven dependency in WAR's WEB-INF/lib folder
    • if previous solution is not possible for some reason
      • create META-INF in Java Resources/src and place faces-config.xml there
      • or export project as WAR and rename it to .jar after exporting

      这篇关于JSF托管bean在jar中没有实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆