1.效果图
2.controller代码
注:在测试的时候我把登录界面的判断也放在这里(login,这样做不好应该分开),返回给表格的数据只有listUser。user.java的代码是int id ,String name, int age, int grade。这里直接返回List《user>
@RequestMapping("/isOk")
public ModelAndView login(HttpServletRequest request,HttpServletResponse response){
String userName=request.getParameter("form-username");
String passWord=request.getParameter("form-password");
Login login=new Login(userName,passWord);
String isok=loginService.getLogin(login);
List<User> listUser=userService.getUser();
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("listUser",listUser);
if(isok.trim().equals("ok")){
modelAndView.setViewName("main");
return modelAndView;
}else
{
modelAndView.setViewName("index");
return modelAndView;
}
}
3页面代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>员工列表</title>
<!--
web资源的路径问题,不是以/开始的都是相对路径,/代表站点根目录
-->
<%
pageContext.setAttribute("path", request.getContextPath());
%>
<link rel="stylesheet"
href="${path}/assets/bootstrap/css/bootstrap.min.css">
<script type="text/javascript"
src="${path }/assets/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript"
src="${path }/assets/js/jquery-1.11.1.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h1>SSM+bootstrap</h1>
</div>
</div>
<div class="row">
<div class="col-md-12">
<table class="table table-hover">
<tr>
<th>#</th>
<th>Name</th>
<th>age</th>
<th>grade</th>
<!-- <th>DeptName</th> -->
<th>Operate</th>
</tr>
<c:forEach items="${listUser}" var="emp">
<tr>
<td>${emp.id }</td>
<td>${emp.name }</td>
<td>${emp.age }</td>
<td>${emp.grade}</td>
<%-- <td>${emp.department.deptName }</td> --%>
<td>
<button class="btn btn-primary btn-sm">
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
增加
</button>
<button class="btn btn-warning btn-sm">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
删除
</button>
</td>
</tr>
</c:forEach>
</table>
</div>
</div>
<!-- 分页功能未使用 -->
[分页案例](http://blog.csdn.net/wu_0916/article/details/74389746)
<div class="row">
<div class="col-md-6">
当前${pageInfo.pageNum }页,总共${pageInfo.pages }页,总共${pageInfo.total }条记录
</div>
<div class="col-md-6">
<nav aria-label="Page navigation">
<ul class="pagination">
<li><a href="${path }/emps?pn=1">首页</a></li>
<c:if test="${pageInfo.hasPreviousPage }">
<li>
<a href="${path }/emps?pn=${pageInfo.pageNum-1}" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
</c:if>
<c:forEach items="${pageInfo.navigatepageNums }" var="page">
<c:if test="${page==pageInfo.pageNum }">
<li class="active"><a href="${path }/emps?pn=${page}">${page}</a></li>
</c:if>
<c:if test="${page!=pageInfo.pageNum }">
<li><a href="${path }/emps?pn=${page}">${page}</a></li>
</c:if>
</c:forEach>
<c:if test="${pageInfo.hasNextPage }">
<li>
<a href="${path }/emps?pn=${pageInfo.pageNum+1 }" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</c:if>
<li><a href="${path }/emps?pn=${pageInfo.pages}">末页</a></li>
</ul>
</nav>
</div>
</div>
</div>
</body>
</html>