Results 1 to 9 of 9

Thread: Setting content type

  1. #1
    Join Date
    Aug 2004
    Posts
    123

    Default Setting content type

    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:

    Code:
    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.

  2. #2
    Join Date
    Aug 2004
    Posts
    123

    Default

    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" %>

  3. #3
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    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

  4. #4

    Default

    I have the same problem. Anybody can help me?

  5. #5
    Join Date
    Jan 2009
    Posts
    20

    Default

    Quote Originally Posted by jonmor View Post
    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" %>
    This no longer works. And having a view with the correct content type doesn't seem to either.

    See this:
    http://forum.springframework.org/sho...d.php?p=223959

  6. #6
    Join Date
    Aug 2007
    Location
    Brisbane, Australia
    Posts
    33

    Default

    <bean id="viewResolver"
    class="org.springframework.web.servlet.view.veloci ty.VelocityViewResolver">
    <property name="cache" value="false" />
    <property name="prefix" value="" />
    <property name="suffix" value=".vm" />
    <property name="contentType" value="text/xml"></property>
    </bean>

  7. #7
    Join Date
    Jan 2009
    Posts
    20

    Default

    Quote Originally Posted by travisjwarren View Post
    <bean id="viewResolver"
    class="org.springframework.web.servlet.view.veloci ty.VelocityViewResolver">
    <property name="cache" value="false" />
    <property name="prefix" value="" />
    <property name="suffix" value=".vm" />
    <property name="contentType" value="text/xml"></property>
    </bean>
    This doesn't work for InternalResourceView

  8. #8
    Join Date
    Mar 2008
    Posts
    6

    Default Solution for XML JSPs with InternalResourceViewResolver

    Quote Originally Posted by jonmor View Post
    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" %>
    We just got this to work with the InternalResourceViewResolver with the following trick.

    First, our InternalResourceViewResolver is configured in the standard way:
    Code:
    <bean name="tempViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="order" value="2" />
    		<property name="prefix" value="/WEB-INF/jsp/" />
    		<property name="suffix" value=".jsp" />
    </bean>

    Then, our JSP has the following on the very first line:

    Code:
    <%@ page contentType="text/xml; charset=UTF-8" %><?xml version="1.0" encoding="UTF-8"?>
    <xmlElements...>
    It is important to note that the <xml ..> tag is on the same line as the page directive. With the <xml ...> tag just one line below, we were getting invalid xml document errors.

  9. #9
    Join Date
    Jan 2009
    Posts
    20

    Default

    Quote Originally Posted by Garberage View Post
    We just got this to work with the InternalResourceViewResolver with the following trick.

    First, our InternalResourceViewResolver is configured in the standard way:
    Code:
    <bean name="tempViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="order" value="2" />
    		<property name="prefix" value="/WEB-INF/jsp/" />
    		<property name="suffix" value=".jsp" />
    </bean>

    Then, our JSP has the following on the very first line:

    Code:
    <%@ page contentType="text/xml; charset=UTF-8" %><?xml version="1.0" encoding="UTF-8"?>
    <xmlElements...>
    It is important to note that the <xml ..> tag is on the same line as the page directive. With the <xml ...> tag just one line below, we were getting invalid xml document errors.

    In tomcat 6.0.14 this still wont work because by default the view will do a forward with the request dispatcher. And tomcat overrides the view and viewresolver. It even ignores the directive on the jsp or any java scriplet in the jsp to set the content type.

    You can set the resolver to always do an include instead.

    Take a look here:

    http://forum.springframework.org/showthread.php?t=66556

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •