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> -->
- <html xmlns:th=”http://www.thymeleaf.org”>在html页面的首行使用这个命名空间,使得可以利用th动态处理元素
- 通过@{}访问web的静态资源
- ${}访问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(>), lt(<)</p> <p th:if="11 < 12"><</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
- spring-boot-笔记-2-thymeleaf初步了解
- 3. Spring Boot 初步了解
- [spring-boot] thymeleaf 热交换
- Spring Boot + Gradle + Thymeleaf
- spring boot 整合 thymeleaf
- spring boot vue Thymeleaf
- spring boot thymeleaf 用法
- Spring Boot使用Thymeleaf
- Spring-Boot thymeleaf异常
- spring-boot集成thymeleaf
- Spring Boot整合thymeleaf
- Spring Boot+Spring Security+Thymeleaf
- Spring Boot学习笔记-Thymeleaf模板引擎的配置
- Thymeleaf系列一 Spring boot 集成Thymeleaf
- Spring Boot (三) Spring Boot for thymeleaf
- spring boot thymeleaf 入门 demo
- spring-boot--使用thymeleaf模板
- spring-boot--使用thymeleaf模板
- POJ1222 EXTENDED LIGHTS OUT 高斯消元入门
- 【CUGBACM15级BC第22场 B】hdu 5143 NPY and arithmetic progression
- nvprof tx1 or tx2
- spring表达语言(SpEL)快速掌握
- 栈(Stack)的C语言实现
- spring-boot-笔记-2-thymeleaf初步了解
- 即时搜索的input和propertychange方法
- 如何从思维模式上彻底改变学习模式?
- Pacemaker
- [IOI1994]The Castle
- [编程题] 度度熊回家
- Java程序实现欧几里得算法-计算两个数的最大公约数
- 匈牙利算法
- 跳转传参,butterknife,xrecyclerview监听,刷新加载综合