SpringMVC框架

7.3 RESTRUL_CRUD_显示所有员工信息

  • 增加页面链接

<a href="empList">To Employee List</a>

  • 增加处理器

package com.atguigu.springmvc.crud.handlers;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import com.atguigu.springmvc.crud.dao.EmployeeDao;

@Controller

public class EmployeeHandler {

@Autowired

private EmployeeDao employeeDao ;

@RequestMapping("/empList")

public String empList(Map<String,Object> map){

map.put("empList", employeeDao.getAll());        //默认存放到request域中        

return "list";

}

  • SpringMVC中没遍历的标签,需要使用jstl标签进行集合遍历增加jstl标签库jar包

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  

<!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>Insert title here</title>

</head>

<body>

<c:if test="${empty requestScope.empList }">

对不起,没有找到任何员工!

</c:if>

<c:if test="${!empty requestScope.empList }">

<table border="1" cellpadding="10" cellspacing="0">                

<tr>

<td>EmpId</td>

<td>LastName</td>

<td>Gender</td>

<td>Email</td>

<td>DepartmentName</td>

<td>Edit</td>

<td>Delete</td>

</tr>

<c:forEach items="${requestScope.empList }" var="emp">

<tr>

<td>${emp.id }</td>

<td>${emp.lastName }</td>

<td>${emp.gender==0?"Female":"Male" }</td>

<td>${emp.email }</td>

<td>${emp.department.departmentName }</td>

<td><a href="">Edit</a></td>

<td><a href="">Delete</a></td>

</tr>                

</c:forEach>        

</table>

</c:if>

</body>

</html>