Thymeleaf语法

说明:在所有微服务的架构设计之中,主要是分为两个部分:WEB、RPC部分,而现在所学习的 thymeleaf组件主要是工作在web部分,使用这个组件的目的是为了替代掉JSP页面文件(随着现代的技术发展,这样的替代似乎有些无力),如果要想在项目之中整合 thymeleaf组件,那么一定要使用到如下的依赖库:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>    

1.如果要想进行页面的应用,则所有的动态页面都需要保存在src/main/view/templates/文件夹下,所有的静态页面需要保存在src/main/view/static/文件夹下。其中view为源文件夹,并且所有的文件后缀都要为*.html。因为这些都是由 SpringBoot为其默认配置完成的,如果要修改的话,则修改application.yml配置文件即可,所有的与thymeleaf相关的默认配置的项都在“ThymeleafProperties”类中有所定义,该类定义有如下的几个属性,这些属性都有默认值:

private String prefix="classpath:/templates/";//默认前缀
private String suffix=".html";//默认后缀
//如果有需要也可以在application.yml文件中修改
application.yml文件
spring:
  thymeleaf:
    prefix: classpath:/pages/  #前缀
    suffix: .htm          #后缀

2.导入图片/js文件/cs文件格式:

  ● 导入css文件:在<head></head>标签中写  <link rel="stylesheet" type="text/css" th:href="@{/css/style.css}">

  ● 导入js文件:在<head></head>标签中写  <script type="text/javascript" th:src="@{/js.message.js}"></script>

  ● 导入图片:在<body></body>标签中写    <img th:src="@{/images/docker.png}">

3.页面对象输出:对于对象,有可能是一个普通的VO对象,那么也有可能是一个Map集合或者是Lit集合。

@RequestMapping("/send")
public String send(Model model){//为了传递request属性
    Message message=new Message();
    message.setTitle("一个老女人的自传");
    message.setNote("傻屌");
    message.setPubdate(new Date());
    model.addAttribute("message",message)  ;
    return "message/message_send" ;  
}

前台html页面输出:

<p th:text="'消息标题:'+${message.title}"/>
<p th:text="'消息内容:'+${message.title}"/>
<p th:text="'发送日期:'+${#dates.format(message.pubdate,'yyyy-MM-dd HH:mm:ss')}"/><!--格式化日期-->

上面的页面输出格式也可以采用下面这种:

 <span th:object="${message}">
        <p th:text="'消息标题:' + *{title}"/>
        <p th:text="'消息内容:' + *{note}"/>
        <p th:text="'发送日期:' + *{#dates.format(pubdate,'yyyy-MM-dd HH:mm:ss')}"/>
    </span>

  

 


posted @ 2019-07-24 22:31  王兴龙123  阅读(256)  评论(0编辑  收藏  举报