tags:

views:

17

answers:

1

Hi,

I am working on a Java web app that uses Struts 2 with the REST plugin. By convention, any URL that ends with ".xml" will be sent through the XStreamHandler. This is fine--it's what I want in most cases.

But in just a small number of cases I want to stream XML back to the browser. I know how to set up a @Result(type="stream") annotation and make it return an InputStream. I've done this for images in other parts of the app. The problem with returning XML as a stream is the REST plugin sees that the caller is requesting XML so it tries to deserialize the Action class instead of just streaming out my InputStream.

How can I tell the REST plugin, just in these few special cases, not to send an XML result through the XStreamHandler?

Thanks!

A: 

Oh, cool, I figured out the answer. The secret is to return an instance of a class that implements com.opensymphony.xwork2.Result.

Assuming 'getFileName()' returns the name of the XML file I want to stream:

@SkipValidation
public com.opensymphony.xwork2.Result streamFile() throws FileNotFoundException {
    return new StreamResult(new FileInputStream(new File(getFileName())));
}
Brian Morearty