Matt RaibleMatt Raible is a Web Developer and Java Champion. Connect with him on LinkedIn.

The Angular Mini-Book The Angular Mini-Book is a guide to getting started with Angular. You'll learn how to develop a bare-bones application, test it, and deploy it. Then you'll move on to adding Bootstrap, Angular Material, continuous integration, and authentication.

Spring Boot is a popular framework for building REST APIs. You'll learn how to integrate Angular with Spring Boot and use security best practices like HTTPS and a content security policy.

For book updates, follow @angular_book on Twitter.

The JHipster Mini-Book The JHipster Mini-Book is a guide to getting started with hip technologies today: Angular, Bootstrap, and Spring Boot. All of these frameworks are wrapped up in an easy-to-use project called JHipster.

This book shows you how to build an app with JHipster, and guides you through the plethora of tools, techniques and options you can use. Furthermore, it explains the UI and API building blocks so you understand the underpinnings of your great application.

For book updates, follow @jhipster-book on Twitter.

10+ YEARS


Over 10 years ago, I wrote my first blog post. Since then, I've authored books, had kids, traveled the world, found Trish and blogged about it all.

Integrating Struts 2 + JSF + Facelets

What if you didn't have to choose between a request-based framework and a component-based framework? What if you could use them together and use request-based for some pages and component-based for others? This is the functionality that the Struts 2 JSF Plugin provides.

To be fair, the JSF-Spring project says it does the same thing for Spring MVC + JSF, but there doesn't appear to be any documentation.

I did some prototyping of Struts 2 + JSF and discovered that it does indeed work. I also discovered that there's no documentation on integrating it with Facelets. Luckily, it's pretty easy to do - hence my reason for writing this entry. You might ask why I want to use Facelets when JSF 1.2 supports JSP fairly well? My reason is because JSP 2.1 hijacks #{}, which Struts 2's OGNL uses for some expressions. Because of this, I want to be able to run on a JSP 2.0 container until a workaround comes along. Sun's JSF 1.2 RI can run on a JSP 2.0 container, while MyFaces 2.1 cannot (at least in my experience).

There's two ways to get Struts 2 + JSF + Facelets working:

  • Create a WEB-INF/faces-config.xml file and override the default view-handler:
    <?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_1_2.xsd"
        version="1.2">
    
        <application>
            <view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
        </application>
    </faces-config> 
    
  • The 2nd way is to use Ajax4JSF and declare the view-handler in your web.xml (allowing you to get rid of faces-config.xml):
    <context-param>
        <param-name>org.ajax4jsf.VIEW_HANDLERS</param-name>
        <param-value>com.sun.facelets.FaceletViewHandler</param-value>
    </context-param>
    
NOTE: You have to use 2nd method if you want to use Ajax4JSF. It won't read the view-handler from faces-config.xml.

If you're using SiteMesh, you may have to add another <parser> element to your sitemesh.xml to get Facelets pages decorated:

<parser content-type="application/xhtml+xml"
    class="com.opensymphony.module.sitemesh.parser.HTMLPageParser"/> 

Thanks to Laurie Harper for his assistance figuring this stuff out.

Now you might ask - why would you want to do this? For one, Struts 2 has a better navigation model (IMO) than JSF. Also, if developers want to use JSF and think it's a better way for a certain module - let them go to it!

Posted in Java at Jul 26 2007, 12:13:59 PM MDT 18 Comments
Comments:

I am not sure Ajax4jsf requires com.sun.facelets.FaceletViewHandler to be in the web.xml. Where you read this info from?

Posted by Sergey Smirnov on July 26, 2007 at 01:03 PM MDT #

It was required last October. I didn't read it anywhere, I learned it from experience. Maybe it's changed with newer versions of Ajax4JSF and Facelets.

Posted by Matt Raible on July 26, 2007 at 01:47 PM MDT #

I'm pretty certain Laurie Harper is male. (I'm also fairly certain that he is quite a helpful contributor to the struts mailing list, responding to messages that everyone else ignores.)

Posted by Dale Newfield on July 27, 2007 at 03:04 AM MDT #

I had the same thought while reading, "isn't Laurie a guy?" :)

Posted by musachy on July 27, 2007 at 01:14 PM MDT #

Doh! Damn ethnocentricity. Fixed now - thanks guys.

Posted by Matt Raible on July 27, 2007 at 01:16 PM MDT #

Hi guys, I am tring to get Struts 2 + JSF plugin working as what is says at Struts 2 JSF Plugin, but the plugin documentation is not that clear for me to follow, since I am new to Struts. Do you guys know what configurations I have to do, in which files to get this plugin working? Do you guys know any other Struts2 JSF plugin references elsewhere? Thank you very much!!!! Lupus

Posted by Lupus on July 30, 2007 at 01:31 PM MDT #

Lupus - it should be a matter of registering FacesServlet in your web.xml and configuring the Struts 2 JSF Plugin appropriately. Have you looked at the showcase example? That's how I figured it out.

I'll make sure and add a Struts 2 + JSF installer for AppFuse Light 1.8. I expect to release AL 1.8 and AppFuse 2.0 this month (crosses fingers).

Posted by Matt Raible on August 02, 2007 at 02:39 PM MDT #

Could any one please send me / leme know a link with a sample applciation includes JSF & Struts 2, ie., which uses JSF components and navigates through Struts 2 Config. I hardling trying out to make one.

My requirement is to use JSF compenents with my existing Struts apps. and navigation should go though "Struts config", and not though "faces-config".

Email id to send sample apps: [email protected]

Posted by Dharani on August 25, 2008 at 08:23 AM MDT #

The Struts 2 showcase example application shows you how to use JSF w/in a Struts 2 application. Have you tried using that as an example? That's how I figured it out.

Posted by Matt Raible on August 25, 2008 at 01:22 PM MDT #

The showcase example is not working. Please send a sample working example.the showcase example is too complicated to undestand...thanx in advance....

Posted by Parash Goswami on September 22, 2009 at 12:01 AM MDT #

I know this article is quite old, but I've been searching for hours, literally, and I'm getting desperate.

Matt, I have an existing Struts 1 application that I've successfully merged with Struts2 since they're in difference namespaces this worked great until I'm able to move struts 1 actions to struts 2.

I've also taken the time to implement Spring into my Struts 2 implementation which also works splendid.

So far so good.

Now, I want Ajax! Not Dojo or JQuery either. I want a complete Ajax component implementation as the foundation for some serious custom work I'm about to implement.

My question, have you ever been able to get RichFaces(or MyFaces) to seamlessly integrate with Struts 2? If not can you point me in the right direction as I'm literally going crazy trying to find ANY example of how this is done. I think I'm close but I'm getting errors related to th OGNL that I can't seem to get around.

Thank you for taking time to respond. I would sincerely appreciate ANY feedback that you can provide.

-john:m

Posted by John on November 14, 2010 at 10:43 PM MST #

My requirement is to use JSF compenents with my existing Struts apps. and navigation should go though "Struts config", and not though "faces-config".

Posted by Sindhu.A on June 22, 2011 at 03:14 PM MDT #

i need to integrate struts2 and jsf .my requirement is that only use jsf component as front and and back coding through struts.xml. if it is possible then please send me some example on my email address [email protected] Thank you in advance...

Posted by rp on June 30, 2011 at 10:28 AM MDT #

Rather than trying to integration JSF components into a Struts 2 application, I'd recommend integrating Ajax components (possibly through GWT). JSF is known for its "components", but in the end, they're just HTML and JavaScript and you can do that with almost any modern JavaScript framework.

Posted by Matt Raible on June 30, 2011 at 11:38 AM MDT #

am using struts2 jsf plugin in my application,want to use a4j for ajax support,what i do for this,anyone help me....pls...

Posted by AS on August 10, 2011 at 08:14 AM MDT #

I have had success using Struts2, JSF + Facelets and Richfaces together. I can provide the configuration needed, but it is too much to post here.

I do have one problem. It appears that there is no way (at least I have not found it) to configure struts2 + JSF/Facelets and still be able to use struts tags. I can use them if I don't configure Facelets, but the Facelets filter does not allow a taglib entry when it parses the .xhtml file.

I can add an xml namespace for struts-tags, and this allows me to get the tag autocomplete in eclipse, but the s:XXX tags are not rendered.

This means that the only real benefit of using Struts2 and facelets together is in the URL mapping provided by struts. Is there anyway to overcome this, or is it all or nothing when adding adding facelets?

Posted by Jesse Demarco on October 07, 2011 at 02:10 PM MDT #

I want to know if struts2 could be integrated with jsf2.x? is there any implementation or example? thanks

Posted by Kein on September 03, 2012 at 09:42 AM MDT #

Matt, I have an existing Struts 1 application that I've successfully merged with Struts2 this worked great.

Now I integrated JSF & Richfaces to my Struts2 implementation, I just need view pages with Rich tags its working fine. I had empty faces-config.xml for this. But I need to know how to call Struts Action by using <h:commandbutton />(Jsf tag in .xhtml).

As I know
1.In JSF, actions are called as "#{beanName.methodName}"
2.In Struts 2, actions are called as "methodNameActioName"

*** Please help me to call Struts Action by using Jsf tags(ex:<h:commandbutton />).

Thank you for taking time to respond. I would sincerely appreciate ANY feedback that you can provide.

Posted by Tito on September 11, 2014 at 08:50 AM MDT #

Post a Comment:
  • HTML Syntax: Allowed