为了账号安全,请及时绑定邮箱和手机立即绑定

Maven-SSM整合Thymeleaf模板引擎 Spring5整合(XML配置文件)

标签:
Spring

1. 依赖

在配置好SSM框架后,在pom.xml中添加如下依赖

<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring5</artifactId>
    <version>3.0.9.RELEASE</version></dependency>

2. 配置文件

Sping_mvc.xml中加入如下配置,并且注释掉jsp的viewResolver或freemarker的配置

<bean id="templateResolver"
      class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
    <property name="prefix" value="/WEB-INF/templates/"/>
    <property name="suffix" value=".html"/>
    <property name="characterEncoding" value="UTF-8"/>
    <property name="order" value="1"/>
    <property name="templateMode" value="HTML5"/>
    <property name="cacheable" value="false"/></bean><bean id="templateEngine"
      class="org.thymeleaf.spring5.SpringTemplateEngine">
    <property name="templateResolver" ref="templateResolver"/></bean><bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
    <property name="templateEngine" ref="templateEngine"/>
    <property name="characterEncoding" value="UTF-8"/></bean>

此配置需要注意以下几点:

  • templateResolver的prefix与suffix对应你的视图层的文件位置

  • templateResolver的characterEncoding和viewResolver的都要设置成UTF-8中文才不会乱码。

  • templateResolver的cacheable一定要在开发的时候设置成false不然无法看到实时的页面数据

3. 测试

  • controller:

    在ModelMap里面随便设置一点值

    @RequestMapping("/test")public String test(ModelMap map) {
        map.put("thText", "设置文本内容");
        map.put("thUText", "设置文本内容");
        map.put("thValue", "设置当前元素的value值");
        map.put("thEach", Arrays.asList("列表", "遍历列表"));
        map.put("thIf", "msg is not null");
        map.put("thObject", new                           UserEntity("sadfa","asfasfd","asfsaf","asdfasf","saf","asfd","sadf",1));      
        return "test";
    }
  • test.html

    <!DOCTYPE html><html lang="cn" xmlns:th="http://www.thymeleaf.org"><head>
        <meta charset="UTF-8">
        <title>Title</title></head><body><h1>TEST</h1><h2>Thymeleaf</h2><!--th:text 设置当前元素的文本内容,常用,优先级不高--><p th:text="${thText}" /><p th:utext="${thUText}" /><!--th:value 设置当前元素的value值,常用,优先级仅比th:text高--><input type="text" th:value="${thValue}" /><!--th:each 遍历列表,常用,优先级很高,仅此于代码块的插入--><!--th:each 修饰在div上,则div层重复出现,若只想p标签遍历,则修饰在p标签上--><div th:each="message : ${thEach}"> <!-- 遍历整个div-p,不推荐-->
        <p th:text="${message}" /></div><div> <!--只遍历p,推荐使用-->
        <p th:text="${message}" th:each="message : ${thEach}" /></div><!--th:if 条件判断,类似的有th:switch,th:case,优先级仅次于th:each, 其中#strings是变量表达式的内置方法--><p th:text="${thIf}" th:if="${not #strings.isEmpty(thIf)}"></p><!--th:insert 把代码块插入当前div中,优先级最高,类似的有th:replace,th:include,~{} :代码块表达式 --><div th:insert="~{grammar/common::thCommon}"></div><!--th:object 声明变量,和*{} 一起使用--><div th:object="${thObject}">
        <p>ID: <span th:text="*{id}" /></p><!--th:text="${thObject.id}"-->
        <p>TH: <span th:text="*{username}" /></p><!--${thObject.thName}-->
        <p>DE: <span th:text="*{password}" /></p><!--${thObject.desc}--></div></body></html>

几点注意:

  • 在html首标签里面加上xmlns

    <html lang="cn" xmlns:th="http://www.thymeleaf.org">
  • 同样把head的meta设置一个charset=“UTF-8”

至此,你就可以去配置tomcat运行项目了。



作者:罗蜀黍ilss
链接:https://www.jianshu.com/p/b997449183e3


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 2
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消