JSF Facelets 模板打包

发布于 2025-01-05 11:01:52 字数 825 浏览 0 评论 0原文

一如既往,我有点困惑。

在这里 https://community.jboss.org/wiki/ModularWebAppsWithJSF2 我了解到它自 JSF 2.0 以来,它很简单,并且开箱即用,可将模板捆绑在单独的 jar 中。

唯一的问题是:我无法让它工作。我只需在 Web 应用程序 WEB-INF/lib 中包含的 jar 中部署所有风格的“page.xhtml”(META-INF 目录、资源目录、根;有或没有 faces-config.xml)并请求一些内容像 http://host/demo/faces/page.xhtml 或执行“包含”或“装饰”模板。我有一个例外。

这里 Java EE6>将 JSF Facelets (xhtml) 和 ManagedBeans 打包为 JAR 我最喜欢的 JSF 老师解释说,要使用自定义的 ResourceResolver 来完成此任务。当我调试资源解析时,我毫不怀疑这会起作用并且会尝试一下。

这是关于机制的问题——两种方法有什么区别?

自动在 META-INF/resources 中查找哪些资源?

As always, i'm a little confused.

Here https://community.jboss.org/wiki/ModularWebAppsWithJSF2 i've learned that it is easy and works out of the box to bundle templates in separate jars since JSF 2.0.

The only problem is: i can't get it working. I simply deploy a "page.xhtml" in all flavors (META-INF directory, resources directory, root; with and without faces-config.xml) in a jar that is included in the web application WEB-INF/lib and request something like http://host/demo/faces/page.xhtml or do an "include" or "decorate" on the template. I get an exception.

Here Java EE6> Packaging JSF facelets (xhtml) and ManagedBeans as JAR my favorite JSF teacher explains to use a custom ResourceResolver to do exactly this. As i debugged the resource resolving i have no doubt that this will work and will give it a try.

This is the question about the mechanics - what is the difference between the two approaches?

Which resources exactly are looked up in META-INF/resources automatically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扛起拖把扫天下 2025-01-12 11:01:52

Facelets 组合(因此,只是简单的 *.xhtml 页面、模板和包含文件)由 ExternalContext#getResource() 其中委托给 ServletContext#getResource()。这需要 Servlet 3.x 兼容的容器,因为 /WEB-INF/lib/*.jar!/META-INF/resources 解析自 Servlet 3.0 以来是新的。如果您还没有使用 Servlet 3.x,或者出于某种原因想要将这些 JAR 放在不同的位置,那么您需要创建一个自定义 ResourceResolver。另请参阅如何创建模块化 JSF 2.0 应用程序?

Facelets 复合组件和静态资源(因此, 组件和 CSS/JS/图像资源将由 加载)已解析从类路径 ClassLoader#getResource()。要将 JAR 文件包含在 JSF 的类路径扫描中,您需要在 /META-INF 中包含一个与 JSF 2.x 兼容的 faces-config.xml 文件JAR 文件的文件夹。同样的情况也适用于 @ManagedBean@FacesValidator@FacesConverter@FacesComponent 和其他 JSF 工件。


在Eclipse中开发时,可以选择Web > Web Fragment Project 用于创建这样的模块项目。它与普通的 Java 项目没有太大区别,期望它将隐式包含 JavaScript 方面和目标运行时,自动创建 /META-INF/web-fragment.xml文件并通过将其自身作为部署程序集添加到该项目来与现有的动态 Web 项目关联。

您还可以使用已准备好正确文件夹结构的现有标准Java 项目/META-INF 文件夹必须位于 Java 源文件夹中。顺便说一句,web-fragment.xml 文件是可选的。您只需手动将 Java 项目添加到主 Web 项目属性的部署程序集部分。请勿不要将其添加为项目的构建路径部分中的另一个项目。

当您(手动)从中构建 JAR 文件时,需要确保将目录条目添加到 JAR 中,否则无法解析 Facelets 组合。如果您使用 Eclipse/Ant/Maven/等构建工具进行构建,则还必须考虑到这一点。如果这是无法控制的,那么自定义 ResourceResolver 是最可靠的方法。

Facelets compositions (so, just plain *.xhtml pages, templates and include files) are resolved by ExternalContext#getResource() which delegates to ServletContext#getResource(). This requires a Servlet 3.x compatible container because /WEB-INF/lib/*.jar!/META-INF/resources resolving from is new since Servlet 3.0. If you aren't on Servlet 3.x yet, or want to put those JARs on a different location for some reason, then you'd need to create a custom ResourceResolver. See also How to create a modular JSF 2.0 application?

Facelets composite components and static resources (so, <cc:xxx> components and CSS/JS/image resources which are to be loaded by <h:outputStylesheet>, <h:outputScript> and <h:graphicImage>) are resolved from the classpath by ClassLoader#getResource(). To include the JAR file in the classpath scan of JSF, you'd need to include a JSF 2.x compatible faces-config.xml file in the /META-INF folder of the JAR file. The same story applies to @ManagedBean, @FacesValidator, @FacesConverter, @FacesComponent and other JSF artifacts.


When developing in Eclipse, you can choose Web > Web Fragment Project to create such a module project. It is not much different from a normal Java project, expect that it will implicitly include JavaScript facet and a targeted runtime, autocreate a /META-INF/web-fragment.xml file and get associated with an existing Dynamic Web Project by adding itself as a deployment assembly to that project.

You can also use an existing standard Java project with the right folder structure prepared. The /META-INF folder has to go in Java source folder. The web-fragment.xml file is by the way optional. You just have to manually add the Java project to the Deployment Assembly section of the main web project properties. Do not add it as another project in project's Build Path section.

When you're (manually) building a JAR file out of it, you need to make sure that the directory entries are added to the JAR, otherwise Facelets compositions can't be resolved. If you're building by build tools like Eclipse/Ant/Maven/etc, this has also to be taken into account. If this is not controllable, a custom ResourceResolver is the most reliable approach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文