9

Is it possible to package JSF facelets and ManagedBeans into a JAR file? So that we can use this code and UI combination in different war/ear projects?

I am not talking about JSF Components!

If yes - can you point me to a tutorial or blog post

I need details about the Jar structure and additional files needed in the Jar?

Thanks Max

Tiny
  • 27,221
  • 105
  • 339
  • 599
Max
  • 91
  • 1
  • 2

1 Answers1

17

Yes, that's definitely possible, assuming that you're using JSF 2.0, part of Java EE 6.

As to the managed beans and other JSF classes like validators, converters, etc, just annotate them with @ManagedBean, @FacesValidator, @FacesConverter, etc and package them in the JAR the usual way. You only need to provide a JSF 2.0 compatible /META-INF/faces-config.xml file in the JAR.

<?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>

This way JSF will be triggered to scan the classes in the JAR for JSF specific annotations. Alternatively you can also just register them in the JAR's faces-config.xml the JSF 1.x way.

As to Facelets resources, just drop them in /META-INF/resources folder of the JAR. It'll be treated the same way as public webcontent of the WAR.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • is it possible to add the the jar file in the ear lib directory? or it should be placed under the war WEB-INF/lib directory? – Omar Al Kababji Oct 29 '11 at 20:27
  • Create answer which work quite well for my managed beans. But how can I deal with composites? I have some composite:implementations laying around in a package which is a view to a some EJBs. I want to use these in my main application and I do not want to collect all composites in the main WAR. Inside the containing WAR I can call them by a tag like where 'e:' is the name space to the library. The resource resolver does not seem to resolve this. – Rick-Rainer Ludwig Oct 31 '11 at 21:57
  • @BalusC What if META-INF/faces-config.xml does not contain beans info? I mean if the managed bean use annotations? Will it go? Or the faces-config.xml here just needed to make recognize IDE that this is the jsf lib only? – cbhogf Sep 22 '16 at 21:06