Announcement Announcement Module
Collapse
No announcement yet.
Setting content type Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Comment


    • #3
      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

      Comment


      • #4
        I have the same problem. Anybody can help me?

        Comment


        • #5
          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

          Comment


          • #6
            <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>

            Comment


            • #7
              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

              Comment


              • #8
                Solution for XML JSPs with InternalResourceViewResolver

                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.

                Comment


                • #9
                  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

                  Comment

                  Working...
                  X