上一章我把整个后台的搭建和逻辑给写出来了,也贴的相应的代码,这章节就来看看怎么使用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(二)的更多相关文章

  1. Spring Boot 2.X(三):使用 Spring MVC + MyBatis + Thymeleaf 开发 web 应用

    前言 Spring MVC 是构建在 Servlet API 上的原生框架,并从一开始就包含在 Spring 框架中.本文主要通过简述 Spring MVC 的架构及分析,并用 Spring Boot ...

  2. Spring Boot入门(四):开发Web Api接口常用注解总结

    本系列博客记录自己学习Spring Boot的历程,如帮助到你,不胜荣幸,如有错误,欢迎指正! 在程序员的日常工作中,Web开发应该是占比很重的一部分,至少我工作以来,开发的系统基本都是Web端访问的 ...

  3. Spring Boot整合Mybatis并完成CRUD操作

    MyBatis 是一款优秀的持久层框架,被各大互联网公司使用,本文使用Spring Boot整合Mybatis,并完成CRUD操作. 为什么要使用Mybatis?我们需要掌握Mybatis吗? 说的官 ...

  4. Spring Boot整合MyBatis(非注解版)

    Spring Boot整合MyBatis(非注解版),开发时采用的时IDEA,JDK1.8 直接上图: 文件夹不存在,创建一个新的路径文件夹 创建完成目录结构如下: 本人第一步习惯先把需要的包结构创建 ...

  5. Spring Boot系列(三):Spring Boot整合Mybatis源码解析

    一.Mybatis回顾 1.MyBatis介绍 Mybatis是一个半ORM框架,它使用简单的 XML 或注解用于配置和原始映射,将接口和Java的POJOs(普通的Java 对象)映射成数据库中的记 ...

  6. 太妙了!Spring boot 整合 Mybatis Druid,还能配置监控?

    Spring boot 整合 Mybatis Druid并配置监控 添加依赖 <!--druid--> <dependency> <groupId>com.alib ...

  7. spring boot 整合 mybatis 以及原理

    同上一篇文章一样,spring boot 整合 mybatis过程中没有看见SqlSessionFactory,sqlsession(sqlsessionTemplate),就连在spring框架整合 ...

  8. Spring Boot 整合mybatis时遇到的mapper接口不能注入的问题

    现实情况是这样的,因为在练习spring boot整合mybatis,所以自己新建了个项目做测试,可是在idea里面mapper接口注入报错,后来百度查询了下,把idea的注入等级设置为了warnin ...

  9. Spring Boot整合Mybatis报错InstantiationException: tk.mybatis.mapper.provider.base.BaseSelectProvider

    Spring Boot整合Mybatis时一直报错 后来发现原来主配置类上的MapperScan导错了包 由于我使用了通用Mapper,所以应该导入通用mapper这个包

随机推荐

  1. mysql(1)—— 详解一条sql语句的执行过程

    SQL是一套标准,全称结构化查询语言,是用来完成和数据库之间的通信的编程语言,SQL语言是脚本语言,直接运行在数据库上.同时,SQL语句与数据在数据库上的存储方式无关,只是不同的数据库对于同一条SQL ...

  2. Spring 4.2.5 + Quartz 2.2.0整合

    jar包使用的Maven库管理的,在这就不罗列了,注意下只有spring3.x以上的版本才支持quartz2.x的版本. 配置文件: <?xml version="1.0" ...

  3. SpringMVC(九):SpringMVC 处理输出模型数据之ModelAndView

    Spring MVC提供了以下几种途径输出模型数据: 1)ModelAndView:处理方法返回值类型为ModelAndView时,方法体即可通过该对象添加模型数据: 2)Map及Model:处理方法 ...

  4. POJ-1995 Raising Modulo Numbers---快速幂模板

    题目链接: https://vjudge.net/problem/POJ-1995 题目大意: 求一堆ab的和模上m 思路: 直接上模板 #include<iostream> #inclu ...

  5. ord()与char()

    >>> ord('王')#获取字符编码 29579 >>> chr(29579)#把编码转成对应的字符 '王'

  6. MySQL表的创建

    第1步:设计 首先要设计一张用于我想要用途的表,例如如下用于描述个人的信息类型: 姓名: 性别: 出生日期: 地址: 最喜爱的食物. 下面为他来指定列和数据类型: 列 | 类型 | 允许值 | - | ...

  7. 使用C# (.NET Core) 实现适配器模式 (Adapter Pattern) 和外观模式 (Facade Pattern)

    本文的概念内容来自深入浅出设计模式一书 现实世界中的适配器(模式) 我带着一个国标插头的笔记本电脑, 来到欧洲, 想插入到欧洲标准的墙壁插座里面, 就需要用中间这个电源适配器. 面向对象的适配器 你有 ...

  8. [ABP]浅谈模块系统与 ABP 框架初始化

    在 ABP 框架当中所有库以及项目都是以模块的形式存在,所有模块都是继承自AbpModule 这个抽象基类,每个模块都拥有四个生命周期.分别是: PreInitialze(); Initialize( ...

  9. TensorFlow学习笔记(MNIST报错修正 适用Tensorflow1.3)

    在Tensorflow实战Google框架下的深度学习这本书的MNIST的图像识别例子中,每次都要报错   错误如下: Only call `sparse_softmax_cross_entropy_ ...

  10. codeforces 815C Karen and Supermarket

    On the way home, Karen decided to stop by the supermarket to buy some groceries. She needs to buy a ...