Spring boot integrates Mybatis + Thymeleaf to develop web (2)

  In the previous chapter, I wrote the entire background construction and logic, and also posted the corresponding code. In this chapter, let’s see how to use the Thymeleaf template engine. Spring Boot recommends the Thymeleaf template by default. Before, jsp was used as the view. Layer rendering, but Spring Boot's support for jsp is not good, so I still follow the guidance of Spring's big brother, and I can't go wrong!

  

  Add in 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>

 

  Create a list.html in the templates folder under resources, Spring Bootd directory structure templates is the template file storage address, static is the static file storage address such as js, css, image.

  Directory Structure

 

  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: ' username ' }
              ,{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 class

 

@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";
    }
}

 

  Notice:

    1. In the Controller class that needs to return the template, use the @Controller annotation instead of @RestController, and fill in the corresponding path name for the return value.

     2. Use @RestController without returning a template, and add the @ResponseBody annotation to the method as follows.

 

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;
    
    
    /**
     * Inquire
     * @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();
    }
}

 

 

  Start the program and enter http://localhost:8082/demo/list in the address bar

 

   Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission. 

   http://www.cnblogs.com/tangyin/p/8968150.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325003097&siteId=291194637