Structure for multiple JSF projects with shared code
Asked Answered
C

1

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.

Commonwealth answered 30/11, 2011 at 2:38 Comment(0)
P
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 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>

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>

That's all.

See also:

Peters answered 30/11, 2011 at 3:22 Comment(14)
Many thanks! I'm also honored by your response having frequented your blogs on a number of JSF issues on many occasions!Commonwealth
@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 :)Trantrance
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)Commonwealth
@BalusC: Is this different if I'm using JSF 1.2?Commonwealth
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/…Peters
Thanks... I've redone the resource resolver and fit it in... I'm missing the tag that actually includes the resource sitting in the jar file. Don't have access to h:outputStylesheet in 1.2. As for 1.2 to 2.0 migration, I have a ton of customizations with RichFaces that I've been using that will take time to migrate ... it's a hard sell at this point to my manager.Commonwealth
Those tags are available since JSF 2.0 only. You'd need to create a servlet which does the streaming job.Peters
I tried a few options: <style><ui:composition template="/common/css/base.css" /></style> threw a XML parse exception (I assume this is because it's trying to actually parse and interpret the contents of the CSS file) <a4j:loadStyle src="/common/css/base.css" /> - this simply does not resolve the content and I see an http 404 error (The requested resource (/test-web/view/content/common/css/base.css)Commonwealth
Those JSF 2.0 tags actually load those resources from the classpath, not from public web content. You cannot link to JAR-packaged resources by URL. You need to create a servlet which does essentially the same by Thread.currentThread().getContextClassLoader().getResourceAsStream("/META-INF/resources" + request.getPathInfo()) and so on. Then, you invoke that servlet by URL instead like as <link rel="stylesheet" src="/resourceServlet/common/css/base.css" />Peters
Should <ui:include src="/common/sometemplate.xhtml" /> not have /resources added as prefix? Both don't work for me if i move that resource into a shared jar in src/META_INF/resources/common/sometemplate.xhtmlBelldame
@BalusC, if I have any dependencies of external libraries to compile such Java Project, I mean .jar libraries, will it include them into compiled .jar? What happened if I have same *.jar in a both projects, Java Project and Dynamic Web Project? Will it included twice? Thank you.Punchboard
Where should I put my resource bundles (i.e. language properties files)? I have exactly the structure like described here. I've tried to put my language files in every possible directory and registered the resource bundle in faces-config, however, the texts are not resolved.Bergwall
@Filip: They're looked up as classpath resources, not as web resources. So, just put them in a package like normal Java classes. See also the answers which you can find when you search on the error message you got.Peters
@BalusC: Thanks for a prompt answer as always. I have already figured it out. However, I think the behavior is like this: the resource bundle is first lookedup in the web application and if it is not found, it is lookedup in jar. My problem was that I had the files with the same name in the webapp and also in the shared lib but exposed as different vars in faces-config. I got no error message and even no ???key??? like errors. Just nothing.Bergwall

© 2022 - 2024 — McMap. All rights reserved.