How do I use an XML template file with Thymeleaf?

classic Classic list List threaded Threaded
6 messages Options
wli
Reply | Threaded
Open this post in threaded view
|

How do I use an XML template file with Thymeleaf?

wli
Apologies in advance if I'm missing an obvious tutorial or bit of documentation, but I am trying to generate an XML site map using a Thymeleaf template:

<?xml version="1.0" encoding="ISO-8859-1"?>
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
        xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:th="http://www.thymeleaf.org">
	<url th:each="page : ${pages}">
		<loc th:text="${page.url}">http://path/to/page</loc>
		<lastmod th:text="${page.lastModified}" th:unless="${#strings.isEmpty(page.lastModified)}">Last Modified</lastmod>
		<changefreq th:text="${page.changeFrequency}" th:unless="${#strings.isEmpty(page.changeFrequency)}">Change Frequency</changefreq>
		<priority th:text="${page.priority}" th:unless="${#strings.isEmpty(page.priority)}">Priority</priority>
	</url>
</urlset>

Here is the relevant configuration:

<bean id="xmlTemplateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
    <beans:property name="prefix" value="/WEB-INF/xmlviews/" />
    <beans:property name="suffix" value=".xml" />
    <beans:property name="templateMode" value="XML" />
    <beans:property name="cacheable" value="false" />
</bean>

<bean id="xmlTemplateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine"> 
	<beans:property name="templateResolver" ref="xmlTemplateResolver" /> 
</bean>

<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
	<beans:property name="templateEngine" ref="xmlTemplateEngine" /> 
	<beans:property name="characterEncoding" value="UTF-8"/>
	<beans:property name="order" value="1" />
	<beans:property name="viewNames" value="sitemap*" />
</bean>

Finally, here is the controller method:

@RequestMapping(value = "/sitemap.xml", method = RequestMethod.GET, produces=MediaType.TEXT_XML_VALUE)
	public String siteMap(Model model, HttpServletRequest request, HttpServletResponse response) throws IOException {

	// Irrelevant code omitted

	response.setContentType("text/xml");
	return "sitemap";
}

The page is actually coming up, however nothing I do seems to work with regards to making it actual XML; it keeps coming across as text/html. Am I missing something obvious, or can I not do this through Thymeleaf?
Reply | Threaded
Open this post in threaded view
|

Re: How do I use an XML template file with Thymeleaf?

Zemi
Administrator
I'm sorry but I don't understand completely your sentence
wli wrote
The page is actually coming up, however nothing I do seems to work with regards to making it actual XML; it keeps coming across as text/html. Am I missing something obvious, or can I not do this through Thymeleaf?
Do you mean that the generated markup is not valid XML? Or what is wrong is just the content type? In the first case, could you post your generated markup?
wli
Reply | Threaded
Open this post in threaded view
|

Re: How do I use an XML template file with Thymeleaf?

wli
The generated markup is correct, but it's coming back as the wrong content type and nothing I'm doing seems to fix that.
Reply | Threaded
Open this post in threaded view
|

Re: How do I use an XML template file with Thymeleaf?

Zemi
Administrator
I know that not helps, but I tried myself to generate a XML and I am facing the same results, content type is always "text/html" no matter what I do.
Reply | Threaded
Open this post in threaded view
|

Re: How do I use an XML template file with Thymeleaf?

Zemi
Administrator
Hi again.

You have a contentType property in the ViewResolver object
   http://www.thymeleaf.org/apidocs/thymeleaf-spring3/2.0.15/org/thymeleaf/spring3/view/ThymeleafViewResolver.html

add it to your configuration:

   <bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
        ...
        <beans:property name="contentType" value="text/xml" /> 
  </bean>
wli
Reply | Threaded
Open this post in threaded view
|

Re: How do I use an XML template file with Thymeleaf?

wli
That did the trick, thanks!