0

I have two projects, I need to reuse same jsf managed bean classes for other projects. Is there any way I can create a separate project for jsf managed bean classes + util classes and refer them in all projects I will do.

Bob Kaufman
  • 12,864
  • 16
  • 78
  • 107
Eric
  • 1,171
  • 2
  • 14
  • 27

2 Answers2

1

Depending on the IDE used, create a simple "Java Project" or a "Web Fragment Project" and make sure that it has a JSF 2.0 compatible /META-INF/faces-config.xml file in the source folder. Then configure your web projects to include that project as a JAR in the /WEB-INF/lib (again, depends on IDE used, in Eclipse just add the module project to Deployment Assembly list in web project's properties). Once JSF finds a JAR in /WEB-INF/lib with a JSF 2.0 compatible /META-INF/faces-config.xml, then JSF will automatically scan the entire JAR for JSF artifacts.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I am using netbeans 7 and it did find the managed beans on editors but when I compile and deploy at runtime it giving me an error!!!! – Eric May 02 '12 at 15:26
  • Apparently there's no JSF 2.0 compatible `/META-INF/faces-config.xml` in the JAR, or the project didn't end up as a valid JAR in webapp's `/WEB-INF/lib`. Sorry, can't help you with more detail with Netbeans as I've never really used it for more than 5 minutes. – BalusC May 02 '12 at 15:27
  • which one are you using eclipse ? – Eric May 02 '12 at 15:28
  • It should be possible in Netbeans as well. You just need to verify if the project has really ended up as a JAR in the `/WEB-INF/lib` of the built WAR and that there's a JSF 2.0 compatible `/META-INF/faces-config.xml` file in the JAR. In Eclipse, only adding the other project to the build path of the main web project isn't right. That's also a common mistake among Eclipse users. The project has to be added as deployment assembly (which ultimately makes it to end up as JAR in `/WEB-INF/lib`). Perhaps you made exactly this mistake in Netbeans. – BalusC May 02 '12 at 15:29
  • I am gonna give it a try now and let you know the resutls, anyway seems you want to keep your dev ID secret :) – Eric May 02 '12 at 15:31
  • Oh, of course I am using Eclipse. I've also some hands on experience with IntelliJ. – BalusC May 02 '12 at 15:32
0

Since you are using Java EE 6, if you are willing to use CDI it would be quite of an easy thing to do. I did it myself a couple of times.

Put your Managed Beans in a separated JAR project and import it on the projects you want to use them. The only thing you have to do is use @Named instead of @ManagedBean and have a beans.xml in /META-INF of both projects. That way you can reference your CDI enabled beans in your facelets pages and also gain the whole flexibility and power of CDI models.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Fabricio Lemos
  • 2,905
  • 2
  • 31
  • 31