Java EE6> Packaging JSF facelets (xhtml) and ManagedBeans as JAR
Asked Answered
F

1

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

Forwhy answered 24/5, 2011 at 0:15 Comment(0)
T
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:

Thou answered 24/5, 2011 at 2:3 Comment(3)
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?Casimiracasimire
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 <e:composite/> where 'e:' is the name space to the library. The resource resolver does not seem to resolve this.Hoosegow
@Thou 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?Doubledecker

© 2022 - 2024 — McMap. All rights reserved.