36

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 makes it easy to add external folders for java sources, but falls short on the other file types. I'd appreciate any ideas.

1 Answer 1

67

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.

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.

E.g.

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
 :

with

<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" />
...

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

If you want to trigger the Faces 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 Faces 4.0 compatible one:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="https://jakarta.ee/xml/ns/jakartaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-facesconfig_4_0.xsd"
    version="4.0">
    <!-- Put shared faces-config.xml config here. -->
</faces-config>

The /META-INF/web-fragment.xml file is mandatory for the JAR to be recognized by the servletcontainer as a "Web Fragment Project" so that it will automatically search and register any @WebServlet, @WebFilter and @WebListener classes as well. It should already be generated by your IDE, but for sake of completeness here is how it should look like for a Servlet 6.0 compatible one:

<?xml version="1.0" encoding="utf-8"?>
<web-fragment 
    xmlns="https://jakarta.ee/xml/ns/jakartaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-fragment_6_0.xsd"
    version="6.0">
    <!-- Put shared web.xml config here. -->
</web-fragment>

That's all.

Real world examples can be found in OmniFaces and OptimusFaces projects.

See also:

Sign up to request clarification or add additional context in comments.

14 Comments

Many thanks! I'm also honored by your response having frequented your blogs on a number of JSF issues on many occasions!
@Heizenberg, if he helped, mark the reply as an answer and vote it up by clicking the up arrow on the side. Its the stack exchange way of saying thanks :)
Thanks Fredrik. I marked it as an answer with the "tick" mark. I now need to "build a reputation" so I can vote it up (needs "15 reputation" to vote up)
@BalusC: Is this different if I'm using JSF 1.2?
Yes, it is. You'd need to implement a facelets resource resolver: stackoverflow.com/questions/5587808/…. But since you mentioned "xhtml" and JSF 1.2 is exit for 2 years, I automatically assumed JSF 2.0... So, you're using JSF 1.2? Sorry, my bad, I should not have assumed JSF 2.0. Is there any chance to upgrade it? This may be helpful then: stackoverflow.com/questions/4441713/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.