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

    1.环境 操作系统:Win10专业版(64位) 数据库:Oracle 12c Release 2(Version 12.2.0.1.0,64位) 2.下载Oracle12c oracle官网下载地址: ...

  2. python的单元测试

    单元测试实际上就是一些"断言"(assert)代码 断言就是判断一个函数或对象的一个方法所产生的结果是否符合你期望的那个结果. python中assert断言是声明布尔值为真的判定 ...

  3. Java-Maven(四):Eclipse集成Maven环境配置

    一般maven都需要集成到IDE上使用的,而不是单独的使用,常见的maven可集成IDE:eclipse.IntelliJ IDEA.但这里就只学习eclipse集成maven的基础上,进行maven ...

  4. [论文阅读] A Discriminative Feature Learning Approach for Deep Face Recognition (Center Loss)

    原文: A Discriminative Feature Learning Approach for Deep Face Recognition 用于人脸识别的center loss. 1)同时学习每 ...

  5. Collection集合框架详解

    [Java的集合框架] 接口: collection      map list     set 实现类: ArryList   HashSet HashMap LinkList   LinkHash ...

  6. @OnetoOne @OnetoMany @ManyToOne(2)

    在班主任(id,name,bjid) 班级(id name) 学生(id name bjid)的 关系中 班主任一对一关联班级 班级一对多关联学生 @OnetoOne @joinColumn(bjid ...

  7. 在Unity3D中利用 RenderTexture 实现游戏内截图

    using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; publ ...

  8. Java中数据表的建立

    class Emp{ private int empno;//职工编号 private String ename;//姓名 private String job;//职位 private double ...

  9. kafka知识体系-kafka设计和原理分析-kafka leader选举

    kafka leader选举 一条消息只有被ISR中的所有follower都从leader复制过去才会被认为已提交.这样就避免了部分数据被写进了leader,还没来得及被任何follower复制就宕机 ...

  10. 机器学习技法:14 Radial Basis Function Network

    Roadmap RBF Network Hypothesis RBF Network Learning k-Means Algorithm k-Means and RBF Network in Act ...