2012/12/30 Burton Rhodes <[email protected]>:
> Lukasz -
>
> Thanks for all your help. You got me on the right track and it appears the
> json Result was what was overriding and setting the content type. As a
> result, I have overridden the JSONResult class to use the "dynamic"
> content-Type.
You're welcome :-)
> /**
> * Custom json result type to set Content-Type response header dynamically
> to either 'application/json' or 'plain/text'
> * according to what the request header identifies in the "Accept" header
> *
> * (this is to fix browser wanting to download the json response on an ajax
> submit)
> *
> * @author Burton Rhodes
> *
> */
> public class JsonDynamicContentTypeResult extends JSONResult {
>
> private static final Logger LOG =
> LoggerFactory.getLogger(JsonDynamicContentTypeResult.class);
>
> @Override
> public void execute(ActionInvocation invocation) throws Exception {
>
> ActionContext actionContext = invocation.getInvocationContext();
> HttpServletRequest request = (HttpServletRequest)
> actionContext.get(StrutsStatics.HTTP_REQUEST);
> HttpServletResponse response = (HttpServletResponse)
> actionContext.get(StrutsStatics.HTTP_RESPONSE);
>
> // Set Content-Type according to what the request will "accept"
> if (request.getHeader("Accept")!=null &&
> request.getHeader("Accept").toLowerCase().contains("application/json")) {
> this.setContentType("application/json");
> } else {
> // Default to text/plain
> this.setContentType("text/plain");
> }
>
> try {
> Object rootObject;
> rootObject = readRootObject(invocation);
> writeToResponse(response, createJSONString(request,
> rootObject), enableGzip(request));
> } catch (IOException exception) {
> LOG.error(exception.getMessage(), exception);
> throw exception;
> }
> }
>
>
> }
Right now contentType isn't dynamically resolved, please register an
issue and I'll add such option to JSONResult so you will be able to
specify contentType via <param/> tag, eg.
<result type="json">
<param name="contentType">${resolveContentType}</param>
</result>
and the action's resolvedContentType method will be called to obtain
valid contentType
Happy New Year :-)
--
Ćukasz
+ 48 606 323 122 http://www.lenart.org.pl/
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]