spring mvc 配置 thymeleaf

一、使用配置文件方式配置thymeleaf时,模板的默认位置是webcontent下面,使用注释方式配置时,模板的默认位置是class目录下。

目录(?)[+]

SpringMVC整合Thymeleaf模板

之前的工作中用到了Thymeleaf 视图模板,用起来真的感觉还不错,下面介绍下SpringMVC + thymeleaf模板的整合 
thymeleaf官方在github上面的宠物店示例https://github.com/thymeleaf/thymeleafexamples-petclinic,可以在github上面下载下来研究下

下面介绍下整合的方法:

  1. 关于springMVC的项目搭建就不多介绍了。网上可以找到很多例子
  2. 本文主要讲的是如何加入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上面的整合的源码,点右边 https://coding.net/u/nanhu/p/spring_myBatis_Thymeleaf/git

版权声明:本文为博主原创文章,未经博主允许不得转载。


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值