spring-boot-笔记-2-thymeleaf初步了解

来源:互联网 发布:深入浅出的数据分析 编辑:程序博客网 时间:2024/05/22 06:22

参看:
http://www.cnblogs.com/beyrl-blog/p/6633182.html
http://www.cnblogs.com/ityouknow/p/5833560.html
http://www.cnblogs.com/nuoyiamy/p/5591559.html
1.thymeleaf的pom.xml配置
2.application.properties文件配置
3.引入url
4.案例


注:
在maven搭建的spring-boot项目中有关web资源文件路径
在src/main/resources下:
static
    |–  css
    |–  images
    |–  js
templates
    |–  hello.html


1. thymeleaf的pom.xml配置

<dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-thymeleaf</artifactId>        </dependency>        <!--             <artifactId>spring-boot-starter-thymeleaf</artifactId>包含了下面的,故可以去掉            <artifactId>spring-boot-starter-web</artifactId>        </dependency>         -->
  1. <html xmlns:th=”http://www.thymeleaf.org”>在html页面的首行使用这个命名空间,使得可以利用th动态处理元素
  2. 通过@{}访问web的静态资源
  3. ${}访问model中的属性值


2.application.properties文件配置

#spring.profiles.active=testPspring.profiles.include: testa,testn,bean#配置tomcat端口server.port=80#配置tomcat访问路径,默认为/server.context-path=/springbootTest#配置tomcat编码server.tomcat.uri-encoding=UTF-8#Log configurationlogging.file=E:/log.loglogging.level.org.springframework.web = DEBUGdegub=true#thymeleaf startspring.thymeleaf.mode=HTML5spring.thymeleaf.encoding=UTF-8spring.thymeleaf.content-type=text/html#开发时关闭缓存,不然没法看到实时页面spring.thymeleaf.cache=false#thymeleaf end

3.引入URL
Thymeleaf对于URL的处理是通过语法@{…}来处理的
<a th:href=”@{http://blog.csdn.net/u012706811}”>绝对路径</a>
<a th:href=”@{/}”>相对路径</a>
<a th:href=”@{css/bootstrap.min.css}”>Content路径,默认访问static下的css文件夹</a>

4. 案例
控制类:

package com.example.springbootNote1;import java.util.ArrayList;import java.util.List;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import com.example.springbootNote1.entity.Person;@Controllerpublic class ThymeleafTest {    private Logger logger = LoggerFactory.getLogger(ThymeleafTest.class);    @Autowired    private Person person;    @RequestMapping("thymeleaf")    public String thymeleafTest(Model model){        List<Person> persons = new ArrayList<Person>();        Person person1 = new Person();        person1.setName("钟亮");        person1.setSex("男");        person1.setAge("12");        Person person2 = new Person();        person2.setName("孙正强");        person2.setSex("男");        person2.setAge("13");        persons.add(person);        persons.add(person1);        persons.add(person2);        model.addAttribute("person", person);        model.addAttribute("persons", persons);        return "thymeleaftest";    }}

实体代码:

package com.example.springbootNote1.entity;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.PropertySource;import org.springframework.stereotype.Component;@Component@ConfigurationProperties(prefix="person")//配置资源文件中的前缀//@PropertySource("classpath:config/bean.properties")//配置资源文件的路径public class Person {    private String name;    private String age;    private String sex;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getAge() {        return age;    }    public void setAge(String age) {        this.age = age;    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }}

html页面

<!DOCTYPE HTML><html xmlns:th="http://www.thymeleaf.org"><head>    <title>hello</title>    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />    <link rel="stylesheet" th:href="@{/css/a.css}"/></head><body><!--/*@thymesVar id="name" type="java.lang.String"*/--><p th:text="'Hello!:, ' + ${person.name} + '!' + '----' + ${person.age} + '----'+ ${person.sex}" ></p><br/><br/><div>    <p>input中的value</p>    <input type="text" th:value="${person.name}"/>    <br/><br/>    <p>运算符:</p>    <p th:with="isEven=(3 % 2 == 0)"></p>    <br/><br/>    <p>条件判断 是否为空</p>    <div th:if="${not #lists.isEmpty(persons)}">        <p>循环遍历</p>        <div th:each="person:${persons}">            <p style="background:olive;color:white;">                <span th:text="${person.name}"></span>--<span th:text="${person.age}"></span>                <span th:text="${person.sex}"></span>            </p>        </div>    </div>    <br/><br/>    <p>switch</p>    <div>        <div th:switch="${person.name}">             <p th:case="'曹雪坤'">曹雪坤</p>             <p th:case="#{person.age}">钟亮</p>             <p th:case="*">孙正强</p>        </div>    </div>    <br/><br/>    <p>if</p>    <p th:if="${person.age == '11'}">满足条件才显示</p>    <br/><br/>    <p>unless</p>    <p th:unless="${person.age == '12'}">不满足条件才显示</p>    <br/><br/>    <!-- <p>判断符号用法:gt(>), lt(<), ge(>=), le(<=), not(!). Also eq(==), neq/ne (!=)</p> -->    <p>gt(&gt;), lt(&lt;)</p>    <p th:if="11 &lt; 12">&lt;</p>    <br/><br/></div><!-- js获取model中的值 --><script th:inline="javascript">    var name = [[${person.name}]];    alert(name);</script></body></html>

资源文件
application-testa.properties

book.author=caoxuekun

application-testn.properties

book.name=spring boot

application-bean.properties

person.name=\u66F9\u96EA\u5764person.age=11person.sex=\u7537
原创粉丝点击