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的更多相关文章
随机推荐
- NSFileHandle类
Objective-C使用NSFileHandle类对文件进行基本操作,IOS文件操作 NSFileHandle类中得方法可以对文件进行基本的读写,偏移量的操作.NSFileHandle基本步骤:1. ...
- iphone X 的适配
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 代理模式精讲(手写JDK动态代理)
代理模式是一种架构型模式,表现出来就是一个类代表另一个类的功能,一般用在想对访问一个类的时候做一些控制,同时又不想影响正常的业务,这种代理模式在现实的生活中应用的也非常的广泛,我用穷举法给举几个好理解 ...
- sysbench--mysql测试
1.下载sysbench-0.4.12.14.tar.gz 2.解压.tar -zxf sysbench-0.4.12.14.tar.gz 3.编译: 填写mysql路劲. ./configure - ...
- JavaScript小技巧整理篇(非常全)
能够为大家提供这些简短而实用的JavaScript技巧来提高大家编程能力,这对于我来说是件很开心的事.每天仅花上不到2分钟的时间中,你将可 以读遍JavaScript这门可怕的语言所呈现给我们的特性: ...
- 修改 root密码
sudo su #切换到root账户sudo passwd root #输入密码
- rbac组件之权限操作(四)
对于权限表的操作有两种方式,第一种是一个个的权限进行curd,另外一种是批量操作,自动发现django程序中的路由,进行批量curd,首先介绍第一种方式. 因为在列出菜单时,已经将权限列表列出来了,所 ...
- python书籍推荐:Python数据科学手册
所属网站分类: 资源下载 > python电子书 作者:today 链接:http://www.pythonheidong.com/blog/article/448/ 来源:python黑洞网 ...
- UVa 1599 理想路径(反向BFS 求最短路径 )
题意: 给定一个有重边有自环的无向图,n个点(2 <= n <= 100000), m条边(1 <= m <= 200000), 每条边有一个权值, 求从第一个点到n的最少步数 ...
- 对Kaldi nnet3进行奇异值分解(SVD)以减小模型大小
用处 基于SVD实现模型压缩以适配低功耗平台 根据nnet3bin/nnet3-copy,nnet3-copy或nnet3-am-copy的"--edits-config" ...