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的更多相关文章
随机推荐
- mybatis保存时将数据库自动生成的主键返回
场景 保存订单数据和订单详情数据时需要将订单的主键作为关联子段添加到明细表中,需要将保存订单时的主键返回给供保存明细表时使用 添加xml中新增数据时的配置 <insert id="in ...
- 小甲鱼python疑难点
1.python生成器 2.while 1: num = input('请输入一个整数(输入Q结束程序):') if num != 'Q': num = int(num) print('十进制 -&g ...
- SQL中带有NOT IN 子查询改写
报表程序中的一段SQL运行很慢,代码如下: 优化前: 耗时:1337s INSERT INTO PER_LTE_ZIB_PB_COMMISSION_07 SELECT P.TOPACTUALID, Q ...
- span-wise drag/lift forces of cylinder
span-wise drag/lift forces of cylinder SR Description: Dear Sir/Madam, I am trying to simulate a 3 ...
- MT4系统自带指标代码
MT4系统自带指标代码 ~ Accelerator Oscillator 震荡加速指标: double iAC() ~ Accumulation/Distribut ...
- 76-Bears/Bulls Power,熊力/牛力震荡指标.(2015.7.1)
Bears/Bulls Power 熊力/牛力震荡指标 Power,熊力/牛力震荡指标.(2015.7.1)" title="76-Bears/Bulls Power,熊力/牛力震 ...
- python面向对象编程实例
1.编写程序, 编写一个学生类, 要求有一个计数器的属性, 统计总共实例化了多少个学生 class Student: """学生类""" c ...
- ElasticSearch全文搜索引擎(A)
文章:[Elasticsearch] 全文搜索 (一) - 基础概念和match查询 全文检索,是从最初的字符串匹配和简单的布尔逻辑检索技术,演进到能对超大文本.语音.图像.活动影像等非结构化数据进行 ...
- idea导入(import)项目和打开(open)项目的区别
前言: 每次接手老项目,都得从git或svn下载下来,但是如果之前的项目不是用idea写的怎么办,可是你又习惯啦idea,那你必须把项目在idea上跑起来,那是用import还是用open呢,如何抉择 ...
- 【进击后端】ubuntu 快速安装node mongodb express
安装软件:node,mongo,express 1.apt install node 2.node -v 3.apt install mongodb 4.mongo -version 5.apt in ...