SpringMVC整合Thymeleaf模板
SpringMVC整合Thymeleaf模板
之前的工作中用到了Thymeleaf 视图模板,用起来真的感觉还不错,下面介绍下SpringMVC + thymeleaf模板的整合
thymeleaf官方在github上面的宠物店示例,可以在github上面下载下来研究下
下面介绍下整合的方法:
- 关于springMVC的项目搭建就不多介绍了。网上可以找到很多例子
- 本文主要讲的是如何加入thymeleaf模板
1.引入thymeleaf的包
maven项目的话
<thymeleaf.version>2.1.3.RELEASE</thymeleaf.version>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>${thymeleaf.version}</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring3</artifactId>
<version>${thymeleaf.version}</version>
</dependency>
如果不是maven项目的话,自己下载相关的包
2.更改必要配置
在springMVC的xml的配置文件如下配置(按照宠物实例的配置来的)
<!-- use thymeleaf -->
<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/thymeleaf/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<!-- Template cache is set to false (default is true). -->
<property name="cacheable" value="false" />
<property name="characterEncoding" value="UTF-8"/>
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<!--
- The ContentNegotiatingViewResolver delegates to the InternalResourceViewResolver and BeanNameViewResolver,
- and uses the requested media type (determined by the path extension) to pick a matching view.
- When the media type is ‘text/html‘, it will delegate to the InternalResourceViewResolver‘s JstlView,
- otherwise to the BeanNameViewResolver.
-->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="contentNegotiationManager" ref="cnManager"/>
<property name="viewResolvers">
<list>
<!-- Used here for ‘xml‘ and ‘atom‘ views -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="1"/>
</bean>
<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="characterEncoding" value="UTF-8"/>
<property name="order" value="2"/>
<!-- We need to set exclusions because Content Negotiation tries to resolve from -->
<!-- Thymeleaf even if a specific view bean (of a different class) is already -->
<!-- found, which might cause exceptions (e.g. ThymeleafView does not have a -->
<!-- ‘marshaller‘ property). -->
<property name="excludedViewNames" value="*.xml" />
</bean>
<!-- Default viewClass: JSTL view (JSP with html output)-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/"/>
<property name="suffix" value=".jsp"/>
<property name="order" value="3"/>
</bean>
</list>
</property>
</bean>
<!-- Simple strategy: only path extension is taken into account -->
<bean id="cnManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="true"/>
<property name="ignoreAcceptHeader" value="true"/>
<property name="defaultContentType" value="text/html"/>
<property name="mediaTypes">
<map>
<entry key="html" value="text/html" />
<entry key="xml" value="application/xml" />
<entry key="atom" value="application/atom+xml" />
</map>
</property>
</bean>
这样就可以了,但是官方宠物店的例子里面没有涉及到中文的问题,这是我们需要注意的地方
bean id=”templateResolver” class=”org.thymeleaf.templateresolver.ServletContextTemplateResolver” 这个bean里面加入 能够使页面中文可以正常显示。
bean class=”org.thymeleaf.spring3.view.ThymeleafViewResolver” 这个bean 中加入 可以是数据中的中文正常显示
页面中thymeleaf的使用的话依照thymeleaf的语法就行了
另外,附上本人在coding.net上面的整合的源码,点右边 spring_myBatis_Thymeleaf
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。