Spring boot 整合 Mybatis + Thymeleaf開發web(二)

  上一章我把整個後臺的搭建和邏輯給寫出來了,也貼的相應的代碼,這章節就來看看怎麼使用Thymeleaf模板引擎吧,Spring Boot默認推薦Thymeleaf模板,以前是用jsp來做爲視圖層的渲染,可是Spring Boot對jsp的支持並很差,因此我仍是跟着Spring老大哥的指引走吧,錯也錯不到哪裏去!javascript

  

  在pox.xml裏面增長css

<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。html

  目錄結構java

 

  list.htmlweb

<!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類spring

 

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

 

  注意:json

    一、在須要返回模板的Controller類,使用@Controller註解,不要使用@RestController,返回值填寫 對應的路徑名稱。app

     二、 在不須要返回模板的狀況使用 @RestController,並在方法上添加@ResponseBody註解  以下。jsp

 

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/listide

 

   版權聲明:本文爲博主原創文章,未經博主容許不得轉載。 

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

相關文章
相關標籤/搜索