Thymeleaf的对象

Thymeleaf是直接支持访问Servlet web的原生资源,HttpServletRequest HttpServletResponse HttpSession ServletContext.

#request:获取HttpServletRequest对象
#response:获取HttpServletResponse 对象
#session:获取HttpSession 对象
#servletContext:获取ServletContext对象

1.Handler

@GetMapping("/servlet")
public String servlet(HttpServletRequest request){
request.setAttribute("valye","request");
request.getSession().setAttribute("valye","request");
request.getServletContext().setAttribute("valye","request");
return "test";
}
<!--request-->
<p th:text="${#request.getAttribute('value')}"></p>
<p th:text="${#session.getAttribute('value')}"></p>
<p th:text="${#servletContext.getAttribute('value')}"></p>
<p th:text="${#reponse}"></p>

Thymeleaf支持直接访问session,相对于

${#servletContext.getAttribute('value')}也可以简化为${value}

Thymeleaf的内置对象

  • dates;日期格式化
  • calendars:日期操作
  • numbers:数字格式化
  • Strings:字符格式化
  • bools:boolean
  • arrays:数组内置对象
  • lists:list集合内置对象
  • sets:set集合内置对象
  • maps;map集合内置对象

    @GetMapping("/uniltity")
public ModelAndView uniltity(){
ModelAndView modelAndView =new ModelAndView();
modelAndView.setViewName("test");
modelAndView.addObject("date",new Date());
Calendar calendar =Calendar.getInstance();
calendar.set(2020,1,1);
modelAndView.addObject("calendar",calendar);
modelAndView.addObject("number",0.06);
modelAndView.addObject("string","Springboot");
modelAndView.addObject("boolean",true);
modelAndView.addObject("array",Arrays.asList("张三","李四","王五"));
List<User> users = Arrays.asList(new User("张三",1),new User("李四",2),new User("王五",3));
modelAndView.addObject("list",users);
Set<User> set =new HashSet<>();
set.add(new User("张三",1));
set.add(new User("李四",2));
modelAndView.addObject("set",set);
Map<Integer,User> map=new HashMap<>();
map.put(1,new User("张三",1));
map.put(2,new User("李四",2));
modelAndView.addObject("map",map);
return modelAndView;
}
date:格式化<span th:text="${#dates.format(date,'yyy-mm-dd')}"></span><br>
当前时间:<span th:text="${#dates.cteateToday()}"></span><br>
Calendar格式化:<span th:text="${#calendars.format(calendar,'yyyy-mm-dd')}"></span><br>
number百分比格式化:<span th:text="${#numbers.formatPercent(number,2,2)}"></span><br>
name是否为空:<span th:text="${#strings.isTmpty(string)}"></span><br>
name的长度:<span th:text="${#strings.length(string)}"></span><br>
name的拼接:<span th:text="${#strings.concat('Good',string)}"></span><br>
boolen是否为true:<span th:text="${#bools.isTrue(boolean)}"></span><br>
arrys的长度:<span th:text="${#arrays.length(array)}"></span><br>
array是否包含张三:<span th:text="${#arrays.contains(array,'张三')}"></span><br>
list是否为空:<span th:text="${#list.isEmpty(list)}"></span><br>
list的长度:<span th:text="${#list.size(list)}"></span><br>
Set是否为空:<span th:text="${#sets.isEmpty(set)}"></span><br>
Set的长度:<span th:text="${#sets.size(set)}"></span><br>
Map是否为空:<span th:text="${#maps.isEmpty(map)}"></span><br>
Map是的长度:<span th:text="${#maps.size(map)}"></span><br>

Spring boot 整合JDBC Template

JDBC Template是spring自带的一个JDBC的模版组件,底层实现了对JDBC的封装,用法和Mybatis类似,需要开发者定义SQL语句,JDBC Template帮助我们完成数据库的连接,SQL的执行,结果集的封装

不足之处灵活性不如Mybatis,因为Mybatis是直接卸载XML王文建中,更有利于扩展和维护,JDBC Template是以硬编码的形式将SQL直接写在java代码中,不利于扩展和维护

1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId>
<artifactId>soringbootdo</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<artifactId>spring-boot-starter-web</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.2.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>

2.实体类:

package com.southwind.entity;

import lombok.Data;

@Data
public class User {
private Integer id;
private String name;
private Integer money;
}

3.Repository

接口:

package com.southwind.Repository;

import com.southwind.entity.User;

import java.util.List;

public interface UserRepository {
public List<User> findAll();
public User finById(Integer id);
public int save (User user);
public int update(User user);
public int delete(Integer id);
}
package com.southwind.Repository.impl;

import com.southwind.Repository.UserRepository;
import com.southwind.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository; import java.util.List;
@Repository
public class UserRepositoryImpl implements UserRepository {
@Autowired
private JdbcTemplate jdbcTemplate;
public List<User> findAll() {
return jdbcTemplate.query(
"select * from people",
new BeanPropertyRowMapper<>(User.class)
);
} public User finById(Integer id) { return jdbcTemplate.queryForObject(
"select * from people where id=?",
new Object[]{id},
new BeanPropertyRowMapper<>(User.class)
);
} public int save(User user) { return jdbcTemplate.update(
"insert into people(id,name,money) values (?,?,?)",
user.getId(),
user.getName(),
user.getMoney() );
} public int update(User user) { return jdbcTemplate.update(
"update people set name=?,money=? where id=?",
user.getName(),
user.getMoney(),
user.getId()
);
} public int delete(Integer id) { return jdbcTemplate.update(
"delete from people where id=?",
id );
}
}

4.Handeler

package com.southwind.Controller;

import com.southwind.Repository.UserRepository;
import com.southwind.Repository.impl.UserRepositoryImpl;
import com.southwind.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*; import java.util.List; @Controller
@RequestMapping("/user")
public class UserHandler {
@Autowired
private UserRepositoryImpl userRepository;
@GetMapping("/findall")
@ResponseBody
public List<User> findall(){
return userRepository.findAll();
} @GetMapping("/findbyid/{id}")
@ResponseBody
public User findbyid(@PathVariable("id")Integer id) {
return userRepository.finById(id);
} @PostMapping("/save")
@ResponseBody
public int save(@RequestBody User user){
return userRepository.save(user);
}
@PutMapping("/update")
@ResponseBody
public int update(@RequestBody User user){
return userRepository.update(user);
}
@DeleteMapping("/delete/{id}")
@ResponseBody
public int delete(@PathVariable Integer id){
return userRepository.delete(id);
}
}

方法

query:

public <T> List<T> query(String sql, RowMapper<T> rowMapper) throws DataAccessException {
return (List)result(this.query((String)sql, (ResultSetExtractor)(new RowMapperResultSetExtractor(rowMapper))));
}

RowMapper是一个接口,作用是分析结果集,将JDBC查询的ResultSet对象转化成对应的POJO

queryForObjiect(String sql,Object[] args, RowMapper rowMapper)

该方法查询一条数据,并将结果封装成一个POJO

update

public int update(String sql, @Nullable Object... args) throws DataAccessException {
return this.update(sql, this.newArgPreparedStatementSetter(args));
}

增加、修改、删除都可以用这个方法

Thymeleaf的内置对象、SpringBoot整合Thymeleaf和JDBC的更多相关文章

  1. Thymeleaf 之 内置对象、定义变量、URL参数及标签自定义属性

    Thymeleaf 之 内置对象.定义变量.URL参数及标签自定义属性 本文章来自[知识林] 如标题所述,这篇文章主要讲述Thymeleaf中的内置对象(list解析.日期格式化.数字格式化等).定义 ...

  2. thymeleaf 的内置对象

       

  3. Springboot整合thymeleaf模板

    Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用. Thymeleaf的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建 ...

  4. 【Springboot】Springboot整合Thymeleaf模板引擎

    Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...

  5. 03-03:springBoot 整合thymeleaf

    thymeleaf 语法详解1.变量输出: th:text :在页面中输出某个值 th:value :将一个值放到input标签中的value中.2.判断字符串是否为空 ①:调用内置对象一定要用# ② ...

  6. SpringBoot系列三:SpringBoot基本概念(统一父 pom 管理、SpringBoot 代码测试、启动注解分析、配置访问路径、使用内置对象、项目打包发布)

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.了解SpringBoot的基本概念 2.具体内容 在之前所建立的 SpringBoot 项目只是根据官方文档实现的一个基础程 ...

  7. 三、SpringBoot整合Thymeleaf视图

    目录 3.1 Thymeleaf视图介绍 3.2 创建SpringBoot项目 3.2 配置Thymeleaf 3.3 编写Demo 3.4 小结 3.1 Thymeleaf视图介绍 先看下官网的介绍 ...

  8. SpringBoot 整合 Thymeleaf & 如何使用后台模板快速搭建项目

    如果你和我一样,是一名 Java 道路上的编程男孩,其实我不太建议你花时间学 Thymeleaf,当然他的思想还是值得借鉴的.但是他的本质在我看来就是 Jsp 技术的翻版(Jsp 现在用的真的很少很少 ...

  9. SpringBoot 整合thymeleaf

    1.Thymeleaf介绍(官网推荐:https://www.thymeleaf.org/doc/articles/thymeleaf3migration.html) Thymeleaf是跟Veloc ...

  10. JavaWeb学习笔记--2.jsp指令元素、动作元素、内置对象

    目录: 一.指令 1.1 page指令 :page指令有13种属性 1.2 include指令:include指令有1种属性,为file.(注意<%@ include%>和<jsp: ...

随机推荐

  1. 绵阳2020CCPC补题

    绵阳2020CCPC D,K,J,L,G D. Defuse the Bombs 知识点:二分答案 复杂度:\(O(nlogn+log^2n)\) vp时我猜了一个结论,验了几个样例就写了,喜提WA3 ...

  2. Kafka事务原理剖析

    一.事务概览 提起事务,我们第一印象可能就是ACID,需要满足原子性.一致性.事务隔离级别等概念,那kafka的事务能做到什么程度呢?我们首先看一下如何使用事务 Producer端代码如下 Kafka ...

  3. 基于sklearn的集成学习实战

    集成学习投票法与bagging 投票法 sklearn提供了VotingRegressor和VotingClassifier两个投票方法.使用模型需要提供一个模型的列表,列表中每个模型采用tuple的 ...

  4. Halo 主题 Redemption 首发版

    Redemption 一款专注阅读.写作的 Halo 博客主题.主要设计思想即是专注阅读.写作,是一款极简类型的博客主题. Redemption 部分设计灵感借鉴 Halo 博客 Zozo 主题,感谢 ...

  5. 基于opencv实现简单人脸检测

    作用:在视频中自动检测出人脸 使用内容:灰度转换,分类器,矩形框选,圆形框选,摄像头读取及释放 代码逻辑: 先读取摄像头后, 对摄像头拍摄到的图片进行逐帧分析, 并对图像进行灰度转换后使用类选择器识别 ...

  6. 你不知道的Map家族中的那些冷门容器

    概述 本篇文章主要讲解下Map家族中3个相对冷门的容器,分别是WeakHashMap.EnumMap.IdentityHashMap, 想必大家在平时的工作中也很少用到,或者压根不知道他们的特性以及适 ...

  7. Flaks框架(Flask请求响应,session,闪现,请求扩展,中间件,蓝图)

    目录 一:Flask请求响应 1.请求相关信息 2.flask新手四件套 3.响应相关信息(响应response增加数据返回) 二:session 1.session与cookie简介 2.在使用se ...

  8. 基于Unet+opencv实现天空对象的分割、替换和美化

           传统图像处理算法进行"天空分割"存在精度问题且调参复杂,无法很好地应对云雾.阴霾等情况:本篇文章分享的"基于Unet+opencv实现天空对象的分割.替换和 ...

  9. .NET周报【12月第4期 2022-12-31】

    祝大家新年快乐! 国内文章 『 再看.NET7』数值类型 https://mp.weixin.qq.com/s/ctiBMPY6Hditk81AzHSRng 在C#中,有int16,用short来定义 ...

  10. [随笔所想] UBC学习生活经验分享

    当时受到了很多人的帮助,在网上也查到了很多经验帖子,比如如何办理签证,如何填写表格,要准备哪些材料以及生活上要带哪些物品,等等.当时就想到等我办理好这些,也一定和大家分享,为更多的人提供一些参考. 1 ...