java 如何引用 JAR 文件中提供的 JSF 托管 bean?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7663818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to reference JSF managed beans which are provided in a JAR file?
提问by Francesco
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
. When I run my web application and I call that view, I get the following error:
在JSF托管beanBusinessObjectTypeListController
位于commons-web-1.0.jar
中/WEB-INF/lib
和引用的BusinessObjectTypeListView.xhtml
。当我运行 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”解析为空
Why isn't the controller class found? It should be in the classpath, is it?
为什么找不到控制器类?它应该在类路径中,是吗?
回答by BalusC
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.
您需要在/META-INF/faces-config.xml
文件中有一个符合 JSF 2.0的commons-web-1.0.jar
文件,以便让 JSF 扫描 JAR 文件中带有 JSF 注释的类,@ManagedBean
并自动注册它们。
<?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. Only JARs with the above /META-INF/faces-config.xml
file will be scanned.
JSF 即不会扫描类路径中每个 JAR 文件的每个类,这太昂贵了。只会/META-INF/faces-config.xml
扫描具有上述文件的JAR 。
You should also ensure that you do nothave 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 自己的文件的声明中没有该metadata-complete="true"
属性,否则 JSF 将假定此faces 配置是完整的,因此不会自动扫描 JAR 文件以获取注释。<faces-config>
/WEB-INF/faces-config.xml
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.
如果这些条件都不满足(或不能满足),那么您需要像<managed-bean>
在 webapp 自己的一样手动注册 bean,/WEB-INF/faces-config.xml
而不是依赖注释。
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
...
This algorithm provides considerable flexibility for developers that are assembling the components of a JSF-based web application. 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. 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. 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.
11.4.2 应用程序启动行为
...
该算法为组装基于 JSF 的 Web 应用程序组件的开发人员提供了相当大的灵活性。例如,一个应用程序可能包含一个或多个自定义 UIComponent 实现以及关联的渲染器,因此它可以在名为“/WEB-INF/faces-config.xml”的应用程序资源中声明它们,而无需以编程方式将它们注册到 Application实例。此外,应用程序可能会选择包含一个包含“META-INF/faces-config.xml”资源的组件库(打包为 JAR 文件)。 此资源的存在会导致存储在此库 JAR 文件中的组件、渲染器和其他 JSF 实现类自动注册,应用程序无需执行任何操作。
回答by Yakari
I have same problem with CDI beans in my case.
在我的情况下,CDI bean 也有同样的问题。
I have common.jar project where i placed the CDI beans. (without beans.xml) and I have webapp.war that contains common.jar in it`s lib and beans.xml.
我有放置 CDI bean 的 common.jar 项目。(没有 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 创建的: - 用于 common.jar 的 maven-archetype-quickstart - 用于 webapp.war 的 maven-archetype-webapp
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/. For WAR packaging you should place the beans.xml in src/main/webapp/WEB-INF/. Remember that only .java files should be put in the src/main/java and src/test/java directories. Resources like .xml files should be in src/main/resources.
已解决:对于 EJB 和 JAR 打包,您应该将 beans.xml 放在 src/main/resources/META-INF/ 中。对于 WAR 打包,您应该将 beans.xml 放在 src/main/webapp/WEB-INF/ 中。请记住,只应将 .java 文件放在 src/main/java 和 src/test/java 目录中。像 .xml 文件这样的资源应该在 src/main/resources 中。
from topic: CDI: beans.xml, where do I put you?
来自主题: CDI:beans.xml,我把你放在哪里?
回答by linkamp
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 查看配置 xml 文件