File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Do I have to choose only one view solution in Struts2? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Do I have to choose only one view solution in Struts2?" Watch "Do I have to choose only one view solution in Struts2?" New topic
Author

Do I have to choose only one view solution in Struts2?

Peter DiCrescenzo
Greenhorn

Joined: Apr 16, 2010
Posts: 10
Hello Struts Folks --

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?

Peter DiCrescenzo
peter.dicrescenzo@gartner.com
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I don't know the overarching answer, but I think supporting both Velocity and FreeMarker is a bad idea: pick one. Better yet, pick one of the three.
Peter DiCrescenzo
Greenhorn

Joined: Apr 16, 2010
Posts: 10
Hello Dave! (disclosure: I've currently only got Ian Roughley's Practical Apache Struts2 Web 2.0 Projects for a source text, but have your Apache Struts2 Web Application Development book from Packt on a wish-list. I'm delighted you're able to weigh in on this.)

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?

-pjd
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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?
Peter DiCrescenzo
Greenhorn

Joined: Apr 16, 2010
Posts: 10
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?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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.
Peter DiCrescenzo
Greenhorn

Joined: Apr 16, 2010
Posts: 10
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?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Do I have to choose only one view solution in Struts2?
 
Similar Threads
Struts2 velocity tag problem (org.apache.velocity.exception.ParseErrorException: Encountered "#end)
Velocity + Struts2 + Sitemesh + Spring +Hibernate Integration How to configur web.xml?
Struts2 Start up error : org.apache.struts2.dispatcher.FilterDispatcher java.lang.NullPointerExcept
Struts2 Action/OGNL in SiteMesh decorator
struts2 out of the box problem