简体   繁体   English

如何引用JAR文件中提供的JSF托管bean?

[英]How to reference JSF managed beans which are provided in a JAR file?

I have a WAR file with the following structure: 我有一个具有以下结构的WAR文件:

在此输入图像描述

The JSF managed bean BusinessObjectTypeListController is located in commons-web-1.0.jar in /WEB-INF/lib and referenced in BusinessObjectTypeListView.xhtml . JSF托管bean BusinessObjectTypeListController位于/WEB-INF/lib中的commons-web-1.0.jar中,并在BusinessObjectTypeListView.xhtml引用。 When I run my web application and I call that view, I get the following error: 当我运行我的Web应用程序并调用该视图时,出现以下错误:

javax.servlet.ServletException: /view/common/businessObjectTypeListView.xhtml @34,94 listener="#{businessObjectTypeListController.selectData}": Target Unreachable, identifier 'businessObjectTypeListController' resolved to null javax.servlet.ServletException:/view/common/businessObjectTypeListView.xhtml @ 34,94 listener =“#{businessObjectTypeListController.selectData}”:目标无法访问,标识符'businessObjectTypeListController'已解析为null

Why isn't the controller class found? 为什么找不到控制器类? It should be in the classpath, is it? 它应该在classpath中,是吗?

You need to have a JSF 2.0 compliant /META-INF/faces-config.xml file in the commons-web-1.0.jar file in order to get JSF to scan the JAR file for classes with JSF annotations like @ManagedBean and auto-register them. 您需要在commons-web-1.0.jar文件中具有符合JSF 2.0的/META-INF/faces-config.xml文件,以便让JSF扫描JAR文件以获取具有JSF注释的类,如@ManagedBean和auto-注册他们。

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    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"
    version="2.0">
</faces-config>

JSF does namely not scan every class of every single JAR file in the classpath, that would have been too expensive. JSF确实没有扫描类路径中每个JAR文件的每个类,这本来太贵了。 Only JARs with the above /META-INF/faces-config.xml file will be scanned. 仅扫描具有上述/META-INF/faces-config.xml文件的JAR。

You should also ensure that you do not have the metadata-complete="true" attribute in the <faces-config> declaration of webapp's own /WEB-INF/faces-config.xml file, otherwise JSF will assume that this faces config is complete and therefore won't auto-scan JAR files for annotations. 您还应该确保在webapp自己的/WEB-INF/faces-config.xml文件的<faces-config>声明中没有 metadata-complete="true"属性,否则JSF会认为这面临的配置是完成,因此不会自动扫描JAR文件以获取注释。

If none of those conditions are (or can be) met, then you need to manually register the bean as <managed-bean> in webapp's own /WEB-INF/faces-config.xml instead of relying on annotations. 如果这些条件都不满足(或者可以满足),那么您需要在webapp自己的/WEB-INF/faces-config.xml手动将bean注册为<managed-bean> ,而不是依赖于注释。

See also chapter 11.4.2 of JSF 2.0 specification (emphasis mine). 另见JSF 2.0规范的第11.4.2章(强调我的)。

11.4.2 Application Startup Behavior 11.4.2应用程序启动行为

... ...

This algorithm provides considerable flexibility for developers that are assembling the components of a JSF-based web application. 该算法为组装基于JSF的Web应用程序的组件的开发人员提供了相当大的灵活性。 For example, an application might include one or more custom UIComponent implementations, along with associated Renderers, so it can declare them in an application resource named “/WEB-INF/faces-config.xml” with no need to programmatically register them with Application instance. 例如,应用程序可能包含一个或多个自定义UIComponent实现以及关联的Renderers,因此它可以在名为“/WEB-INF/faces-config.xml”的应用程序资源中声明它们,而无需以编程方式将它们与Application一起注册。实例。 In addition, the application might choose to include a component library (packaged as a JAR file) that includes a “META-INF/faces-config.xml” resource. 此外,应用程序可能会选择包含一个包含“META-INF / faces-config.xml”资源的组件库(打包为JAR文件)。 The existence of this resource causes components, renderers, and other JSF implementation classes that are stored in this library JAR file to be automatically registered, with no action required by the application. 此资源的存在会导致存储在此库JAR文件中的组件,呈现器和其他JSF实现类自动注册,而不需要应用程序执行任何操作。

I have same problem with CDI beans in my case. 我的CDI bean也有同样的问题。

I have common.jar project where i placed the CDI beans. 我有common.jar项目,我放置了CDI bean。 (without beans.xml) and I have webapp.war that contains common.jar in it`s lib and beans.xml. (没有beans.xml),我的webapp.war包含了它的lib和beans.xml中的common.jar。

when i call a cdi bean from jsf, i get it is not reachable exception:/ 当我从jsf调用一个cdi bean时,我得到它是不可达的异常:/

project structure is created using maven : - maven-archetype-quickstart for common.jar - maven-archetype-webapp for webapp.war 项目结构使用maven创建: - maven-archetype-quickstart for common.jar - maven-archetype-webapp for webapp.war

I am using eclipse/juno en deploy to Glassfish 3.1.x. 我正在使用eclipse / juno en部署到Glassfish 3.1.x.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Resolved: For EJB and JAR packaging you should place the beans.xml in src/main/resources/META-INF/. 已解决:对于EJB和JAR打包,您应将beans.xml放在src / main / resources / META-INF /中。 For WAR packaging you should place the beans.xml in src/main/webapp/WEB-INF/. 对于WAR打包,您应该将beans.xml放在src / main / webapp / WEB-INF /中。 Remember that only .java files should be put in the src/main/java and src/test/java directories. 请记住,只有.java文件应放在src / main / java和src / test / java目录中。 Resources like .xml files should be in src/main/resources. 像.xml文件这样的资源应该在src / main / resources中。

from topic: CDI: beans.xml, where do I put you? 来自主题: CDI:beans.xml,我在哪里放你?

In my opinion the class BusinessObjectTypeListController is founded properly but does not instantiated. 在我看来,类BusinessObjectTypeListController是正确建立的,但没有实例化。

How you create the instance of class on a view? 如何在视图上创建类的实例? If you use a BeanFactory review the config xml files 如果您使用BeanFactory查看config xml文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM