如何引用 JAR 文件中提供的 JSF 托管 bean? [英] How to reference JSF managed beans which are provided in a JAR file?

查看:19
本文介绍了如何引用 JAR 文件中提供的 JSF 托管 bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构如下的 WAR 文件:

I have a WAR file with the following structure:

JSF 托管 bean BusinessObjectTypeListController 位于 /WEB-INF/lib 中的 commons-web-1.0.jar 并在 /WEB-INF/lib 中引用代码>BusinessObjectTypeListView.xhtml.当我运行 Web 应用程序并调用该视图时,出现以下错误:

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:

javax.servlet.ServletException:/view/common/businessObjectTypeListView.xhtml @34,94 listener="#{businessObjectTypeListController.selectData}": 目标不可达,标识符businessObjectTypeListController"解析为空

javax.servlet.ServletException: /view/common/businessObjectTypeListView.xhtml @34,94 listener="#{businessObjectTypeListController.selectData}": Target Unreachable, identifier 'businessObjectTypeListController' resolved to null

为什么找不到控制器类?它应该在类路径中,是吗?

Why isn't the controller class found? It should be in the classpath, is it?

推荐答案

您需要在 commons- 中有一个符合 JSF 2.0 的 /META-INF/faces-config.xml 文件web-1.0.jar 文件,以便让 JSF 扫描 JAR 文件中带有 JSF 注释的类,例如 @ManagedBean 并自动注册它们.

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.

<?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 即不会扫描类路径中每个 JAR 文件的每个类,这太昂贵了.只会扫描具有上述 /META-INF/faces-config.xml 文件的 JAR.

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.

您还应该确保您没有中有metadata-complete=true"属性> webapp自己的/WEB-INF/faces-config.xml文件的声明,否则JSF会认为这个faces配置是完整的,因此不会自动扫描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 中手动将 bean 注册为 <managed-bean>-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.

另请参阅 JSF 2.0 规范的第 11.4.2 章(重点我的).

See also chapter 11.4.2 of JSF 2.0 specification (emphasis mine).

...

该算法为正在组装基于 JSF 的 Web 组件的开发人员提供了相当大的灵活性应用.例如,一个应用程序可能包含一个或多个自定义 UIComponent 实现,以及关联的渲染器,因此它可以在名为/WEB-INF/faces-config.xml"的应用程序资源中声明它们无需以编程方式将它们注册到 Application 实例.此外,应用程序可能会选择包含一个包含META-INF/faces-config.xml"资源的组件库(打包为 JAR 文件).此资源的存在导致组件、渲染器和其他 JSF 实现类存储在此自动注册库 JAR 文件,应用程序无需执行任何操作.

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.

这篇关于如何引用 JAR 文件中提供的 JSF 托管 bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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