Spring Boot的Web配置

一.使用YML文件配置多套环境
   
 
application.yml 
spring:
  profiles:
    #active指向其他配置文件======此处引用(application-test.properties)
    active: test
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql:///invoicingsystem
    username: root
    password: 123
application-test.properties
#映射级别
mybatis.configuration.auto-mapping-behavior=full
 
 
#包别名
mybatis.type-aliases-package=com.invoicing.project.entity
 
 
#取消使用缓存
spring.thymeleaf.cache=false
 
 
#端口号
server.port=7070
 
如需配置多套运行环境,添加配置文件的同时,切换yml文件profiles-active引用的文件即可
 
文件中可以配置不同的端口号和其他配置,在properties或者yml文件都可以配置
 
二. thymeleaf的介绍与配置
 
简单介绍:
Thymeleaf 与JSP类似,最大的区别在于,JSP在运行之后才能得纯HTML,而 Thymeleaf 在运行之前也是纯html。Thymeleaf跟 Velocity、FreeMarker 等模板引擎大同小异 ,相较与其他的模板引擎,它有如下三个极吸引人的特点:
 
1.Thymeleaf 在有网络和无网络的环境下皆可运行。它可以让在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。
 
2.Thymeleaf 开箱即用。它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、该jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。
 
3.Thymeleaf 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。
 
配置:
1、这里使用maven来管理项目,首先在pom文件中引入相关依赖。
        
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf</artifactId>
    <version>3.0.11.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring4</artifactId>
    <version>3.0.11.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
 
 
2、配置属性
 
其实完全可以直接使用,不用配置。但是Spring Boot官方文档建议在开发时将缓存关闭,那就在application.properties文件中加入下面这行就行了:
 
spring.thymeleaf.cache=false
 
小例子:
 
 
标签中的th:text属性用来填充该标签的内容,也就是说在上面的例子中<span>标签中的值将会被th:text属性中${session.uesr.userName}的值替换。假如${session.uesr.userName}的值是Hello Thymeleaf!那么网页打开时显示的内容将会变成Hello Thymeleaf!。
 
在传统的JSP中,我们需要在<span>标签中加入${session.uesr.userName}才能让浏览器显示数据,每次开发前端页面我们必须部署项目启动服务器之后才能显示数据,从而根据显示的数据进行美化或者修改前端的页面。使用Thymeleaf 模板引擎之后,不需要部署项目,只要在前端设置模板数据用于查看显示效果就可以了,在标签中加入th:text属性之后,项目部署时前端设置的模板数据就会被后端的数据(通常是数据库中的数据)替换,这样开发的时候就可以专注于前端或者专注于后端,大大提高我们的开发效率,如今的是趋势前后端开发分离,使用Thymeleaf 模板引擎能减少团队之间因前后端开发差异产生的问题。
 
Thymeleaf的标准表达式语法
 
Thymeleaf有四种标准表达式语法
 
    变量表达式
    URL表达式
    选择(星号)表达式
    文字国际化表达式
 
在这里着重介绍前面两种
1. 变量表达式
 
变量表达式即OGNL表达式或Spring EL表达式(在Spring术语中也叫model attributes)。像这种:${user.name}
 
<p>UserName is :  <span th:text="${user.name}">xiaoming</span> !</p>
 
默认运行结果是:UserName is : xiaoming ! 如果${user.name}的值是xiaohong则输出UserName is : xiaohong !
这种与JSP的方式一样
 
2. URL表达式
 
Thymeleaf对于URL的处理是通过语法@{…}来处理,举个简单的例子:
 
<a th:href="@{http://www.baidu.com}">Thymeleaf</a>
 
Thymeleaf支持绝对路径URL也支持相对路径的URL:
相对路径写法:
@{…/thymeleaf/hello(userName=${user.username})}
添加参数:
 
@{/thymeleaf/hello(userName=${user.username},userPassWord=${user.userpassword})}
 
URL后面的(userName=${user.username},userPassWord=${user.userpassword})表示将括号内的内容作为参数处理,多个参数用逗号隔开,该语法避免使用字符串拼接,大大提高了可读性
 
如果要对URL进行修改,可以使用 th:href和th:src
 
<!-- 访问的地址是 'http://localhost:8080/thymeleaf/hello?userName=xiaoming)'  -->
<a href="hello.html"
   th:href="@{http://localhost:8080/thymeleaf/hello(userName=${user.username})}">view</a>
 
 
<a href="hello.html" th:href="@{/thymeleaf/hello(userName=${user.username})}">view</a>
 
3.1算术运算
 
表达式支持算数运算,比如:+、-(减号)、*、/、%以及单独的 -(负号)
简单举两个例子
语法     实例     运行结果
+     <p th:text="${Product.price+999}" ></p>     1000(设Product.price为1)
-     <p th:text="${Product.price-999}" ></p>     1(设Product.price为1000)
3.2 逻辑运算
 
逻辑运算符>, <, <=,>=,==,!=,!都可以使用,唯一需要注意的是使用<,>时需要用它的HTML转义符
3.3 条件运算
 
If-then: (if) ? (then)
 
 
If-then-else: (if) ? (then) : (else)
 
 
Default: (value) ?: (defaultvalue)
 
 
Thymeleaf常用标签
关键字     操作     例子
th:id     替换id     <input th:id="'xxx' + ${collect.id}"/>
th:text     文本替换     <p th:text="${collect.description}">description</p>
th:utext     支持html的文本替换     <p th:utext="${htmlcontent}">conten</p>
th:object     替换对象     <div th:object="${session.user}">
th:value     属性赋值     <input th:value="${user.name}" />
th:with     变量赋值运算     <div th:with="isEven=${prodStat.count}%2==0"></div>
th:style     设置样式     th:style="'display:' + @{(${sitrue} ? 'none' : 'inline-block')} + ''"
th:onclick     点击事件     th:"'getCollect()'"
th:each     属性赋值     tr th:each="user,userStat:${users}"
th:if     判断条件     <a th:if="${userId == collect.userId}" >
th:unless     和th:if判断相反     <a th:href="@{/login}" th:unless=${session.user != null}>Login</a>
th:href     链接地址     <a th:href="@{/login}" th:unless=${session.user != null}>Login</a>
th:switch     多路选择 配合th:case 使用     <div th:switch="${user.role}">
th:case     th:switch的一个分支     <p th:case="'admin'">User is an administrator</p>
th:fragment     布局标签,定义一个代码片段,方便其它地方引用     <div th:fragment="alert">
th:include     布局标签,替换内容到引入的文件     <head th:include="layout :: htmlhead" th:with="title='xx'"></head>
th:replace     布局标签,替换整个标签到引入的文件     <div th:replace="fragments/header :: title"></div>
th:selected     selected选择框 选中     th:selected="(${xxx.id} == ${configObj.dd})"
th:src     图片类地址引入     <img class="img-responsive" alt="App Logo" th:src="@{/img/logo.png}" />
th:inline     定义js脚本可以使用变量     <script type="text/javascript" th:inline="javascript">
th:action     表单提交的地址     <form action="subscribe.html" th:action="@{/subscribe}">
th:remove     删除某个属性     <tr th:remove="all"> 1.all:删除包含标签和所有的孩子。’2.body:不包含标记删除,但删除其所有的孩子。3.tag:包含标记的删除,但不删除它的孩子。4.all-but-first:删除所有包含标签的孩子,除了第一个。5.none:什么也不做。这个值是有用的动态评估。
th:attr     设置标签属性,多个属性可以用逗号分隔     比如 th:attr="src=@{/image/aa.jpg},title=#{logo}",此标签不太优雅,一般用的比较少。
posted @ 2019-12-04 20:11  怀鑫  阅读(769)  评论(0编辑  收藏  举报