SpringMVC_RESTRUL_CRUD
编写POJO
Departmet:
package org.springmvc.curd.entity;
public class Department {
private int id;
private String departmentName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
@Override
public String toString() {
return "Department [id=" + id + ", departmentName=" + departmentName + "]";
}
public Department() {
}
public Department(int id, String departmentName) {
super();
this.id = id;
this.departmentName = departmentName;
}
}
Employee:
package org.springmvc.curd.entity;
public class Employee {
private Integer id;
private String lastName;
private String email;
private Integer gender;
private Department department = new Department();
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public Integer getId() {
return id;
}
public void setId(Integer 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 Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + ", department="
+ department + "]";
}
public Employee(int id, String lastName, String email, int i, Department department) {
super();
this.id = id;
this.lastName = lastName;
this.email = email;
this.gender = i;
this.department = department;
}
public Employee() {
}
}
编写Dao:
DepartmentDao:
package org.springmvc.crud.dao; import java.util.Collection;
import java.util.HashMap;
import java.util.Map; import org.springframework.stereotype.Repository;
import org.springmvc.curd.entity.Department;
@Repository
public class DepartmentDao {
private static Map departments;
static {
departments = new HashMap<Integer,Department>();
departments.put(101,new Department(101,"Depertment_101"));
departments.put(102,new Department(102,"Depertment_102"));
departments.put(103,new Department(103,"Depertment_103"));
departments.put(104,new Department(104,"Depertment_104"));
departments.put(105,new Department(105,"Depertment_105"));
}
public Collection<Department> getDepartments(){
return departments.values();
}
public Department getDepartment(Integer id) { return (Department) departments.get(id);
}
}
EmployeeDao:
package org.springmvc.crud.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 org.springmvc.curd.entity.Department;
import org.springmvc.curd.entity.Employee; import com.sun.org.apache.regexp.internal.recompile;
import com.sun.org.apache.xml.internal.security.Init;
@Repository
public class EmployeeDao {
private static Map<Integer, Employee> employees;
private static Integer initId =1006;
@Autowired
private DepartmentDao departmentDao;
static {
employees = new HashMap<>();
employees.put(1001, new Employee(1001, "employee_1001","1001@163.com", 1,new Department()));
employees.put(1002, new Employee(1002, "employee_1002","1001@163.com", 0,new Department()));
employees.put(1003, new Employee(1003, "employee_1003","1001@163.com", 1,new Department()));
employees.put(1004, new Employee(1004, "employee_1004","1001@163.com", 0,new Department()));
employees.put(1005, new Employee(1005, "employee_1005","1001@163.com", 1,new Department()));
}
public void save(Employee employee){
if (employee.getId()==null) {
employee.setId(initId++);
}
employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));
employees.put(employee.getId(), employee);
}
public Collection<Employee> getAll(){
return employees.values();
}
public Employee get(Integer id) {
return employees.get(id);
}
public void delete(Integer id) {
employees.remove(id);
}
}
编写jsp:
index.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>
<a href="emps" >list all emps</a> </body>
</html>
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>
<!--
springmvc 处理静态资源
dispatcherServlet 设置默认拦截了所有请求 包括页面中对图片 ,css,js文件等的加载
而在dipatcherServlet中并没有配置有过相关映射处理
1、配置<mvc:default-servlet-handler/> 处理被映射过的url
2、配置<mvc:annotation-driven></mvc:annotation-driven> -->
<script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
/* 先将delete的url发送到form 在由form进行delete类型请求*/
$(function(){
$(".delete").click(function(){
var href = $(this).attr("href");
$("form").attr("action", href).submit();
return false;
});
})
</script>
</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>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.gender==0?'female':'male'}</td>
<td>${emp.department.departmentName}</td>
<td><a href="">Edit</a></td>
<td><a class="delete" href="emp/${emp.id}">Delete</a></td> </tr>
</c:forEach>
</table>
<br><br>
<a href="addEmployee">addEmployee</a>
<!-- 只能由post请求转化为Delete请求 而默认的超链接是get请求
只能编写表单指定为post在进行转化
表单要求: name="_method" value="DELETE"
-->
<form action="" method="POST">
<input type="hidden" name="_method" value="DELETE"/>
</form>
</c:if>
</body>
</html>
input.jsp:
<%@page import="org.springmvc.curd.entity.Department"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.HashMap"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!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>
<!-- 使用form标签 更方便得进行表单值得回显-->
<form:form action="save" method="POST" modelAttribute="employee">
<!--path 属性对应 HTML 表单name -->
lastName:<form:input path="lastName"/>
<br>
email:<form:input path="email"/>
<br>
<% Map<String, String> genders = new HashMap<String, String>();
genders.put("0","female");
genders.put("1","male");
request.setAttribute("genders", genders);
%>
gender:<form:radiobuttons path="gender" items="${genders}" /> <br>
department:<form:select path="department.id"
items="${departments}"
itemLabel="departmentName"
itemValue="id" ></form:select> <input type="submit" value="submit">
</form:form>
</body>
</html>
编写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.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="org.springmvc"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:default-servlet-handler/>
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
编写Handler
package org.springmvc.curd.handlers; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springmvc.crud.dao.DepartmentDao;
import org.springmvc.crud.dao.EmployeeDao;
import org.springmvc.curd.entity.Department;
import org.springmvc.curd.entity.Employee; import com.fasterxml.jackson.annotation.JsonFormat.Value;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.sun.org.apache.regexp.internal.recompile; @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";
}
@RequestMapping(value="addEmployee",method=RequestMethod.GET)
public String input(Map<String,Object> map) {
map.put("departments",departmentDao.getDepartments());
map.put("employee", new Employee());
return "input";
}
//@PathVariable可用于获取随url发送过来的变量 匹配到参数
@RequestMapping(value="save", method = RequestMethod.POST)
public String save(Employee employee) {
employeeDao.save(employee);
return "redirect:emps";
}
@RequestMapping(value="/emp/{id}",method=RequestMethod.DELETE)
public String delete(@PathVariable("id")Integer id) {
employeeDao.delete(id);
return "redirect:emps";
}
}
SpringMVC_RESTRUL_CRUD的更多相关文章
随机推荐
- x264介绍
x264 编辑 H.264是ITU(International Telecommunication Unite 国际通信联盟)和MPEG(Motion Picture Experts Group ...
- swiper实现响应式全屏自动轮播
html: <!--轮播 --> <div class="Excellent_swi"> <div class="swiper-contai ...
- 分布式集群算法 memcached 如何实现分布式?
memcached 是一个”分布式缓存”,然后 memcached 并不像 mongoDB 那 样,允许配置多个节点,且节点之间”自动分配数据”. 就是说--memcached 节点之间,是不互相通信 ...
- 在rubymine中集成heroku插件
先安装heroku,参见http://www.cnblogs.com/jecyhw/p/4906990.html Heroku安装之后,就自动安装上git,目录为C:\Program Files (x ...
- django-3 admin开启后台配置并展示表内容
设置了superuser 之后,可以在run server 后, 通过浏览器访问后台,进行界面配置. 1. python manage.py creatersuperuser 此命令在manage.p ...
- Python之文件处理-批量修改md文档内容
目录 Python之文件处理-批量修改md文档内容 Python之文件处理-批量修改md文档内容 #!/usr/bin/env python # -*- coding:utf-8 -*- import ...
- Python之面向对象多态
Python之面向对象多态 多态与多态性: 多态: 多态是指一类事物有多种形态,一个抽象类有多个子类,因而多态的概念依赖于继承. 1.序列类型有多种形态:字符串.列表.元组. 2.动物有多种形态:Pe ...
- Hdu 5274 Dylans loves tree (树链剖分模板)
Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...
- FPGA学习笔记(八)—— 状态机设计实例之独立按键消抖
###### [该随笔中部分内容转载自小梅哥] ######### 独立按键消抖自古以来在单片机和FPGA中都是个不可避免的问题,首先,解释一下什么叫做按键抖动,如图,按键在按下和松开的那个瞬间存在大 ...
- 前端开发:JavaScript---DOM & BOM
DOM:Document Object Model 文档对象类型 模态框案例 <!DOCTYPE html> <html lang="en"> <h ...