一、构建springboot工程
参考源码地址
https://github.com/leechenxiang/imooc-springboot-starter
可选IDE
STS Spring Tool Suit
快速开始:https://spring.io/quickstart
配置文件 application.properties
############################################################ # # 开发模式设置 # ############################################################ # 热部署生效 spring.devtools.restart.enabled=true # 监听目录 spring.devtools.restart.additional-paths=src/main/java #spring.devtools.restart.exclude=static/**,public/** #spring.devtools.restart.exclude=WEB-INF/** ############################################################ # # server 服务端配置 # ############################################################ server.port=8080 #server.servlet.context-path=/demo #server.error.path=/error #server.address=192.168.1.2 #server.session-timeout=60 ############################################################ # # server.tomcat 服务端配置 # ############################################################ #server.tomcat.max-threads=250 server.tomcat.uri-encoding=UTF-8 #server.tomcat.basedir=H:/springboot-tomcat-tmp #server.tomcat.access-log-enabled=true #server.tomcat.access-log-pattern= #server.tomcat.accesslog.directory= #logging.path=H:/springboot-tomcat-tmp #logging.file=myapp.log
二、接口返回json
# 时间格式化 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss spring.jackson.time-zone=GMT+8 spring.jackson.serialization.write-dates-as-timestamps=false package com.example.demo.pojo; /** * 统一的返回封装 */ public class JsonResult { private Integer code; private String msg; private Object data; public JsonResult(Integer code, String msg, Object data) { this.code = code; this.msg = msg; this.data = data; } public static JsonResult success(Object data){ return new JsonResult(0, "success", data); } public static JsonResult error(String errorMessage) { return new JsonResult(-1, errorMessage, null); } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } } package com.example.demo.pojo; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonInclude; import lombok.Data; import java.util.Date; /** * jackson注解使用示例 */ @Data public class User { private String name; private Integer age; // 忽略显示 @JsonIgnore private String password; // 格式化 @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", locale="zh", timezone="GMT+8") private Date birthday; // 为空不显示 @JsonInclude(JsonInclude.Include.NON_NULL) private String desc; }
三、热部署
<!--热部署--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency>
IDEA需要做额外的配置
四、资源属性配置
resource.properties
# 配置文件 com.demo.name=MyBlog com.demo.language=zh package com.example.demo.pojo; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; /** * 资源文件中的配置映射到实体类 */ @Configuration @ConfigurationProperties(prefix = "com.demo") @PropertySource(value = "classpath:resource.properties") public class Resource { private String name; private String language; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLanguage() { return language; } public void setLanguage(String language) { this.language = language; } }
五、模板引擎
<!--模板引擎 freemarker--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <!--模板引擎 thymeleaf--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ############################################################ # # freemarker # ############################################################ # 上线改为true spring.freemarker.cache=false #spring.freemarker.template-loader-path=classpath:/templates #spring.freemarker.charset=UTF-8 #spring.freemarker.check-template-location=true #spring.freemarker.content-type=text/html #spring.freemarker.expose-request-attributes=true #spring.freemarker.expose-session-attributes=true #spring.freemarker.request-context-attribute=request #spring.freemarker.suffix=.ftl ############################################################ # # thymeleaf # ############################################################# # 上线改为true spring.thymeleaf.cache=false #spring.thymeleaf.prefix=classpath:/templates/ #spring.thymeleaf.suffix=.html #spring.thymeleaf.mode=HTML5 #spring.thymeleaf.encoding=UTF-8 #spring.thymeleaf.servlet.content-type=text/html # 静态文件 spring.mvc.static-path-pattern=/static/**
freemarker
<h2>freemarker</h2> <p>${resource.name}</p> <p>${resource.language}</p>
thymeleaf
基本使用方式
对象引用方式
时间类型转换
text与utext
URL
引入静态资源文件js/css
条件判断th:if与th:unless
循环th:each
分支th:switch与th:case
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> <script th:src="@{/static/js/index.js}"></script> </head> <body> <h2>基本语法</h2> <p><input type="text" th:value="${user.name}" th:id="${user.name}" th:name="${user.name}"></p> <p><input type="text" th:value="${user.age}"></p> <p><input type="text" th:value="${user.birthday}"></p> <h2>日期格式化</h2> <p><input type="text" th:value="${#dates.format(user.birthday, 'yyyy-MM-dd')}"></p> <h2>简便写法</h2> <div th:object="${user}"> <p><input type="text" th:value="*{name}" th:id="*{name}" th:name="*{name}"></p> <p><input type="text" th:value="*{age}"></p> <p><input type="text" th:value="*{birthday}"></p> </div> <h2>text与utext</h2> <p>text: <span th:text="${user.desc}"></span></p> <p>utext: <span th:utext="${user.desc}"></span></p> <h2>网址</h2> <a th:href="@{https://www.baidu.com/}">www.baidu.com</a> <h2>表单</h2> <form th:action="@{/thymeleaf/user}" method="post" th:object="${user}"> <!-- field == id, name, value --> <input type="text" th:field="*{name}"> <input type="submit"> </form> <h2>判断</h2> <p th:if="${user.age} == 20"> age == 20</p> <p th:if="${user.age} gt 20"> age > 20</p> <p th:if="${user.age} lt 20"> age < 20</p> <p th:if="${user.age} ge 20"> age >= 20</p> <p th:if="${user.age} le 20"> age <= 20</p> <h2>选择框</h2> <select name="" id=""> <option th:selected="${user.age} == 20">20</option> <option th:selected="${user.age} == 18">18</option> </select> <h2>循环</h2> <p th:each="person:${userList}"> <span th:text="${person.name}"></span> </p> <h2>分支</h2> <p th:switch="${user.name}"> <span th:case="#{roles.superadmin}">超级管理员</span> <span th:case="#{roles.manager}">管理员</span> <span th:case="*">普通会员</span> </p> </body> </html>