PDA

View Full Version : Setting content type


jonmor
Mar 17th, 2006, 02:39 PM
I want to explicitly set the content type of some responses to 'text/xml' (away from the default 'text/html'). I've looked back through the archives for how to do this and the recurrent answer is to set the content type for the View. Trouble is, I really can't see how I would do this. Taking a very simplified example, where I'm simply wanting to take the output of a JSP page and set the content type for it, I might have this in my AbstractController subclass:


protected ModelAndView handleRequestInternal(HttpServletRequest req, HttpServletResponse res) throws Exception {
return new ModelAndView("xmlPage");
}


How do I set the content type for the view here? If I do a getView on the ModelAndView, I will get a null, because the view hasn't been resolved yet. (And even if I did, I would need to cast it from View to something like AbstractView, which contains the setContentType method). I don't want to set this globally in my ViewResolver, as some responses should be text/xml and others text/html.

Any pointers would be appreciated.

jonmor
Mar 17th, 2006, 03:14 PM
Aha, I think I may have been misunderstanding what the term 'view' meant in this context. It's not the View class, but the jsp (for example). If I stick the following at the top of my JSP, it works:

<%@ page contentType="text/xml; charset=UTF-8" %>

Colin Yates
Mar 20th, 2006, 08:10 AM
For future reference, you can set the contentType as a property of most (if not all) of the View implementations (as in JstlView, XmlView, InternalUrlResourceView etc.) within Spring.

Not at my desk so I cannot confirm which ones you can and cannot set ;)

lexius
Nov 25th, 2007, 11:10 AM
I have the same problem. Anybody can help me?