SpringBoot+Thymeleaf超好用的前后端数据交互模板引擎

开发工具选用IDEA,尽量选择高版本的Thymeleaf避免版本不兼容问题,使用它可以完全替代JSP。

准备

  • 1、pom文件
    除了普通的SpringBoot项目,版本选择1.5.10,除了引入Web模块的场景启动器,数据库模块等等之外,还需要thymeleaf的场景启动器,为了兼容性,还需要指定thymeleaf较高的版本 ,pom文件主要的依赖如下(包含但不仅限于,看需求):
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath/>
    </parent>
&lt;!-- 布局功能的支持程序thymeleaf3主程序  layout2以上版本 --&gt;
&lt;!-- thymeleaf2 SpringBoot的版本是1.5.10 在这里指定较高版本的thymeleaf 3.0.0--&gt;
&lt;properties&gt;
    &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
    &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
    &lt;java.version&gt;1.8&lt;/java.version&gt;
    &lt;thymeleaf.version&gt;3.0.0.RELEASE&lt;/thymeleaf.version&gt;
    &lt;thymeleaf-layout-dialect.version&gt;2.0.0&lt;/thymeleaf-layout-dialect.version&gt;
&lt;/properties&gt;

&lt;!--下面这些依赖看需要选择,主要的是这些--&gt;
&lt;dependencies&gt;
	 &lt;!--模板引擎--&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt;
    &lt;/dependency&gt;
     &lt;!--web模块是一定要的--&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
    &lt;/dependency&gt;
&lt;/dependencies&gt;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 2、application.properties文件指定视图解析器
    当一个请求被SpringMVC的Controller解析了之后,就会根据这样的视图解析器去找寻对应的html文件,比如,controller返回的字符串结果是“hello”,解析之后的响应的html文件会是/templates/hello.html,这是SpringMVC最简单的使用。
#thymeleaf start
#视图解析器的前缀放在这个文件夹
spring.thymeleaf.prefix=classpath:/templates/
#后缀
spring.thymeleaf.suffix=.html
#模式
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.servlet.content-type=text/html
#编码格式
spring.thymeleaf.encoding=utf-8
#不用缓存
spring.thymeleaf.cache=false
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**
#thymeleaf end

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 3、附上项目结构
    在这里插入图片描述

使用

1、所有html页面添加头

<html xmlns:th="http://www.thymeleaf.org">

 
 
  • 1

2、引入CSS和JS文件
对比上面给出的目录结构图,不难发现thyleleaf引入资源文件是直接写static目录下的绝对路径即可,语法如下:

 <link href="/layui/css/layui.css" th:href="@{/layui/css/layui.css}" rel="stylesheet">
 <script src="/js/jquery-1.9.1.min.js" th:src="@{/js/jquery-1.9.1.min.js}"></script>

 
 
  • 1
  • 2

3、传值给html页面动态显示
在Controller里面使用Model:

@Controller
public class TestController {
    /**
     * 测试视图解析器
     */
    @RequestMapping("/hello")
    public String hello(Model model) {
        String name = "xiaosha";
        model.addAttribute("name", name);
        return "hello";
    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

hello.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"/>
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'">3333</p>
</body>
</html>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

测试结果:
在这里插入图片描述

传递对象

最常用的就是传值到表单,做数据回显,后端Controller在Model存储了一个对象:

    @RequestMapping("/getQuestion")
    public String getQuestionToForm(Model model) {
        Question question = new Question();
        question = questionService.getQuestionByWord(word);
        model.addAttribute("que",question);
        return "questionlist";
    }

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

前端页面这样写:使用${que.word}取出值来

<input type="text" id="word" name="word"  th:value="${que!=null}?${que.word}">
<input type="text" id="answer" name="answer" th:value="${que!=null}?${que.answer}">              

 
 
  • 1
  • 2

使用thymeleaf将后端的值在html页面上显示就是这么方便, 不管是字符串、数组、对象、集合大致都差不多,可以直接在页面上进行数据校验,逻辑判断等等。快速入门请参照官方文档: https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#introducing-thymeleaf

在JS里面怎么拿取值呢?也是超级简单!

var levle = [[${level}]];   //thymeleaf在Js里面获取SpringMVCModel里面写的值

 
 
  • 1
@RequestMapping(value="/socketQuestion")
public String socketQuestion( Model model,@RequestParam String level){
        model.addAttribute("level",level);
        return "questionsocket";
 }

 
 
  • 1
  • 2
  • 3
  • 4
  • 5

通常情况下前端想要获取后端的数据是通过Ajax发送异步请求去获取,然后写JS代码去处理这些数据,最后拼接字符串形成html标签,最后被浏览器解析,实现无刷新而更改页面数据。使用了Thymeleaf之后,在JS里面通过[[${ }]]获取值也很方便。

抽取公共元素

在一个网站中,经常是这样的情况,头部、底部、菜单栏等是每个页面都有的,并且功能大致都一样,所以thymeleaf提供了将这些一样的标签页面元素抽取到一个html文件中,当其它html页面有相同的部分需要用到时,直接使用一对标签引入即可。
大致实现步骤,使用一对nav标签,使用 th:fragment=" xxx" 对一部分代码块命名,抽取到一个html页面。

<!--顶栏-->
<nav  th:fragment="topbar">
    <div class="top">
        <div align="right">
            <button type="button" class="btn-sign"  style="vertical-align:middle"><span>免费注册</span></button>
            <button type="button" class="btn-log" style="vertical-align:middle"><span>马上登录</span></button>
        </div>
    </div>
</nav>

<!–底部信息展示栏–>
<nav th:fragment=“bottombar”>
<div class=“foot” style=“background-color:#323232 ;margin-top: 20px;”>
<br><br>
<p style=“color: #C7C7C7” align=“center”><span style=“color: #556666;”>destiny@XuRuiBiao.com</span>&nbsp;&nbsp;xiaosha</p><br>
<p style=“color:#556666” align=“center” ;>XXXX公司注册</p><br>
<p style=“color:#C7C7C7;” align=“center”>客服电话:+86-xxxxxxx</p><br>
</div>
</nav>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

在其他任何需要这部分页面的地方直接引入即可:

<!--引入公共顶栏-->
<div th:replace="common::topbar"></div>
...
<!--引入公共底部栏-->
<div th:replace="common::bottombar"></div>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5

附:常用的语法语句

1、获取请求域、Session域里面的数据:

  • ${param.x} 返回名为x 的 request参数。(可能有多个值)
  • ${session.x} 返回名为x的Session参数。
  • ${application.x} 返回名为 servlet context 的参数。

2、日期的输出

  <span th:text="${#calendars.format(today,'dd MMMM yyyy')}">13 May 2011</span>

 
 
  • 1

3、循环

<tr th:each="prod : ${prods}">
     <td th:text="${prod.name}">Onions</td>
     <td th:text="${prod.price}">2.41</td>
     <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5

4、条件判断
if 和 unless

<a href="comments.html" th:href="@{/comments(prodId=${prod.id})}" th:unless="${#lists.isEmpty(prod.comments)}">view</a>
<a href="comments.html"  th:href="@{/product/comments(prodId=${prod.id})}"   th:if="${not #lists.isEmpty(prod.comments)}">view</a>

 
 
  • 1
  • 2

switch

<div th:switch="${user.role}">
	<p th:case="'admin'">User is an administrator</p>
	<p th:case="#{roles.manager}">User is a manager</p>
	<p th:case="*">User is some other thing</p> 
</div>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
                                </div>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于计算机专业的学生而言,参加各类比赛能够带来多方面的益处,具体包括但不限于以下几点: 技能提升: 参与比赛促使学生深入学习和掌握计算机领域的专业知识与技能,如编程语言、算法设计、软件工程、网络安全等。 比赛通常涉及实际问题的解决,有助于将理论知识应用于实践中,增强问题解决能力。 实践经验: 大多数比赛都要求参赛者设计并实现解决方案,这提供了宝贵的动手操作机会,有助于积累项目经验。 实践经验对于计算机专业的学生尤为重要,因为雇主往往更青睐有实际项目背景的候选人。 团队合作: 许多比赛鼓励团队协作,这有助于培养学生的团队精神、沟通技巧和领导能力。 团队合作还能促进学生之间的知识共享和思维碰撞,有助于形成更全面的解决方案。 职业发展: 获奖经历可以显著增强简历的吸引力,为求职或继续深造提供有力支持。 某些比赛可能直接与企业合作,提供实习、工作机会或奖学金,为学生的职业生涯打开更多门路。 网络拓展: 比赛是结识同行业人才的好机会,可以帮助学生建立行业联系,这对于未来的职业发展非常重要。 奖金与荣誉: 许多比赛提供奖金或奖品,这不仅能给予学生经济上的奖励,还能增强其成就感和自信心。 荣誉证书或奖状可以证明学生的成就,对个人品牌建设有积极作用。 创新与研究: 参加比赛可以激发学生的创新思维,推动科研项目的开展,有时甚至能促成学术论文的发表。 个人成长: 在准备和参加比赛的过程中,学生将面临压力与挑战,这有助于培养良好的心理素质和抗压能力。 自我挑战和克服困难的经历对个人成长有着深远的影响。 综上所述,参加计算机领域的比赛对于学生来说是一个全面发展的平台,不仅可以提升专业技能,还能增强团队协作、沟通、解决问题的能力,并为未来的职业生涯奠定坚实的基础。
很高兴回答您的问题!使用Spring BootThymeleaf和MyBatis开发员工管理系统是一个不错的选择。下面是一些建议的步骤: 1. 配置Spring Boot项目:创建一个新的Spring Boot项目并配置好相关的依赖项,如Spring MVC、Thymeleaf、MyBatis等。 2. 设计数据库模型:根据您的业务需求,设计合适的数据库模型来存储员工信息。可以使用MySQL或其他关系型数据库。 3. 创建实体类:在Java代码中创建与数据库表对应的实体类,为员工信息定义属性和相关方法。 4. 实现数据访问层:创建数据访问层接口,并使用MyBatis提供的注解或XML文件来编写SQL语句。这样可以与数据库进行交互,执行增删改查等操作。 5. 创建服务层:编写服务层代码,处理业务逻辑,例如员工的增删改查、数据校验等。 6. 编写控制器:使用Spring MVC创建控制器类,处理来自用户的请求,并调用相应的服务层方法。 7. 创建前端页面:使用Thymeleaf模板引擎创建员工管理系统的前端页面。通过Thymeleaf可以方便地将后端数据渲染到前端页面上。 8. 实现用户界面交互:使用HTML、CSS和JavaScript等技术,为员工管理系统创建用户友好的界面,实现增删改查等交互操作。 9. 部署和测试:将项目部署到服务器上,并进行测试和调试,确保系统的正常运行。 这只是一个简单的开发流程示例,您可以根据具体需求进行调整和扩展。希望对您有所帮助!如果有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值