日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

第八篇:Spring Boot整合Thymeleaf_入门试炼04

發布時間:2024/9/27 javascript 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第八篇:Spring Boot整合Thymeleaf_入门试炼04 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

繼承parent父工程,新建一個子項目,名稱為spring-boot-chapter-8

  • 1、引入 thymeleaf 模板依賴
<!-- 引入 thymeleaf 模板依賴 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency>
  • application.yml添加配置信息
############################################################ # thymeleaf 靜態資源配置 ############################################################ spring:thymeleaf:prefix: classpath:/templates/suffix: .htmlmode: HTML5encoding: UTF-8servlet:content-type: text/htmlcache: false #spring.thymeleaf.prefix:前綴 #spring.thymeleaf.suffix:后綴 #spring.thymeleaf.mode=HTML5:模板 #spring.thymeleaf.encoding:編碼格式 #spring.thymeleaf.servlet.content-type #spring.thymeleaf.cache:關閉緩存,即時刷新,上線生產數安靜需要改為true
  • 創建實體類User
@Data public class User {private Long id;private String name;private String password;private String desc;private Integer age;private Date birthday; }
  • 新建ThymeleafController
@Controller @RequestMapping("/th") public class ThymeleafController {@RequestMapping("/index")public String index(ModelMap map) {map.addAttribute("name", "thymeleaf-imooc");return "thymeleaf/index";}@RequestMapping("center")public String center() {return "thymeleaf/center/center";}@RequestMapping("test")public String test(ModelMap map) {User u = new User();u.setName("superadmin");u.setAge(10);u.setPassword("123465");u.setBirthday(new Date());u.setDesc("<font color='green'><b>hello imooc</b></font>");map.addAttribute("user", u);User u1 = new User();u1.setAge(19);u1.setName("imooc");u1.setPassword("123456");u1.setBirthday(new Date());User u2 = new User();u2.setAge(17);u2.setName("LeeCX");u2.setPassword("123456");u2.setBirthday(new Date());List<User> userList = new ArrayList<>();userList.add(u);userList.add(u1);userList.add(u2);map.addAttribute("userList", userList);return "thymeleaf/test";}@PostMapping("postform")public String postform(User u) {System.out.println("姓名:" + u.getName());System.out.println("年齡:" + u.getAge());return "redirect:/th/test";}@RequestMapping("showerror")public String showerror(User u) {int a = 1 / 0;return "redirect:/th/test";} }

新建接口類UserRepository

public interface UserRepository {/*** 保存或更新用戶** @param user* @return*/User saveOrUpdateUser(User user);/*** 根據id刪除用戶** @param id*/void deleteUser(Long id);/*** 根據id查詢用戶** @param id*/User getUserById(Long id);/*** 查詢用戶列表*/Collection<User> listUsers(); }
  • 新建接口實現類UserRepositoryImpl
@Repository public class UserRepositoryImpl implements UserRepository {private static AtomicLong counter = new AtomicLong();//計數器 添加用戶/次 +1private final ConcurrentMap<Long, User> userMap = new ConcurrentHashMap<Long, User>();@Overridepublic User saveOrUpdateUser(User user) {Long id = user.getId();if (id == null) {id = counter.incrementAndGet();user.setId(id);}this.userMap.put(id, user);//id 對應 userreturn user;}@Overridepublic void deleteUser(Long id) {this.userMap.remove(id);}@Overridepublic User getUserById(Long id) {return this.userMap.get(id);}@Overridepublic Collection<User> listUsers() { // Collection<User> userList = this.userMap.values();return this.userMap.values();} }
  • 在resourcestemplates/目錄下面新建thymeleaf目錄: 新建index.html和test.html
    index.html內容如下:
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"> <head lang="en"><meta charset="UTF-8" /><title></title> </head> <body> Thymeleaf模板引擎 <h1 th:text="${name}">hello world~~~~~~~</h1> </body> </html>
  • test.html內容如下:
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"> <head lang="en"><meta charset="UTF-8" /><title></title><!-- <script th:src="@{/static/js/test.js}"></script> --></head> <body><div>用戶姓名:<input th:id="${user.name}" th:name="${user.name}" th:value="${user.name}"/><br/>用戶年齡:<input th:value="${user.age}"/><br/>用戶生日:<input th:value="${user.birthday}"/><br/>用戶生日:<input th:value="${#dates.format(user.birthday, 'yyyy-MM-dd')}"/><br/> </div><br/><div th:object="${user}">用戶姓名:<input th:id="*{name}" th:name="*{name}" th:value="*{name}"/><br/>用戶年齡:<input th:value="*{age}"/><br/>用戶生日:<input th:value="*{#dates.format(birthday, 'yyyy-MM-dd hh:mm:ss')}"/><br/> </div><br/>text 與 utext :<br/> <span th:text="${user.desc}">abc</span> <br/> <span th:utext="${user.desc}">abc</span> <br/> <br/>URL:<br/> <a href="" th:href="@{http://www.imooc.com}">網站地址</a> <br/><br/> <form th:action="@{/th/postform}" th:object="${user}" method="post" th:method="post"><input type="text" th:field="*{name}"/><input type="text" th:field="*{age}"/><input type="submit"/> </form> <br/><br/> <div th:if="${user.age} == 18">十八歲的天空</div> <div th:if="${user.age} gt 18">你老了</div> <div th:if="${user.age} lt 18">你很年輕</div> <div th:if="${user.age} ge 18">大于等于</div> <div th:if="${user.age} le 18">小于等于</div> <br/><br/> <select><option >選擇框</option><option th:selected="${user.name eq 'lee'}">lee</option><option th:selected="${user.name eq 'imooc'}">imooc</option><option th:selected="${user.name eq 'LeeCX'}">LeeCX</option> </select> <br/><br/> <table><tr><th>姓名</th><th>年齡</th><th>年齡備注</th><th>生日</th></tr><tr th:each="person:${userList}"><td th:text="${person.name}"></td><td th:text="${person.age}"></td><td th:text="${person.age gt 18} ? 你老了 : 你很年輕">18歲</td><td th:text="${#dates.format(user.birthday, 'yyyy-MM-dd hh:mm:ss')}"></td></tr> </table> <br/><br/> <div th:switch="${user.name}"><p th:case="'lee'">lee</p><p th:case="#{roles.manager}">普通管理員</p><p th:case="#{roles.superadmin}">超級管理員</p><p th:case="*">其他用戶</p> </div> <br/></body> </html>
  • 在resourcestemplates/thymeleaf目錄下新建center目錄并在目錄下:
    新建index.html和test.html center.html內容如下:
<!DOCTYPE html> <html> <head lang="en"><meta charset="UTF-8" /><title></title> </head> <body> Thymeleaf模板引擎 <h1>center page</h1> </body> </html>
  • 啟動項目,依次測試: GET請求
  • 訪問首頁:http://localhost:8080/th/index

  • 訪問test頁:http://localhost:8080/th/test
  • 訪問中心頁:http://localhost:8080/th/center

    http://localhost:8080/showerror

    POST請求:
  • 訪問post表單首頁:http://localhost:8080/th/postform?name=hhhh&age=1

本文源碼下載:

github地址:
https://github.com/gb-heima/Spring-Boot-Actual-Combat/tree/master/parent/spring-boot-chapter-8

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的第八篇:Spring Boot整合Thymeleaf_入门试炼04的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。