Spring boot 整合 Mybatis + Thymeleaf开发web(二)
上一章我把整个后台的搭建和逻辑给写出来了,也贴的相应的代码,这章节就来看看怎么使用Thymeleaf模板引擎吧,Spring Boot默认推荐Thymeleaf模板,之前是用jsp来作为视图层的渲染,但是Spring Boot对jsp的支持并不好,所以我还是跟着Spring老大哥的指引走吧,错也错不到哪里去!
在pox.xml里面增加
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
在resources下面的templates文件夹创建一个list.html, Spring Bootd 目录结构templates是模板文件存放地址,static为静态文件存放地址如js、css、image。
目录结构

list.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用户列表</title>
<link rel="stylesheet" type="text/css" th:href="@{/layui/css/layui.css}" media="all">
<script type="text/javascript" th:src="@{/layui/layui.js}"></script>
</head>
<script>
layui.use('table', function(){
var table = layui.table;
table.render({
elem: '#list'
,url:'/rest/find'
,cols:[([[)]{
checkbox: true,
fixed: true,
width:'5%'
},
{field:'name', width:'25%', align: 'center',title: '昵称', sort: true}
,{field:'author', width:'25%', align: 'center',title: '用户名'}
,{field:'price', width:'25.2%', align: 'center',title: '价格', sort: true}
[(]])]
,page: true,
height: 'auto'
});
});
</script> <body >
<h1>用户列表</h1>
<div style="width: 900px">
<table class="layui-table" lay-size="lg" lay-filter="demo">
<thead>
<tr>
<th>昵称</th>
<th>加入时间</th>
<th>签名</th>
<th>操作</th>
</tr>
</thead>
<tbody th:each="user:${users}" >
<tr>
<td th:text="${user.name}"></td>
<td th:text="${user.author}"></td>
<td th:text="${user.price}"></td>
</tr>
</tbody>
</table> <table class="layui-hide" id="list" lay-filter="demo"></table>
</div> </body>
</html>
TestController类
@Controller
@RequestMapping(value="/demo")
public class TestController { @Autowired
private BookBean bookBean; @Autowired
private BookBeanService bookBeanService; @RequestMapping(value = "/list")
public String getListUser(Model model){
try {
List<BookBean> findBookBeanInfo = bookBeanService.findBookBeanInfo();
model.addAttribute("users",findBookBeanInfo);
} catch (Exception e) {
e.printStackTrace();
}
return "/user/list";
}
}
注意:
1、在需要返回模板的Controller类,使用@Controller注解,不要使用@RestController,返回值填写 对应的路径名称。
2、 在不需要返回模板的情况使用 @RestController,并在方法上添加@ResponseBody注解 如下。
package com.example.demo.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.bean.BookBean;
import com.example.demo.service.BookBeanService; @RestController
@RequestMapping(value="/rest")
public class FindListController {
@Autowired
private BookBean bookBean; @Autowired
private BookBeanService bookBeanService; /**
* 查询
* @return
*/
@RequestMapping(value="/find")
@ResponseBody
public String add(){
JSONObject result = new JSONObject();
try {
System.out.println(1);
List<BookBean> findBookBeanInfo = bookBeanService.findBookBeanInfo();
String jsonString = JSONObject.toJSONString(findBookBeanInfo);
JSONArray array = JSON.parseArray(jsonString);
result.put("data",array);
result.put("code", 0);
result.put("msg", "");
result.put("count", 10);
System.out.println(result.toString());
} catch (Exception e) {
e.printStackTrace();
}
return result.toString();
}
}
启动程序 在地址栏输入http://localhost:8082/demo/list

版权声明:本文为博主原创文章,未经博主允许不得转载。
http://www.cnblogs.com/tangyin/p/8968150.html
Spring boot 整合 Mybatis + Thymeleaf开发web(二)的更多相关文章
- Spring Boot 2.X(三):使用 Spring MVC + MyBatis + Thymeleaf 开发 web 应用
前言 Spring MVC 是构建在 Servlet API 上的原生框架,并从一开始就包含在 Spring 框架中.本文主要通过简述 Spring MVC 的架构及分析,并用 Spring Boot ...
- Spring Boot入门(四):开发Web Api接口常用注解总结
本系列博客记录自己学习Spring Boot的历程,如帮助到你,不胜荣幸,如有错误,欢迎指正! 在程序员的日常工作中,Web开发应该是占比很重的一部分,至少我工作以来,开发的系统基本都是Web端访问的 ...
- Spring Boot整合Mybatis并完成CRUD操作
MyBatis 是一款优秀的持久层框架,被各大互联网公司使用,本文使用Spring Boot整合Mybatis,并完成CRUD操作. 为什么要使用Mybatis?我们需要掌握Mybatis吗? 说的官 ...
- Spring Boot整合MyBatis(非注解版)
Spring Boot整合MyBatis(非注解版),开发时采用的时IDEA,JDK1.8 直接上图: 文件夹不存在,创建一个新的路径文件夹 创建完成目录结构如下: 本人第一步习惯先把需要的包结构创建 ...
- Spring Boot系列(三):Spring Boot整合Mybatis源码解析
一.Mybatis回顾 1.MyBatis介绍 Mybatis是一个半ORM框架,它使用简单的 XML 或注解用于配置和原始映射,将接口和Java的POJOs(普通的Java 对象)映射成数据库中的记 ...
- 太妙了!Spring boot 整合 Mybatis Druid,还能配置监控?
Spring boot 整合 Mybatis Druid并配置监控 添加依赖 <!--druid--> <dependency> <groupId>com.alib ...
- spring boot 整合 mybatis 以及原理
同上一篇文章一样,spring boot 整合 mybatis过程中没有看见SqlSessionFactory,sqlsession(sqlsessionTemplate),就连在spring框架整合 ...
- Spring Boot 整合mybatis时遇到的mapper接口不能注入的问题
现实情况是这样的,因为在练习spring boot整合mybatis,所以自己新建了个项目做测试,可是在idea里面mapper接口注入报错,后来百度查询了下,把idea的注入等级设置为了warnin ...
- Spring Boot整合Mybatis报错InstantiationException: tk.mybatis.mapper.provider.base.BaseSelectProvider
Spring Boot整合Mybatis时一直报错 后来发现原来主配置类上的MapperScan导错了包 由于我使用了通用Mapper,所以应该导入通用mapper这个包
随机推荐
- 表单提交中的input、button、submit的区别
1.input[type=submit] 我们直接来看例子: 代码如下: <form> <input name="name"> <input type ...
- 日推20单词 Day03
1.occur v. 发生,发现 2.harvest n.收获,丰收 vt.收割,得到 3.crop n.庄稼,收成 4.yield n.产量 v.产出,屈服 5.field n.田野 6.featu ...
- oracle批量插入优化方案
今天听DBA说如果从一个表批量查询出一批数据之后批量插入另外一张表的优化方案: 1)不写归档日志: 2)采用独占 关于insert /*+ append */我们需要注意以下三点: a.非归档模式下, ...
- 28.C++- 单例类模板(详解)
单例类 描述 指在整个系统生命期中,一个类最多只能有一个实例(instance)存在,使得该实例的唯一性(实例是指一个对象指针) , 比如:统计在线人数 在单例类里,又分为了懒汉式和饿汉式,它们的区 ...
- 《跟我学Shiro》学习笔记 第一章:Shiro简介
前言 现在在学习Shiro,参照着张开涛老师的博客进行学习,然后自己写博客记录一下学习中的知识点,一来可以加深理解,二来以后遗忘了可以查阅.没有学习过Shiro的小伙伴,也可以和我一起学习,大家共同进 ...
- p2p项目总结
1.关于ajax请求所要注意的地方:$.psot(url,json,callback,type) (1)url路径问题,在html中写绝对路径不能用EL表达式,EL表达式只能在jsp中使用 (2)js ...
- [LeetCode] Non-decreasing Array 非递减数列
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
- [HNOI2010]STONE取石头游戏
题目描述 A 公司正在举办一个智力双人游戏比赛----取石子游戏,游戏的获胜者将会获得 A 公司提供的丰厚奖金,因此吸引了来自全国各地的许多聪明的选手前来参加比赛. 与经典的取石子游戏相比,A公司举办 ...
- [HNOI2016]大数
题目描述 小 B 有一个很大的数 S,长度达到了 N 位:这个数可以看成是一个串,它可能有前导 0,例如00009312345.小B还有一个素数P.现在,小 B 提出了 M 个询问,每个询问求 S 的 ...
- bzoj 1058: [ZJOI2007]报表统计
Description 小Q的妈妈是一个出纳,经常需要做一些统计报表的工作.今天是妈妈的生日,小Q希望可以帮妈妈分担一些工 作,作为她的生日礼物之一.经过仔细观察,小Q发现统计一张报表实际上是维护一个 ...