上一章我把整个后台的搭建和逻辑给写出来了,也贴的相应的代码,这章节就来看看怎么使用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. ssh整合之二hibernate单独搭建

    1.首先我们需要去拷贝我们的hibernate所需的jar包  这里还需要加入我们C3P0的jar包,因为我们hibernate中使用的C3P0连接池 2. 编写我们的关系映射文件Customer.c ...

  2. redis中的aof模式,产生的是增量数据,还是全量数据?

    先说答案:全量数据. 1.修改redis.conf,开启rdb,禁用aof 上面这个是持久化文件的路径,我们ll看下: 2.启动redis后,cli查看里面的key [root@mini1 redis ...

  3. python开发:python字符串操作方法

    name = "my \tname is {name} and i am {year} old" capitalize:第一个单词的首字母大写的方法 print(name.capi ...

  4. golang-在gin中cookie跨域设置(配合ajax)

    1.当我在golang中,在前后端分离的情况下使用cookies时发现,跨域没有被允许.代码如下: func AccessJsMiddleware() gin.HandlerFunc { return ...

  5. Java 高级开发必修知识---反射

    Class类的使用 1) 在面向对象的世界里,万事万物皆对象 A. Java语言中,普通数据类型,静态成员不是对象,其他皆对象 B. 每一个类也是对象 C. 类是java.lang.Class类的实例 ...

  6. POJ-2996 Help Me with the Game---模拟棋子

    题目链接: https://vjudge.net/problem/POJ-2996 题目大意: 给出白方和黑方的棋子和对应的坐标,输出该副棋盘的样子 1,棋盘中大写字母表示的是白方棋子,小写是黑方.2 ...

  7. ecshop PC版本智能跳转到对应手机版页面

    以下适用于PC跳转到ectouch手机版的写法.其他手机端的方法类似. 修改文件 includes/lib_main.php 增加以下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  8. [LeetCode] Top K Frequent Words 前K个高频词

    Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...

  9. XML 解析默认去掉命名空间和注释

    //创建xml文档 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(@"C:\Users\Tony\Downloads\统一标准报文格 ...

  10. JavaScript数组操作总结

    以前特别相信自己的大脑,后来,再也不相信了!大脑是虚无的,重要的东西一定要让它有一个物质的具体的副本.事无巨细! 1.创建数组: new Array(); new Array(size); new A ...