别再写jsp了,Thymeleaf它不香吗?

2023-11-10 01:30
文章标签 jsp thymeleaf 不香

本文主要是介绍别再写jsp了,Thymeleaf它不香吗?,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. 啥是 Thymeleaf

在学 Thymeleaf 之前我们先看一下使用 jsp 开发遇到的主要问题:

jsp 的痛点

1.页面包含大量 java 代码,代码太混乱

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html><head><title>jsp</title></head><body><%String name = (String)request.getAttribute("name");int age = (int)request.getAttribute("age");%><p>姓名: <%=name%></p><p>年龄: <%=age%></p></body>
</html>

2.jsp 技术已经是很多年前的老技术了,现在的主流框架都不推荐使用,基本被淘汰了。

模板引擎技术

虽然 jsp 基本被淘汰了,但是它的技术替代品如雨后春笋,层不出穷。

模板引擎技术就是其中的代表。

我们都知道传统的页⾯开发通常采⽤ HTML+ JS 技术,⽽现在⼤部分⽹站都采⽤标签化 + 模块化的设计。

模板引擎技术就是根据这种⽅式,使⽤户界⾯与业务数据分离⽽产⽣的。

它可以⽣成特定格式的⽂档,⽤于⽹站的模板引擎就会⽣成⼀个标准的 HTML ⽂档在原有的 HTML 页⾯中来填充数据,最终达到渲染页⾯的⽬的。

模板引擎技术说白了就是把数据和页⾯整合在⼀起的技术。

Thymeleaf 简介

Thymeleaf 是一种可以替代 jsp 的模板引擎技术。它有如下优点:

1.与 SpringBoot 完美整合:SpringBoot 提供了 Thymeleaf 的默认配置,我们可以像以前操作 jsp 一样来操作 Thymeleaf。

2.开箱即用:支持 JSTL 格式语法。

3.多方言支持:Thymeleaf 提供 spring 标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、国际化等功能。

2. 环境搭建

这里我们创建 springboot 项目。

1.引入依赖

	<dependencies><!-- thymeleaf --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><!-- web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><!-- test --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

2.Thymeleaf 配置

application.yml

server:port: 8081servlet:context-path: /thymeleaf-demospring:thymeleaf:# thymeleaf 配置cache: false # 禁用缓存,修改完实时展示数据prefix: classpath:/templates/ # 文件所在位置suffix: .html # 后缀web:resources:static-locations: classpath:/static/ # 静态资源

在 springboot 的配置文件中,我们配置了 thymeleaf 的默认前缀和后缀。

还配置了静态资源的访问路径。

3.html 文件引入 Thymeleaf 的命名空间

xmlns:th="http://www.thymeleaf.org"

html 文件必须引入 thymeleaf 的命名空间才能使用相关语法。

3. 常用语法

1.th:text

作用:如果变量有值,则替换标签里面的默认值,否则展示标签的默认值。

例如:

@GetMapping("hello")
public String hello(Model model) {model.addAttribute("msg", "黄沙百战穿金甲,不破楼兰终不还!");return "hello";
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>msg</title>
</head>
<body>
<p>-------------诗句赏析-------------</p>
<p th:text="${msg}">人生得意须尽欢,莫使金樽空对月。</p>
</body>
</html>

测试: 

2.th:href

作用:用来指向文件的 url。

例如:

<link rel="stylesheet" type="text/css" th:href="@{css/login.css}"/>

3.th:action

作用:用来配置 form 表单的请求路径

th:action="@{/login}"

例如:

<form th:action="@{/login}" method="post">姓名: <input type="text" name="username"/>密码: <input type="password" name="password"/><input type="submit" class="button" value="登录"/>
</form>

4.th:if

作用:条件判断,如果判断为真就显示数据或者执行相关操作。

例如:

<p th:if="${username!=null && username!=''}"> 欢迎你: <span th:text="${username}"></span></p>

5.th:src

作用:用来指向文件的 url。

例如:

<img th:src="@{images/title.jpg}" >

6.th:onclick

作用:绑定事件。

例如:

<a href="javascript:;" th:onclick="'deleteUser('+${user.id}+')'">删除</a>

4. 小案例

场景:用户登录成功之后,展示用户信息和用户列表。

 

controller

/* 进入登录界面 */
@GetMapping("/toLogin")
public String toLogin() {return "login";
}@PostMapping("/login")
public String login(Model model, String username, String password) {// 用户名model.addAttribute("username", username);// 用户列表信息List<User> userList = Stream.of(new User(1, "张三", 18), new User(2, "李四", 19), new User(3, "王五", 20)).collect(Collectors.toList());model.addAttribute("userList", userList);return "index";
}

login.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><link rel="stylesheet" type="text/css" th:href="@{css/login.css}"/><title>登录界面</title>
</head>
<body>
<p class="login_title">登录页面</p>
<form th:action="@{/login}" method="post">姓名: <input type="text" name="username"/>密码: <input type="password" name="password"/><input type="submit" class="button" value="登录"/>
</form>
</body>
</html>

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>用户列表</title>
</head>
<body>
<p th:if="${username!=null && username!=''}"> 欢迎你: <span th:text="${username}"></span></p>
<img th:src="@{images/title.jpg}">
<p>-------------用户列表-------------</p>
<table class="table" border><tr><td> ID </td><td> 姓名 </td><td> 年龄 </td><td> 操作 </td></tr><tr th:each="user:${userList}"><td><span th:text="${user.id}"></span></td><td><span th:text="${user.name}"></span></td><td><span th:text="${user.age}"></span></td><td><a href="javascript:;" th:onclick="'deleteUser('+${user.id}+')'">删除</a></td></tr>
</table>
<script>function deleteUser(id) {console.log("id: " + id);}
</script>
</body>
</html>

这篇关于别再写jsp了,Thymeleaf它不香吗?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/379639

相关文章

SpringBoot如何访问jsp页面

《SpringBoot如何访问jsp页面》本文介绍了如何在SpringBoot项目中进行Web开发,包括创建项目、配置文件、添加依赖、控制层修改、测试效果以及在IDEA中进行配置的详细步骤... 目录SpringBoot如何访问JSP页python面简介实现步骤1. 首先创建的项目一定要是web项目2. 在

Thymeleaf:生成静态文件及异常处理java.lang.NoClassDefFoundError: ognl/PropertyAccessor

我们需要引入包: <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>sp

JSP 简单表单显示例子

<html><!--http://localhost:8080/test_jsp/input.html --><head><meta http-equiv="Content-Type" content="text/HTML; charset=utf-8"><title>input页面</title></head><body><form action="input.jsp" method

基于JSP的实验室管理系统

你好呀,我是计算机学姐码农小野!如果有相关需求,可以私信联系我。 开发语言:Java 数据库:MySQL 技术:JSP技术 + Spring Boot框架 工具:IDEA/Eclipse、Navicat、Tomcat 系统展示 首页 用户个人中心 实验室管理 设备报备管理 摘要 随着社会的发展和科学技术的进步,互联网技术越来越受欢迎。网络计算机

在项目开发中,jsp页面不会少了,如何公用页面(添加页面和修改页面)和公用样式代码(css,js)?

在项目开发中,如何公用添加页面和修改页面? <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><html><head><title>岗位设置</title><%@ include file="/WEB-INF/jsp/public/common.jspf"%></head><body> <!-- 标

通过js得到时间,并显示到jsp上

代码: 部分jsp代码: <div id="tt" style="height:60px;border: 0px;padding-top: 5px;padding-left:5px;"> <div style="float:left;">          <input id="startdate" style="width:120px;" editable="false" class="

Spring Boot学习记录-–Thymeleaf模板

自从来公司后都没用过jsp当界面渲染了,因为前后端分离不是很好,反而模板引擎用的比较多,thymeleaf最大的优势后缀为html,就是只需要浏览器就可以展现页面了,还有就是thymeleaf可以很好的和spring集成.下面开始学习. 1.引入依赖 maven中直接引入 <dependency><groupId>org.springframework.boot</groupI

请解释JSP中的九大内置对象及其作用。什么是Java Web中的请求转发和重定向?它们有什么区别?

请解释JSP中的九大内置对象及其作用。 JSP(JavaServer Pages)中的九大内置对象(也称为隐式对象或自动对象)是JSP容器为每个页面提供的Java对象,这些对象在JSP页面被转换成Servlet时自动可用,无需显式声明。这些对象极大地简化了JSP页面的开发,因为它们提供了对Web应用程序中常见功能的直接访问。以下是九大内置对象及其作用的详细解释: request:javax.

JSP的增删改查part2

增加显示数据库表格cdsn的功能 1. 》》对CdsnDao接口和方法,CdsnService接口和方法进行处理,并增加CdsnServlet用于对新建展示页面进行处理 对cdsnDao接口和方法增加 》》接口 //获取cdsn用户数据列表 public List<cdsn> getCdsnList();》》CdsnDaoImpl增加内容//获得数据库所有数据publ

JSP的增删改查part1

运用Myeclisp对数据库进行增删改查 要建立6个库 1).其中dao层用与连接数据库和对数据库进行相关操作; 2).entity层用于存放数据库连接后的实体数据; 3.)service层是在mcv三层模式中新添加一层,能够更加清晰的定义应用程序的边界,需要操作数据的时候,通过service层访问DAO层来实现。