SpringMVC使用restful实现员工列表的查询

2025-10-27 22:38:10

1、配置web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xmlns="http://java.sun.com/xml/ns/javaee" 

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 

id="WebApp_ID" version="3.0">

        <!-- The front controller of this Spring Web application, 

                responsible for handling all application requests -->

        <servlet>

                <servlet-name>springDispatcherServlet</servlet-name>

                <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

                

                <!-- 配置SpringMVC配置文件的位置和名称 -->

                <!-- 实际上也可以使用默认的配置文件:/WEB-INF/<servlet-name>-servlet.xml -->

                <init-param>

                        <param-name>contextConfigLocation</param-name>

                        <param-value>classpath:springmvc.xml</param-value>

                </init-param>

                <load-on-startup>1</load-on-startup>

        </servlet>

        <!-- Map all requests to the DispatcherServlet for handling -->

        <servlet-mapping>

                <servlet-name>springDispatcherServlet</servlet-name>

                <url-pattern>/</url-pattern>

        </servlet-mapping>

        

        <!-- 可以把POST请求转为为DELETE或者POST请求 -->

        <filter>

                <filter-name>HiddenHttpMethodFilter</filter-name>

                <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>

        </filter>

        

        <filter-mapping>

                <filter-name>HiddenHttpMethodFilter</filter-name>

                <url-pattern>/*</url-pattern>

        </filter-mapping>

        

        

</web-app>

SpringMVC使用restful实现员工列表的查询

2、配置springmvc.xml文件:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xmlns:context="http://www.springframework.org/schema/context"

        xmlns:mvc="http://www.springframework.org/schema/mvc"

        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd

                http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

        <!-- 配置自动扫描的包 -->

        <context:component-scan base-package="com.gwolf.springmvc">

        </context:component-scan>

        

        <!-- 配置视图解析器,如何把handler方法返回值解析为实际的物理视图 -->

        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

                <property name="prefix" value="/WEB-INF/views/"></property>

                <property name="suffix" value=".jsp"></property>

        </bean>

               

        <mvc:annotation-driven></mvc:annotation-driven>

        

</beans>

SpringMVC使用restful实现员工列表的查询

3、新建一个jsp页面list.jsp

<%@ 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.employees }">

                没有任何员工信息.

        </c:if>

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

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

                        <tr>

                                <th>ID</th>

                                <th>LastName</th>

                                <th>Email</th>

                                <th>Gender</th>

                                <th>Department</th>

                                <th>Edit</th>

                                <th>Delete</th>

                        </tr>

                        

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

                                <tr>

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

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

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

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

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

                                        <td><a href="emp/${emp.id}">Edit</a></td>

                                        <td><a class="delete" href="emp/${emp.id}">Del</a></td>

                                </tr>

                        </c:forEach>

                </table>

        </c:if>

</body>

</html>

SpringMVC使用restful实现员工列表的查询

4、DepartmentDao层代码如下:

package com.gwolf.springmvc.dao;

import java.util.Collection;

import java.util.HashMap;

import java.util.Map;

import org.springframework.stereotype.Repository;

import com.gwolf.springmvc.domain.Department;

@Repository

public class DepartmentDao {

        private static Map<Integer, Department> departments = null;

        

        static{

                departments = new HashMap<Integer, Department>();

                

                departments.put(101, new Department(101, "D-AA"));

                departments.put(102, new Department(102, "D-BB"));

                departments.put(103, new Department(103, "D-CC"));

                departments.put(104, new Department(104, "D-DD"));

                departments.put(105, new Department(105, "D-EE"));

        }

        

        public Collection<Department> getDepartments(){

                return departments.values();

        }

        

        public Department getDepartment(Integer id){

                return departments.get(id);

        }

        

}

SpringMVC使用restful实现员工列表的查询

5、EmployeeDao代码实现如下:

package com.gwolf.springmvc.dao;

import java.util.Collection;

import java.util.HashMap;

import java.util.Map;

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

import org.springframework.stereotype.Repository;

import com.gwolf.springmvc.domain.Department;

import com.gwolf.springmvc.domain.Employee;

@Repository

public class EmployeeDao {

        private static Map<Integer, Employee> employees = null;

        

        @Autowired

        private DepartmentDao departmentDao;

        

        static{

                employees = new HashMap<Integer, Employee>();

                employees.put(1001, new Employee(1001, "E-AA", "aa@163.com", 1, new Department(101, "D-AA")));

                employees.put(1002, new Employee(1002, "E-BB", "bb@163.com", 1, new Department(102, "D-BB")));

                employees.put(1003, new Employee(1003, "E-CC", "cc@163.com", 0, new Department(103, "D-CC")));

                employees.put(1004, new Employee(1004, "E-DD", "dd@163.com", 0, new Department(104, "D-DD")));

                employees.put(1005, new Employee(1005, "E-EE", "ee@163.com", 1, new Department(105, "D-EE")));

        }

        

        private static Integer initId = 1006;

        

       

        

        public Collection<Employee> getAll(){

                return employees.values();

        }

        

      

        

        public void delete(Integer id){

                employees.remove(id);

        }

}

SpringMVC使用restful实现员工列表的查询

SpringMVC使用restful实现员工列表的查询

6、控制层代码实现如下:

package com.gwolf.springmvc.handlers;

import java.util.Map;

import javax.validation.Valid;

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

import org.springframework.stereotype.Controller;

import org.springframework.validation.Errors;

import org.springframework.validation.FieldError;

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

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

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

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

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

import com.gwolf.springmvc.dao.DepartmentDao;

import com.gwolf.springmvc.dao.EmployeeDao;

import com.gwolf.springmvc.domain.Employee;

@Controller

public class EmployeeHandler {

        @Autowired

        private EmployeeDao employeeDao;

        

        @Autowired

        private DepartmentDao departmentDao;

              

        @RequestMapping("/emps")

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

                map.put("employees", employeeDao.getAll());

                return "list";

        }

        

}

SpringMVC使用restful实现员工列表的查询

7、启动tomcat服务器,查看程序执行结果:

SpringMVC使用restful实现员工列表的查询

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢