History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: WW-1808
Type: Improvement Improvement
Status: Resolved Resolved
Resolution: Fixed
Priority: Minor Minor
Assignee: Unassigned
Reporter: Jasper Rosenberg
Votes: 2
Watchers: 2
Operations

If you were logged in you would be able to see more operations.
Struts 2

Support Freemarker template_exception_handler=rethrow

Created: 13/Mar/07 07:49 AM   Updated: 26/Jun/07 08:30 PM
Component/s: Plugin - Tags
Affects Version/s: 2.0.6
Fix Version/s: 2.1.0

Flags: Patch


 Description  « Hide
If you set in freemarker.properties, the property:
template_exception_handler=rethrow

Then you do not want the FreemarkerResult to output to the Writer unless you know the template processed cleanly (so you can handle the exception and go to a nice error page rather than being stuck with half rendered template output).

My suggestion is to add a new parameter to FreemarkerResult, something like "writeCompleted" which defaults to false and, if true, renders the template into a String buffer, writing the result to the actual Writer only if it succeeded.

Something like:

// Process the template
Writer writer = getWriter();
if (getWriteCompleted()) {
    CharArrayWriter charArrayWriter = new CharArrayWriter();
    template.process(model, charArrayWriter);
    charArrayWriter.flush();
    charArrayWriter.writeTo(writer);
} else {
    template.process(model, writer);
}

 All   Comments   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Martin Gilday - 05/May/07 06:05 AM
I have created a patch for the same issue in WebWork 2.2.5, using a slightly different method.
See [1] for the patch and [2] for the WebWork forum posting.

[1] http://jira.opensymphony.com/browse/WW-1458
[2] http://forums.opensymphony.com/thread.jspa?threadID=81479

It should be trivial to apply the same patch to Struts2, assuming the FreemarkerResult has not changed too much between projects. I wouldn't have thought it has.

We are currently developing a new app using WW2.2.5 so we will be giving this a thorough testing over the next week.

Jasper Rosenberg - 05/May/07 06:26 AM
FYI, the relevant FreemarkerResult code had actually changed a bit in Struts 2 from WW 2.2.4.

The reason I suggested adding a parameter for this behavior rather than just always writing to a buffer first is that I didn't want users that weren't using rethrow to have to have this extra overhead for every page rendered.

We have been using my suggested patch in production for months (originally with WW 2.2.4), and it behaves as desired.

Martin Gilday - 05/May/07 04:06 PM
Ah I see. Sorry Jasper, I didn't see where writeCompleted was set. Kind of obvious now I read it again. Have you investigated if it is possible to have the param set based on what is set in freemarker.properties to avoid the duplication in xwork.xml/struts.xml ?

Jasper Rosenberg - 05/May/07 07:34 PM
That is an excellent point. Perhaps instead of checking getWriteCompleted() (or in addition to it), in FreemarkerResult, we can just check the exception handler on the configuration:

if (configuration.getTemplateExceptionHandler() == TemplateExceptionHandler.RETHROW_HANDLER) {
    CharArrayWriter charArrayWriter = new CharArrayWriter();
    template.process(model, charArrayWriter);
    charArrayWriter.flush();
    charArrayWriter.writeTo(writer);
} else {
    template.process(model, writer);
}

This handles this most common case, but we might still want the optional parameter in case someone wants to use their own TemplateExceptionHandler that also requires the same behavior for FreemarkerResult.

Musachy Barroso - 26/Jun/07 08:30 PM
Thanks for the feedback and the patch guys!