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

2026-02-17 18:10:49

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>

                                        <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实现员工列表的查询

猜你喜欢