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. 决策树(二):后剪枝,连续值处理,数据加载器:DataLoader和模型评估

    在上一篇文章中,我们实现了树的构造,在下面的内容中,我们将中心放在以下几个方面 1.剪枝 2.连续值处理 3.数据加载器:DataLoader 4.模型评估 一,后剪枝 • 为什么剪枝  –" ...

  2. Spring MVC文件请求处理详解:MultipartResolver

    org.springframework.web.multipart.MultipartResolver是Spring-Web针对RFC1867实现的多文件上传解决策略. 1 使用场景 前端上传文件时, ...

  3. SpringCloud Alibaba(三) - GateWay网关

    1.基本环境搭建 1.1 依赖 <!-- Gatway 网关会和springMvc冲突,不能添加web依赖 --> <dependency> <groupId>or ...

  4. day32 6 请求转发与重定向的区别、session会话对象 & cookie & 8 应用程序上下文对象ServletContext & 5 请求转发与jsp页面内置对象

    1 请求转发与重定向的区别 2 session与cookie的区别 3 过滤器与监听器的区别 4 web-inf目录 web-inf目录是安全目录,无法从客户端访问,只能通过(服务端的)servlet ...

  5. Spring Security(8)

    您好,我是湘王,这是我的博客园,欢迎您来,欢迎您再来- 之前虽然实现了角色和权限之间的简单配对,但是如果每一个角色都要重新来过一次,就有点呆板了.如果能够配置一个「角色模板」,再通过这个模板来配置其他 ...

  6. 2.6:Python数据存取-文件、文件夹及目录、数据库

    一.Python文件读写 1.文件的打开模式 <class '_io.TextIOWrapper'>和<class '_io.BufferedReader'>.python使用 ...

  7. JavaEE Day11 BootStrap

    之前:前端知识 HTML+CSS+JavaScript           不好写 今日内容:前端的开发框架,内部定义了丰富的CSS样式和JS代码,只需要拿来用就行 会用即可 一.BootStrap ...

  8. Spring02:注解IOC、DBUtils单表CRUD、与Junit整合

    今日内容:基于注解的IOC及IOC的案例 Spring中IOC的常用注解 案例-使用xml方式和注解方式实现单表的CRUD操作 持久层技术选型:DBUtils 改造基于注解的IOC案例,使用纯注解的方 ...

  9. python3 利用当前时间、随机数产生一个唯一的数字作为文件名

    一.python3 利用当前时间.随机数产生一个唯一的数字作为文件名 代码如下: #-*-coding:utf-8-*- #python3自动生成文件名 from datetime import * ...

  10. forms组件源码剖析

    一:forms组件源码剖析 1.forms组件源码切入点: 1.0 form_obj.is_valid() 2.0 def is_valid(self): """ Ret ...