zoukankan      html  css  js  c++  java
  • SpringMVC实现查询功能

    1 web.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3     xmlns="http://java.sun.com/xml/ns/javaee"
     4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     5     id="WebApp_ID" version="2.5">
     6 
     7     <!-- 配置HiddenHttpMethodFilter:把POST 请求转为DELETE/PUT 请求 -->
     8     <filter>
     9         <filter-name>HiddenHttpMethodFilter</filter-name>
    10         <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    11     </filter>
    12     <filter-mapping>
    13         <filter-name>HiddenHttpMethodFilter</filter-name>
    14         <url-pattern>/*</url-pattern>
    15     </filter-mapping>
    16     
    17     <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
    18     <servlet>
    19         <servlet-name>springDispatcherServlet</servlet-name>
    20         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    21         <init-param>
    22             <param-name>contextConfigLocation</param-name>
    23             <param-value>classpath:springmvc.xml</param-value>
    24         </init-param>
    25         <load-on-startup>1</load-on-startup>
    26     </servlet>
    27 
    28     <!-- Map all requests to the DispatcherServlet for handling -->
    29     <servlet-mapping>
    30         <servlet-name>springDispatcherServlet</servlet-name>
    31         <url-pattern>/</url-pattern>
    32     </servlet-mapping>
    33 </web-app>

    2 springmvc.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:c="http://www.springframework.org/schema/c"
     5     xmlns:aop="http://www.springframework.org/schema/aop"
     6     xmlns:context="http://www.springframework.org/schema/context"
     7     xmlns:mvc="http://www.springframework.org/schema/mvc"
     8     xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
     9         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
    10         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    11         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
    12     
    13     <!-- 配置自动扫描的包 -->
    14     <context:component-scan base-package="com.springmvc"></context:component-scan>
    15 
    16     <!-- 配置视图解析器 -->
    17     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    18         <property name="prefix" value="/WEB-INF/views/"></property>
    19         <property name="suffix" value=".jsp"></property>
    20     </bean>
    21     
    22     <!-- 处理静态资源 -->
    23     <mvc:default-servlet-handler/>
    24     <mvc:annotation-driven></mvc:annotation-driven>
    25     
    26 
    27 </beans>

    3 实体类com.springmvc.crud.entities

     1 package com.springmvc.crud.entities;
     2 
     3 import java.util.Date;
     4 
     5 public class Employee {
     6 
     7     private Integer id;
     8     private String lastName;
     9     private String email;
    10     //1 male, 0 female
    11     private Integer gender;
    12     
    13     private Date birth;
    14     private Float salary;
    15 
    16     public Integer getId() {
    17         return id;
    18     }
    19 
    20     public void setId(Integer id) {
    21         this.id = id;
    22     }
    23 
    24     public String getLastName() {
    25         return lastName;
    26     }
    27 
    28     public void setLastName(String lastName) {
    29         this.lastName = lastName;
    30     }
    31 
    32     public String getEmail() {
    33         return email;
    34     }
    35 
    36     public void setEmail(String email) {
    37         this.email = email;
    38     }
    39 
    40     public Integer getGender() {
    41         return gender;
    42     }
    43 
    44     public void setGender(Integer gender) {
    45         this.gender = gender;
    46     }
    47 
    48     public Date getBirth() {
    49         return birth;
    50     }
    51 
    52     public void setBirth(Date birth) {
    53         this.birth = birth;
    54     }
    55 
    56     public Float getSalary() {
    57         return salary;
    58     }
    59 
    60     public void setSalary(Float salary) {
    61         this.salary = salary;
    62     }
    63 
    64     @Override
    65     public String toString() {
    66         return "Employee [id=" + id + ", lastName=" + lastName + ", email="
    67                 + email + ", gender=" + gender 
    68                 + ", birth=" + birth + ", salary=" + salary + "]";
    69     }
    70 
    71     public Employee(Integer id, String lastName, String email, Integer gender) {
    72         super();
    73         this.id = id;
    74         this.lastName = lastName;
    75         this.email = email;
    76         this.gender = gender;
    77     }
    78 
    79     public Employee() {
    80     }
    81 }

    4 com.springmvc.crud.dao

     1 package com.springmvc.crud.dao;
     2 
     3 import java.util.Collection;
     4 import java.util.HashMap;
     5 import java.util.Map;
     6 
     7 import org.springframework.beans.factory.annotation.Autowired;
     8 import org.springframework.stereotype.Repository;
     9 
    10 import com.springmvc.crud.entities.Employee;
    11 
    12 @Repository
    13 public class EmployeeDao {
    14     
    15     private static Map<Integer,Employee> employees=null;
    16     
    17     /*@Autowired
    18     private DepartmentDao departmentDao;*/
    19     
    20     static{
    21         employees = new HashMap<Integer, Employee>();
    22         
    23         employees.put(1001, new Employee(1001, "E-AA", "aa@163.com", 1));
    24         employees.put(1002, new Employee(1002, "E-BB", "bb@163.com", 1));
    25         employees.put(1003, new Employee(1003, "E-CC", "cc@163.com", 0));
    26         employees.put(1004, new Employee(1004, "E-DD", "dd@163.com", 0));
    27         employees.put(1005, new Employee(1005, "E-EE", "ee@163.com", 1));
    28     }
    29     
    30     private static Integer initId=1006;
    31     
    32     public void save(Employee employee){
    33         if(employee.getId()==null){
    34             employee.setId(initId++);
    35         }
    36         //employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));
    37         employees.put(employee.getId(), employee);
    38     }
    39     
    40     public Collection<Employee> getAll(){
    41         return employees.values();
    42     }
    43     public Employee get(Integer id){
    44         return employees.get(id);
    45     }
    46     
    47     public void delete(Integer id ){
    48         employees.remove(id);
    49     }
    50     
    51     
    52 }

    5  控制器 com.springmvc.crud.handler

      

     1 package com.springmvc.crud.handler;
     2 
     3 import java.util.Map;
     4 
     5 import org.springframework.beans.factory.annotation.Autowired;
     6 import org.springframework.stereotype.Controller;
     7 import org.springframework.web.bind.annotation.RequestMapping;
     8 
     9 import com.springmvc.crud.dao.EmployeeDao;
    10 
    11 @Controller
    12 public class EmployeeHandler {
    13 
    14     @Autowired
    15     private EmployeeDao employeeDao;
    16     
    17     @RequestMapping("/listAll")
    18     public String list(Map<String,Object> map){
    19         map.put("employees",employeeDao.getAll());
    20         return "list";
    21     }
    22 }

    6 WEB-INF/views/list.jsp

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
     4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     5 <html>
     6 <head>
     7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     8 <title>Insert title here</title>
     9 </head>
    10 <body>
    11     <table border="1" cellpadding="10" cellspacing="0">
    12         
    13         <tr>
    14             <th>ID</th>
    15             <th>LastName</th>
    16             <th>Email</th>
    17             <th>Gender</th>
    18         </tr>
    19         
    20         <c:forEach items="${requestScope.employees }" var="emp">
    21             <tr>
    22                 <td>${emp.id }</td>
    23                 <td>${emp.lastName }</td>
    24                 <td>${emp.email }</td>
    25                 <td>${emp.gender==0? 'male' : 'female' }</td>
    26             </tr>
    27         </c:forEach>
    28     </table>
    29 </body>
    30 </html>

     

  • 相关阅读:
    3月2日 学习记录
    2月10日 体温APP开发总结
    2月8日 体温APP开发记录
    2月5日 体温APP开发记录
    2月4日 体温APP开发记录
    2月3日 体温APP开发记录
    2月2日 体温APP开发记录
    2月1日 体温APP开发记录
    Java并发编程之synchronized详解(锁优化、锁升级)
    mysql 中关于获取行号@rownum:=@rownum+1 需要进一步了解
  • 原文地址:https://www.cnblogs.com/noaman/p/6213238.html
Copyright © 2011-2022 走看看