【SpringMVC】CRUD(增删改查)——查

29 篇文章 0 订阅
25 篇文章 0 订阅

查询员工列表(无数据库)

**说明:**该操作不涉及数据库,是将数据存储在前端页面request域中,前端页面直接获取的。
结构目录:
在这里插入图片描述

步骤:

  1. 前端页面(index.js)直接发送/emps请求。
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!-- 访问项目就要展示员工列表页面 -->
<jsp:forward page="/emps"></jsp:forward>>
  1. 控制器EmployeeController.java拦截/emps请求,拦截后将employeeDao中的数据保存到前端页面list.jsprequest域中,以便该页面能直接获取数据。
package com.atguigu.controller;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.atguigu.bean.Employee;
import com.atguigu.dao.EmployeeDao;

@Controller
public class EmployeeController {
	
	@Autowired
	EmployeeDao employeeDao;
	
	/*
	 * 查询所有员工
	 * */
	@RequestMapping(value="/emps")
	public String getEmps(Model model){
		Collection<Employee> all = employeeDao.getAll();
		model.addAttribute("emps", all);
		return "list";
	}
}

  1. list页面中获取reques域中的数据,并展示出来。
<%@ 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>员工列表</title>
</head>
<body>
	<h1>员工列表</h1>
	<table>
		<tr>
			<th>ID</th>
			<th>lastName</th>
			<th>lastName</th>
			<th>email</th>
			<th>gender</th>
			<th>departmentName</th>
			<th>EDIT</th>
			<th>DELETE</th>
		</tr>
		<c:forEach items="${emps}" var="emp">
			<tr>
			<td>${emp.id}</td>
			<td>${emp.lastName}</td>
			<td>${emp.email}</td>
			<td>${emp.gender==0?"女":"男"}</td>
			<td>${emp.department.departmentName}</td>
			<td>EDIT</td>
			<td>DELETE</td>
		</tr>
		</c:forEach>
	</table>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值