I'm (very new to Struts2 but am) working on integrating Struts2 as an MVC foundation for a web app that, at the moment, just provides a set of custom taglibs and a data object layer for a team of content creators and page designers, using Hibernate-by-way-of-Spring-IOC.
The goal is to build in a templating aspect to the app, where requests to the actions, after business logic, result in template-driven output (either through Velocity or FreeMarker), or a JSP error page. In selecting Struts2, I'd thought that it'd be possible to provide support for both along with JSP, based on the selection of result types I can choose from (freemarker, velocity, default JSP, et cetera).
Using the Struts2 Starter archetype in Maven, I know I can start with this...
web.xml ...and substitute org.apache.struts2.sitemesh.FreeMarkerPageFilter or org.apache.struts2.sitemesh.VelocityPageFilter for PageFilter. But I'm not clear on how I should layer in the filters for all three, if the action's conditions could result in a different template being driven, like this:
struts.xml Trying to direct to the native VelocityResult with PageFilter turned on results in ClassNotFound exceptions for Velocity's Context object; switching to VelocityPageFilter but directing to a default JSP result returns exceptions complaining about missing FreeMarker dependencies.
In searching JavaRanch and the web at large, I've found examples showing me that I can use Velocity or FM or JSP for view output, but nothing saying that I can (or can't, for that matter) provide support for all three, switching between results when I need to. Have I stumbled upon an implied, but not explicit, limitation of Struts2, or am I missing something painfully obvious?
The environment I'm working with will necessitate at least two - my content guys currently generate hundreds of JSPs, many of which (but nowhere near all) would be candidates for templating.
The actual templating action could drive to Velocity (or FreeMarker) only, if I absolutely have to do that - but I'd still need to provide for an "Error" result which would drive to one of the error-code JSPs we've got (300,400,500 errors etc.), and I can't preclude JSP views from additional actions that I end up authoring in Struts2.
You'd recommended using native Velocity results to Thiyagaraja Thamilvaanan last year - but is there a better approach than using the native result options in Struts2, that are still optimal for Struts2, considering what I'm trying to accomplish? Is Struts2 still an appropriate MVC framework for me to try to do this in?
I recommended Velocity only because that's what they were already trying to do--between Velocity and FreeMarker I recommend FreeMarker because of the better error reporting.
You're aware you can template using JSP, right?
Joined: Apr 16, 2010
Yes, but I'm looking to support a templating scheme like Velocity (and/or FreeMarker); again, is there a better approach than using the native result options in Struts2, that are still optimal for Struts2? Is Struts2 still an appropriate MVC framework for me to try to do this in?
Like I said--I don't know. I think trying to support more than JSP plus another one is a bad idea. SiteMesh will glom together whatever you want, depending on how you're doing your templating. I have multiple JSP + FreeMarker apps together, but except for components, I've never mixed multiple technologies together, because I think it gets too confusing.
Joined: Apr 16, 2010
I'll work with that to start - so SiteMesh should allow me to have a Struts2 action where my result could be driven to JSPs (PageFilter) or templates (VelocityPageFilter), depending on which result I pick? What did I miss in my configuration to allow that?
I have no idea. As I've said, I don't use Velocity, and its support is not as good as the FreeMarker support. The result choice shouldn't matter as far as SiteMesh goes--as long as the filters are in the correct order (which I don't know what is), I don't know why it wouldn't work. I know people are doing SiteMesh + FreeMarker, and I'd be surprised if JSP didn't "just work".