上一章我把整个后台的搭建和逻辑给写出来了,也贴的相应的代码,这章节就来看看怎么使用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. Window7下安装Jmeter

    解压Jmeter,存放位置为D:\apache-jmeter-2.11 用户变量——>新建变量名JMETER_HOME,变量值为存放目录 系统变量——>添加;%JMETER_HOME%/l ...

  2. 集智robot微信公众号开发笔记

    开发流程 公众号基本配置(首先得有公众平台账号) 在开发菜单的基本配置中填写好基本配置项 首先配置服务器地址.Token.和消息加密密钥(地址为开发者为微信验证留的接口.token可以随便填写,只要在 ...

  3. c语言文件中关于while(!feof(fp)) 循环多输出一次的问题

      文件中关于while(!feof(fp)) 循环多输出一次的问题   feof(fp)有两个返回值:如果遇到文件结束,函数feof(fp)的值为1,否则为0.   当读到文件末尾时,文件指针并没有 ...

  4. 区块链3.0:拥抱EOS

    EOS是当下最火的区块链技术,被社会广泛看好为下一代区块链3.0.不同于以太坊的学习,EOS的主语言是C++,本文作为EOS研究的首篇文章,重点介绍EOS的创新点,它的周边生态,各种概念原理的解释,以 ...

  5. WPF Uri

    场景:自定义控件Generic.xaml样式引用资源字典Dictionary1.xaml. 方式:绝对路径. 方式1: <ResourceDictionary> <ResourceD ...

  6. 如何在jenkins的maven项目中,用mvn命令行指定findbugs的黑名单规则文件

    一:问题背景 最近在研究jenkins的过程中,针对maven项目,打算添加findbugs进行静态检查,但我不太想在项目的pom中进行修改,最好可以只修改jenkins的job配置,即配置外部化. ...

  7. 走在spring的路上。。。。

    一些spring的概念理解: 1.为什么需要spring? spring与我们平时用的工厂模式最大的差别在于,工厂模式设计还需要单独去建一个工厂类并去维护它, 而spring可只通过配置文件便可创建并 ...

  8. python3全栈开发- 元类metaclass(面试必考题)

    一.知识储备 #exec:三个参数 #参数一:字符串形式的命令 #参数二:全局作用域(字典形式),如果不指定,默认为globals() #参数三:局部作用域(字典形式),如果不指定,默认为locals ...

  9. Evensgn 的债务

    问题 A: Evensgn 的债务 大致题意:a欠b5元,b欠c5元,那么最小债务总额为a欠c5元,给你关系,求最小债务总额! 不想说话...一句超级大水题,我居然没读懂!!差点想到网络流了...其实 ...

  10. 【USACO】AC自动机

    Description 对,这就是裸的AC自动机. 要求:在规定时间内统计出模版字符串在文本中出现的次数. Input 第一行:模版字符串的个数N. 第2->N+1行:N个字符串.(每个模版字符 ...