Config from docs, but not the same result

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

Config from docs, but not the same result

mresc
Hi, I use spring mvc and thymeleaf config from the official docs. But I have to name my template the file with double extension (".html.html") to get it running. Here my config:
	<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
		<property name="prefix" value="/WEB-INF/templates/" />
		<property name="suffix" value=".html" />
		<property name="templateMode" value="XHTML" />
	</bean>

	<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
		<property name="templateResolver" ref="templateResolver" />
	</bean>
	
   	<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
		<property name="templateEngine" ref="templateEngine" />
		<property name="order" value="1" />
		<property name="viewNames" value="*.html,*.xhtml" />
	</bean>
	
	<bean name="main" class="org.thymeleaf.spring3.view.ThymeleafView">
		<property name="staticVariables">
			<map>
				<entry key="footer" value="Some company: &lt;b&gt;ACME&lt;/b&gt;" />
			</map>
		</property>
	</bean>
My controller returns just a string with the name of template without extension, but it doesn't work till my template has double extension, something like "/WEB-INF/templates/template.html.html". What's wrong?
Reply | Threaded
Open this post in threaded view
|

Re: Config from docs, but not the same result

danielfernandez
Administrator
Hi,

Actually, that code is taken from different --and, you are right, incompatible-- fragments of code from the tutorial documents. Put together, those fragments don't recognize your view names correctly and force you to the double ".html" extension.

In fact, if you are only using one Spring ViewResolver (i.e. if you are only using Thymeleaf as a view-layer technology) your ViewResolver bean does not need a "viewNames" property at all, because you don't need to make a distinction between which pages will be resolved by thymeleaf and which ones by other template engines --like e.g. JSP--.

If you remove that "viewNames" property, your files will not need the double extension, and you will be able to name your views from at the Spring MVC controller without the ".html" suffix.

Regards,
Daniel.
Reply | Threaded
Open this post in threaded view
|

Re: Config from docs, but not the same result

mresc
Thanks for reply. I have more controllers than this one and they generates json and xml responses. It's comfortable to let spring's ContentNegotiatingViewResolver with favorPathExtension=true controll which view is used. How can I configure thymeleaf to render only *.html requests?
Reply | Threaded
Open this post in threaded view
|

Re: Config from docs, but not the same result

danielfernandez
Administrator

In that case, leave the "*.html" value in "viewNames" and just remove the suffix from your template resolver.

The difference is, you will need to specify the ".html" part in your view names at the controller.

Regards,
Daniel.
Reply | Threaded
Open this post in threaded view
|

Re: Config from docs, but not the same result

mresc
I've tried it already. In this case files with fragment have to have no extension, because th:include can't find it if the name ends with ".html".
Reply | Threaded
Open this post in threaded view
|

Re: Config from docs, but not the same result

mresc
So, it works now. Suffix is still there, so there's no need to add .html to template name. I've removed viewNames property and it works now: .json, .xml and .html (via thymeleaf).

Thanks for help.

Best regards, Anton.