您现在的位置是:首页 > java技术交流java技术交流
springboot整合thymeleaf springboot博客(二)
上善若水2020-06-10 18:06:36【java技术交流】
2003人已围观
简介当前环境下thymeleaf无疑是使用最为广泛的模板引擎,随着spring完善,出现了许多优秀的页面渲染技术,jsp技术已经越来越少人使用,thymeleaf无疑是主流,一起来学习thymeleaf整
当前环境下thymeleaf无疑是使用最为广泛的模板引擎,随着spring完善,出现了许多优秀的页面渲染技术,jsp技术已经越来越少人使用,thymeleaf无疑是主流,一起来学习thymeleaf整合thymeleaf.
引入jar包
当我们不知道pom依赖是什么的时候,可以去maven中心仓库中搜索,点进去复制坐标,粘贴到pom中即可.
在项目pom文件中加入依赖 这里注意引入的是spring-boot-starter-thymeleaf,引错会报找不到映射.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
这里我们选择版本号,让maven自动导入.我们可以看到默认导入了最新版3.0.11.RELEASE
版
编写入门案例
本案例参考https://github.com/wuyouzhuguli/SpringAll thymeleaf章节书写.
首先建立一个bean与一个控制器
package com.springboot.blog.bean;
public class Account {
private String account;
private String name;
private String password;
private String accountType;
private String tel;
public Account(String account, String name, String password, String accountType, String tel) {
super();
this.account = account;
this.name = name;
this.password = password;
this.accountType = accountType;
this.tel = tel;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAccountType() {
return accountType;
}
public void setAccountType(String accountType) {
this.accountType = accountType;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
}
package com.springboot.blog.controller;
import com.springboot.blog.bean.Account;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.ArrayList;
import java.util.List;
@Controller
public class IndexController {
@RequestMapping("/account")
public String index(Model m) {
List<Account> list = new ArrayList<Account>();
list.add(new Account("KangKang", "康康", "e10adc3949ba59abbe56e", "超级管理员", "17777777777"));
list.add(new Account("Mike", "麦克", "e10adc3949ba59abbe56e", "管理员", "13444444444"));
list.add(new Account("Jane","简","e10adc3949ba59abbe56e","运维人员","18666666666"));
list.add(new Account("Maria", "玛利亚", "e10adc3949ba59abbe56e", "清算人员", "19999999999"));
m.addAttribute("accountList",list);
return "account";
}
}
增加css与html,建立文件夹
table {
margin: 20px 40px 20px 0px;
width: 100%;
border-collapse: collapse;
border-spacing: 0;
table-layout:auto;
word-wrap: break-word;
}
table>tbody>tr:nth-of-type(odd) {
background-color: #F7F7F7
}
th, td {
padding: 8px;
text-align: left;
vertical-align: middle;
font-weight: normal;
font-size: 12px;
border-bottom: 1px solid #fff;
}
th {
padding-bottom: 10px;
color: #fff;
font-weight: 700;
background: rgba(66, 185, 131, .9)
}
td {
border-bottom-width: 1px
}
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>account</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" th:href="@{/css/style.css}" type="text/css">
</head>
<body>
<table>
<tr>
<th>no</th>
<th>account</th>
<th>name</th>
<th>password</th>
<th>accountType</th>
<th>tel</th>
</tr>
<tr th:each="list,stat : ${accountList}">
<td th:text="${stat.count}"></td>
<td th:text="${list.account}"></td>
<td th:text="${list.name}"></td>
<td th:text="${list.password}"></td>
<td th:text="${list.accountType}"></td>
<td th:text="${list.tel}"></td>
</tr>
</table>
</body>
</html>
修改yml配置
spring:
thymeleaf:
cache: false #关闭缓存
运行http://127.0.0.1:8080/account
成功配置第一个thymeleaf 页面.
自己遇到的问题
在配置所有代码后请求提示This application has no explicit mapping for /error, so you are seeing this as a fallback.
,在排除application入口文件位置没有错误的情况下,最终发现是yml配置出现问题,可能在网上复制的配置存在特殊字符还是啥的.清空yal,运行正常,解决.
Tags: springboot博客
很赞哦! (1)
相关文章
随机图文
一个让人瞬间爆笑的笑话
1、有一个女孩子平常被妈妈管的很严。有一次被男朋友叫去看电影,临出门时妈妈嘱咐说:“出去要放聪明点不要被男人占了便宜,如果他摸你上面你就说不要,摸你下边你就说停。”女孩说记住了,晚上回来她妈问她有没有被占便宜,女孩哭着说:“占了,他上下一起摸我,我就照你教的说:不要停,不要停。 2、幸福就是痒的时候挠一下,不幸就是痒了但挠不着,更不幸的是,很久以来灵魂和肉体都感觉不到那种蠢蠢欲动的痒了。 3、php技术提升心得与方法
现在的PHP市场虽然充斥了大量的的PHP开发人员,但这些人当中真正能称得上高手的却寥寥无几。很多公司虽然招聘了一些PHP开发人员,但是由于技术水平不高,导致公司的项目一直堆积。这不仅另公司无奈也让已经入职的PHP开发人员着急,他们也想要在PHP领域更近一步,但却苦于找不到提高自己的方法,下面我们的鸥仔收集了一些PHP大神的一些工作方式、习惯,让大家看看PHP大神们是如何工作,也希望这些方法能帮助到那些想要在PHP领域更近一步的人。10个笑死人不偿命的笑话,发给你,足以让你笑着过周末了!
1.妻子想让老公早回家,于是规定:晚于23点回家就锁门。第一周奏效,第二周老公又晚归,老婆按制度把门锁了,于是老公干脆不回家了。 老婆郁闷,后经高人指点,修改规定:23点前不回家,我就开着门睡觉。老公大惊,从此准时回家。 可见制度的精髓不在于强制,而在于对被执行者利益的拉动。 2.小鸡问母鸡:可否不用下蛋,带我出去玩啊?母鸡道:不行,我要工作! 是因为你创造价值,淘汰是因为你失去价值。过去快乐段子(二)
1、本人男,今天上厕所,突然闯进来一女生,我还没反应过来,这女生大吼:“对不起,我什么也没看见。”哥顿时就不爽了,你TM是在嘲笑我小吗?2、有生之年我希望人们用这三句话来羞辱我:1,你怎么帅成这个球样