web项目 crud 五 查询用户

  • 现在 “用户列表” 这些数据仍然还是前端人员提供的写死的数据,现在从后台进行获取数据然后进行动态显示
  • 本文的重点主要是数据在页面显示时用的是 Thymeleaf 进行操作,这是新内容,其余内容都是以前 Spring MVC 经常做的

  • 创建用户和部门实体

  • 用户实体
package com.xuxu.pojo;

import java.util.Date;

/**
 * 用户实体
 */
public class User {
    private Integer uId;
    private String uName;
    private String email;
    private Integer gender;
    private Date birth;
    private Department department;

    public User() {
    }

    public User(Integer uId, String uName, String email, Integer gender, Date birth, Department department) {
        this.uId = uId;
        this.uName = uName;
        this.email = email;
        this.gender = gender;
        this.birth = birth;
        this.department = department;
    }

    public Integer getuId() {
        return uId;
    }

    public void setuId(Integer uId) {
        this.uId = uId;
    }

    public String getuName() {
        return uName;
    }

    public void setuName(String uName) {
        this.uName = uName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Integer getGender() {
        return gender;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }
}
  • 部门实体
package com.xuxu.pojo;

/**
 * 部门实体
 */
public class Department {
    private Integer deptId;
    private String deptName;

    public Department() {
    }

    public Department(Integer deptId, String deptName) {
        this.deptId = deptId;
        this.deptName = deptName;
    }

    public Integer getDeptId() {
        return deptId;
    }

    public void setDeptId(Integer deptId) {
        this.deptId = deptId;
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }
}
  • UserDaoImpl 模拟 请求数据库获取用户信息
package com.xuxu.dao.impl;

import com.xuxu.pojo.Department;
import com.xuxu.pojo.User;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户数据访问层实现类
 */
@Component
public class UserDaoImpl {
    public static List<User> userList;

    /**
     *  模拟数据
     */
    static {
        userList = new ArrayList<User>();
        userList.add(new User(1001, "用户1", "aa@163.com", 1, new Date(), new Department(101, "部门1")));
        userList.add(new User(1002, "用户2", "bb@163.com", 1, new Date(), new Department(102, "部门2")));
        userList.add(new User(1003, "用户3", "cc@163.com", 0, new Date(), new Department(103, "部门3")));
        userList.add(new User(1004, "用户4", "dd@163.com", 0, new Date(), new Department(104, "部门4")));
        userList.add(new User(1005, "用户5", "ee@163.com", 1, new Date(), new Department(105, "部门5")));
        userList.add(new User(1006, "用户6", "ff@163.com", 0, new Date(), new Department(101, "部门6")));
        userList.add(new User(1007, "用户7", "gg@163.com", 1, new Date(), new Department(102, "部门7")));
        userList.add(new User(1008, "用户8", "hh@163.com", 0, new Date(), new Department(103, "部门8")));
        userList.add(new User(1009, "用户9", "ii@163.com", 1, new Date(), new Department(104, "部门9")));
    }

    public List<User> getUserList() {
        return userList;
    }
}
  • UserController
package com.xuxu.controller;

import com.xuxu.dao.impl.UserDaoImpl;
import com.xuxu.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Administrator
 * @create 2019/2/27
 * 用户控制层
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserDaoImpl userDaoImpl;
    /**
     * @param username    前端传入用户名
     * @param password    前端传入密码
     * @param httpSession session存放登录用户信息
     * @return 重定向页面
     */
    @PostMapping("/login")
    public String login(String username, String password,HttpSession httpSession) {
        /**
         * 当账号不为空,密码为 123456 时,模拟登录成功,否则失败时重定向返回登录页面
         * 重定向时 要以 "/" 开头表示应用根地址
         */
        if (!StringUtils.isEmpty(username) && "123456".equals(password)) {
            httpSession.setAttribute("userName",username);
            return "redirect:/userList";
        }else{
            //重定向到index页面,index是之后要配置的请求路径之一
            return "redirect:/index";
        }
    }

    /**
     *查询用户列表
     * @param model
     * @return  用户列表
     */
    @GetMapping("users")
    public String getAllUser(Model model){
        List<User> userList = userDaoImpl.getUserList();
        model.addAttribute("userList",userList);
        /** 往前端 Thymeleaf 模板引擎时,开头不要加 "/" ,因为它默认配置的前缀就是:
         * spring.thymeleaf.prefix=classpath:/templates/
         */
        return "userList";
    }
}

userList.html

  • 页面如何取值进行遍历显示才是本文的重点,后台的部分以前非 Spring Boot 时已经用的很熟了
  • 如同 JSP 可以使用 JSTL 的 <c:forEach> 、<c:forTokens> 进行遍历一样,Thymeleaf 同样有自己的遍历方式
<!DOCTYPE html>
<!-- saved from url=(0052)http://getbootstrap.com/docs/4.0/examples/dashboard/ -->
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Dashboard Template for Bootstrap</title>
    <!-- Bootstrap core CSS -->
    <link th:href="@{/asserts/css/bootstrap.min.css}" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link th:href="@{/asserts/css/dashboard.css}" rel="stylesheet">
    <style type="text/css">
        /* Chart.js */

        @-webkit-keyframes chartjs-render-animation {
            from {
                opacity: 0.99
            }
            to {
                opacity: 1
            }
        }

        @keyframes chartjs-render-animation {
            from {
                opacity: 0.99
            }
            to {
                opacity: 1
            }
        }

        .chartjs-render-monitor {
            -webkit-animation: chartjs-render-animation 0.001s;
            animation: chartjs-render-animation 0.001s;
        }
    </style>
</head>

<body>
<!--引入通用头部-->
<div th:replace="common::head"></div>

<div class="container-fluid">
    <div class="row">
        <!--引入通用左侧菜单-->
        <!--<div th:replace="common::left"></div>-->
        <div th:replace="common::left(activeUri='userList')"></div>

        <main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
            <h2>Section title</h2>
            <div class="table-responsive">
                <table class="table table-striped table-sm">
                    <thead>
                    <tr>
                        <th>用户ID</th>
                        <th>用户名</th>
                        <th>邮箱</th>
                        <th>性别</th>
                        <th>生日</th>
                        <th>部门ID</th>
                        <th>部门名</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <!-- th:each 表示遍历,userList是后台的参数名;循环时每次的变量名为 user
                    如同 JSTL 一样,当userList为null 或者大小为0 时,不会报错-->
                    <tr th:each="user:${userList}">
                        <td th:text="${user.uId}"></td>
                        <!--这是行内写法 写成<td th:text="${user.uName}"></td>也是可以的-->
                        <td>[[${user.uName}]]</td>
                        <td th:text="${user.email}"></td>
                        <!-- 三元运算符,User的gender为0则表示女生,1表示男生-->
                        <td th:text="${user.gender==1?'男':'女'}"></td>
                        <!--日期格式转换-->
                        <td th:text="${#dates.format(user.birth,'yyyy-MM-dd')}"></td>
                        <td th:text="${user.department.deptId}"></td>
                        <td th:text="${user.department.deptName}"></td>
                        <td>
                            <a class="btn btn-sm btn-primary">编辑</a>
                            <button class="btn btn-sm btn-danger deleteBtn">删除</button>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </main>
    </div>
</div>

<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script type="text/javascript" th:src="@{/asserts/js/jquery-3.2.1.slim.min.js}"></script>
<script type="text/javascript" th:src="@{/asserts/js/popper.min.js}"></script>
<script type="text/javascript" th:src="@{/asserts/js/bootstrap.min.js}"></script>

<!-- Icons -->
<script type="text/javascript" th:src="@{/asserts/js/feather.min.js}"></script>
<script>
    feather.replace()
</script>

<!-- Graphs -->
<script type="text/javascript" th:src="@{/asserts/js/Chart.min.js}"></script>
<script>
    var ctx = document.getElementById("myChart");
    var myChart = new Chart(ctx, {
        type: 'line',
        data: {
            labels: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
            datasets: [{
                data: [15339, 21345, 18483, 24003, 23489, 24092, 12034],
                lineTension: 0,
                backgroundColor: 'transparent',
                borderColor: '#007bff',
                borderWidth: 4,
                pointBackgroundColor: '#007bff'
            }]
        },
        options: {
            scales: {
                yAxes: [{
                    ticks: {
                        beginAtZero: false
                    }
                }]
            },
            legend: {
                display: false,
            }
        }
    });
</script>

</body>

</html>
  • common.html中添加请求接口地址

  • 效果

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值