具有共享代码的多个 JSF 项目的结构 [英] Structure for multiple JSF projects with shared code

查看:40
本文介绍了具有共享代码的多个 JSF 项目的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个共享大量代码的 JSF 项目 - java 类、xhtml 文件、标签库、css 和 javascript 文件等.我的开发环境/平台主要由 Eclipse、Ant、Perforce 和 Tomcat 组成.

I have two JSF projects that share a lot of code - java classes, xhtml files, tag libraries, css and javascript files etc. My dev environment/platform consists mainly of Eclipse, Ant, Perforce and Tomcat.

有没有人找到一种方法来创建和组织共享代码,以便公共代码可以保留在一组文件夹中?

Has anyone found a way to create and organize the shared code so that the common code can stay within one set of folders?

Eclipse 可以很容易地为 java 源添加外部文件夹,但在其他文件类型上存在不足.我很感激任何想法.

Eclipse makes it easy to add external folders for java sources, but falls short on the other file types. I'd appreciate any ideas.

推荐答案

创建一个新的Java 项目"在 Eclipse 中.将它作为另一个项目添加到主动态 Web 项目的 Deployment Assembly 属性中.这样,它会自动在 Web 项目构建的 /WEB-INF/lib 中作为 JAR 结束.由于较新的 Eclipse 版本,您还可以将项目创建为Web 片段项目".这样部署组装步骤将自动完成.

Create a new "Java Project" in Eclipse. Add it as another project to the Deployment Assembly property of the main dynamic web project. This way it will automatically end up as a JAR in /WEB-INF/lib of the build of the web project. Since newer Eclipse versions, you can also create the project as "Web Fragment Project". This way the Deployment Assembly step will be done automatically.

将所有共享资源文件放在Java项目的/META-INF/resources文件夹中.把它当作主 Web 项目的 WebContent/resources.标签文件可以保存在它们自己的 /META-INF/tags 文件夹中.

Put all those shared resource files in /META-INF/resources folder of the Java project. Just treat it like WebContent/resources of the main web project. Tagfiles can just be kept in their own /META-INF/tags folder.

例如

CommonWebProject
 |-- META-INF
 |    |-- resources
 |    |    `-- common
 |    |         |-- css
 |    |         |    `-- some.css
 |    |         |-- js
 |    |         |    `-- some.js
 |    |         |-- images
 |    |         |    `-- some.png
 |    |         |-- components
 |    |         |    `-- somecomposite.xhtml
 |    |         `-- sometemplate.xhtml
 |    |-- tags
 |    |    `-- sometag.xhtml
 |    |-- beans.xml
 |    |-- faces-config.xml
 |    |-- some.taglib.xml
 |    |-- web-fragment.xml
 |    `-- MANIFEST.MF
 :

<h:outputStylesheet library="common" name="css/some.css" />
<h:outputScript library="common" name="js/some.js" />
<h:graphicImage library="common" name="images/some.png" />
<common:somecomposite />
<common:sometag />
<ui:include src="/common/sometemplate.xhtml" />
...

如果您使用 Maven,/META-INF 文件夹必须放在 src/main/resources 中,因此不是 src/main/java!

In case you're using Maven, the /META-INF folder has to be placed in src/main/resources and thus NOT src/main/java!

如果您还想触发 JSF 注释扫描器,以便您可以输入 @FacesValidator@FacesConverter@FacesComponent@FacesRenderer 和 consorts 在该项目中,然后创建一个 /META-INF/faces-config.xml 文件.下面是一个 JSF 2.3 兼容的:

If you want to trigger the JSF annotation scanner as well so that you can put @FacesValidator, @FacesConverter, @FacesComponent, @FacesRenderer and consorts in that project as well, then create a /META-INF/faces-config.xml file as well. Below is a JSF 2.3 compatible one:

<?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_3.xsd"
    version="2.3">
    <!-- Put shared faces-config.xml config here. -->
</faces-config>

/META-INF/web-fragment.xml 是 servlet 容器将 JAR 识别为Web 片段项目"所必需的;并且应该已经由您的 IDE 生成,但为了完整起见,这里是 Servlet 4.0 兼容的样子:

The /META-INF/web-fragment.xml is mandatory for the JAR to be recognized by the servletcontainer as a "Web Fragment Project" and should already be generated by your IDE, but for sake of completeness here is how it should look like for a Servlet 4.0 compatible one:

<?xml version="1.0" encoding="utf-8"?>
<web-fragment
    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-fragment_4_0.xsd"
    version="4.0">
    <!-- Put shared web.xml config here. -->
</web-fragment>

仅此而已.

这篇关于具有共享代码的多个 JSF 项目的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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