1. 程式人生 > >SpringMVC學習六

SpringMVC學習六

dao color tail ast Language servle spa format 技術分享

SpringMVC_RESTRUL_CRUD_顯示所有員工信息

截圖:

技術分享圖片

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name> <!-- 配置org.springframework.web.filter.HiddenHttpMethodFilter:可以把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> <!-- 配置DispatcherServlet --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置 DispatcherServlet的一個初始化參數:配置SpringMvc 配置文件的位置和名稱--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <!-- 設置啟動 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <!-- 請求處理 --> <servlet-name>springDispatcherServlet</servlet-name> <!-- /:應答所有請求 --> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>

springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 
         xmlns:aop="http://www.springframework.org/schema/aop"
 
       xmlns:mvc="http://www.springframework.org/schema/mvc"
 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 
       xmlns:context="http://www.springframework.org/schema/context" 
 
       xmlns:p="http://www.springframework.org/schema/p"
       
       xmlns:tx="http://www.springframework.org/schema/tx"
 
  xsi:schemaLocation="
 
              http://www.springframework.org/schema/beans    
 
                 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     
              http://www.springframework.org/schema/tx      
 
              http://www.springframework.org/schema/tx/spring-mvc-4.0.xsd
 
               http://www.springframework.org/schema/context
 
               http://www.springframework.org/schema/context/spring-context-4.0.xsd
               
               http://www.springframework.org/schema/mvc
               
               http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
 
               http://www.springframework.org/schema/aop
 
             http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
             
             <!-- 配置自定義掃描包 -->
             <context:component-scan base-package="como.springmvc.handlers"></context:component-scan>
             
           <!-- 配置視圖解析器:如何把handler方法返回值解析為實際的物理視圖 -->
           <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
           <!-- spring中加入jstl標簽庫 -->
           <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
                       <!-- 前綴 -->
                       <property name="prefix" value="/WEB-INF/views/"></property>
                       <!-- 後綴 -->
                       <property name="suffix" value=".jsp"></property>
           </bean>
           </bean> 
           </beans>
 

實體類:

Department.java

package como.springmvc.handlers.entity;

public class Department {
        private int id;
        private String departementName;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getDepartementName() {
            return departementName;
        }
        public void setDepartementName(String departementName) {
            this.departementName = departementName;
        }
        public Department(int id, String departementName) {
            super();
            this.id = id;
            this.departementName = departementName;
        }
        public Department() {
            super();
        }
        
        
}

Employee.java

package como.springmvc.handlers.entity;

public class Employee {
        private int id;
        private String lastName;
        private String email;
        private int gender;
        private Department department;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getLastName() {
            return lastName;
        }
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
        public String getEmail() {
            return email;
        }
        public void setEmail(String email) {
            this.email = email;
        }
        public int getGender() {
            return gender;
        }
        public void setGender(int gender) {
            this.gender = gender;
        }
        public Department getDepartment() {
            return department;
        }
        public void setDepartment(Department department) {
            this.department = department;
        }
        public Employee(int id, String lastName, String email, int gender,
                Department department) {
            super();
            this.id = id;
            this.lastName = lastName;
            this.email = email;
            this.gender = gender;
            this.department = department;
        }
        public Employee() {
            super();
        }
        
        
         
        
}

Dao層代碼:

DepartmentDao.java

package como.springmvc.handlers.dao;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Repository;

import como.springmvc.handlers.entity.Department;

@Repository//@Repository用於標註數據訪問組件,即DAO組件
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"));
}
//獲取全部的departments
public Collection<Department> getDepartments(){
return departments.values();
}

//獲取指定id的department
public Department getDepartment(Integer id){
return departments.get(id);
}
}

Employee.java

package como.springmvc.handlers.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 como.springmvc.handlers.entity.Department;
import como.springmvc.handlers.entity.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@123",1, new Department(101, "D-AA")));
            employees.put(1002, new Employee(1002,"E-BB","bb@123",0, new Department(102, "D-BB")));
            employees.put(1003, new Employee(1003,"E-CC","cc@123",1, new Department(103, "D-CC")));
            employees.put(1004, new Employee(1004,"E-DD","dd@123",1, new Department(104, "D-DD")));
            employees.put(1005, new Employee(1005,"E-EE","ee@123",0, new Department(105, "D-EE")));
        }
        
        public Collection<Employee> getAll(){
            return employees.values();
        }
       
        
}

視圖層代碼:

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>
</title>

  </head>
  
  <body>
    <a href="emps">list all Employees</a>
  </body>
</html>

list.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP list.jsp starting page</title>
     

  </head>
  
  <body>
    <c:if test="${empty requestScope.employees }">
    no information
    </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.departementName}</td>
                        <td><a href="">Edit</a></td>
                        <td><a href="">Delete</a></td>
                       
                    </tr>
            
            </c:forEach>
    </table>
    </c:if>
  </body>
</html>

SpringMVC學習六