基于SpringMVC的增删改查
废话不多说,直接开始步骤!
1.创建一个Dynamic Web Project
2.在WEB-INF包下的lib文件夹中引入相关jar
commons-logging-.jar jstl.jar spring-aop-.RELEASE.jar spring-beans-.RELEASE.jar spring-context-.RELEASE.jar spring-core-.RELEASE.jar spring-expression-.RELEASE.jar spring-web-.RELEASE.jar spring-webmvc-.RELEASE.jar standard.jar
3.编写web.xml文件---该文件配置SpringMVC的主控制器DispatcherServlet,该主控负责拦截请求,并将请求分发给handler控制器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringMVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4.编写springmvc-servlet.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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
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">
<context:component-scan base-package="com.jesse"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:default-servlet-handler/>
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
5.创建三个包,如以下图所实

说明:由于还没整合mybatis,所以先创建一些静态数据,只要方便操作就可以
以下是三个包中的代码:
package com.jesse.model;
public class Employee {
private Integer id;
private String lastName;
private String email;
private Integer gender;
private 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;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
@Override
public String toString() {
return "Employee [id=" + id + ", lastName=" + lastName + ", email="
+ email + ", gender=" + gender + ", department=" + department
+ "]";
}
public Employee(Integer id, String lastName, String email, Integer gender,
Department department) {
super();
this.id = id;
this.lastName = lastName;
this.email = email;
this.gender = gender;
this.department = department;
}
public Employee() {
// TODO Auto-generated constructor stub
}
}
package com.jesse.model;
public class Department {
private Integer id;
private String departmentName;
public Department() {
// TODO Auto-generated constructor stub
}
public Department(int i, String string) {
this.id = i;
this.departmentName = string;
}
public Integer getId() {
return id;
}
public void setId(Integer 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
+ "]";
}
}
package com.jesse.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 com.jesse.model.Department;
import com.jesse.model.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@163.com", 1, new Department(101, "D-AA")));
employees.put(1002, new Employee(1002, "E-BB", "bb@163.com", 1, new Department(102, "D-BB")));
employees.put(1003, new Employee(1003, "E-CC", "cc@163.com", 0, new Department(103, "D-CC")));
employees.put(1004, new Employee(1004, "E-DD", "dd@163.com", 0, new Department(104, "D-DD")));
employees.put(1005, new Employee(1005, "E-EE", "ee@163.com", 1, new Department(105, "D-EE")));
}
private static Integer initId = 1006;
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);
}
public void update(Integer id,Employee employee){
Employee emp = employees.get(id);
emp = employee;
}
}
package com.jesse.dao;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Repository;
import com.jesse.model.Department;
@Repository
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"));
}
public Collection<Department> getDepartments(){
return departments.values();
}
public Department getDepartment(Integer id){
return departments.get(id);
}
}
6.首先先创建index.jsp,用于跳转显示所有数据(查询)
<a href="query">query</a>
以上代码在我们运行时显示query,只要我们点击,就会跳转到控制器中调用使用@RequestMapping("query")修饰的方法
@Controller
public class EmployeeHandler {
@Autowired
private EmployeeDao employeeDao;
/*
@Autowired
private DepartmentDao departmentDao;
*/
/**
* 查询
*/
@RequestMapping("query")
public String query(Map<String,Object> map){
map.put("allEmployees", employeeDao.getAll());
return "list";
}
根据视图解析器的配置,当调用该方法后会跳转/WEB-INF/list.jsp页面,现在就来编写list.jsp页面,以让我们看到所有数据
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<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.allEmployees}" 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.departmentName}</td>
<td><a href="edit/${emp.id}">Edit</a></td>
<td><a href="delete/${emp.id}">Delete</a></td>
</tr>
</c:forEach>
</table>
<a href="readyData">Add Employee</a>
</body>
</html>
查询就写好了,让大家一起来看显示的结果,界面有点粗糙,但是是练习,所有漂亮美观与否无关紧要,把握时间才重要!

7.添加数据
在上图中,只要一点击Add Employee,就会跳转到添加页面,我们要做成以下图例的样子:

从上图看出,Department是下拉框,这意味着值必须从DepartmentDao取,所以我们得通过handler来取值
@RequestMapping("readyData")
public String readyData(Map<String,Object> map){
map.put("department", departmentDao.getDepartments());
map.put("employee", new Employee());
//map.put("actionType", "add");
return "addPage";
}
创建一个addPage.jsp的页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page import="java.util.Map"%>
<%@page import="java.util.HashMap"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
</head>
<body>
<form:form action="add" method="post" modelAttribute="employee">
<form:hidden path="id"/>
LastName:<form:input path="lastName"/><br>
Email:<form:input path="email"/><br>
<%
Map<String,String> gender = new HashMap();
gender.put("0", "Female");
gender.put("1","Male");
request.setAttribute("gender",gender);
%>
Gender:<form:radiobuttons path="gender" items="${gender}"/><br>
Department:<form:select path="department.id" items="${department}" itemValue="id" itemLabel="departmentName">
</form:select><br>
<input type="submit" value="submit">
</form:form>
</body>
</html>
也许大家注意到了,为什么方法中要存放一个空的Employee对象--->map.put("employee", new Employee()); 而addPage.jsp表单标签中也使用了该属性modelAttribute="employee",原因在于回显数据的功能,它认为我们肯定在更新数据时都要回显数据,所以必须有一个对象和表单内的属性一一对应,一个没对应都会异常,由于我们是添加数据,而不是回显数据,所有传入一个空的对象即可,当submit后,就会去寻找以下方法:
@RequestMapping("add")
public String addEmployee(Employee employee){
employeeDao.save(employee);
return "redirect:/query";
}
添加成功就重定向到查询结果集页面
8.删除数据
删除数据只要在方法中传入要删除数据的id,即可删除
在结果集点击Delete后,直接调用以下方法:
@RequestMapping(value="delete/{id}",method=RequestMethod.GET)
public String delete(@PathVariable("id") Integer id){
employeeDao.delete(id);
return "redirect:/query";
}
同样的是删除成功后重定向到查询结果集页面
9.更新数据,也是整个增删改成最繁琐的,涉及到回显数据,以及@ModelAttribute的使用等等
在结果集点击Edit,跳转到修改数据页面,该页面之前是添加数据的页面,不同得是之前传的employee对象new出来的一个空对象,而现在传的employee是已查询出来的一个对象
回显数据:
@RequestMapping("edit/{id}")
public String updateEmployee(@PathVariable("id") Integer id,
Map<String,Object> map){
map.put("employee", employeeDao.get(id));
map.put("actionType", "update");
map.put("department", departmentDao.getDepartments());
return "addPage";
}
说明:map中key值为employee的对象,是通过@PathVariable("id")获得的,就相当于根据id来获取结果集中的某一条记录,然后将该记录放入map中,并且这条记录必须和刚才的添加页面的modelAttribute="employee"的值是对应的,也就是表单标签具备回显功能,只要这两个值对应,自动把那条记录传入进来,结果为:

但是一个问题来了,更新页面其实是使用的之前的添加页面,而action值为add,这时需要进行修改,否则提交的都是add方法
在add方法和updateEmployee方法都放上一个actionType作为判断依据:
map.put("actionType", "update");
map.put("actionType","add");
然后再提交页面进行判断:
<form:form action="${requestScope.actionType=='add'?'add':'update'}" method="post" modelAttribute="employee">
<form:hidden path="id"/>
LastName:<form:input path="lastName"/><br>
Email:<form:input path="email"/><br>
<%
Map<String,String> gender = new HashMap();
gender.put("0", "Female");
gender.put("1","Male");
request.setAttribute("gender",gender);
%>
Gender:<form:radiobuttons path="gender" items="${gender}"/><br>
Department:<form:select path="department.id" items="${department}" itemValue="id" itemLabel="departmentName">
</form:select><br>
<input type="submit" value="submit">
</form:form>
如果传入进来的actionType是update,调用控制器方法更新:
@RequestMapping("update")
public String update(@RequestParam(value="id") Integer id,Employee employee){
employeeDao.update(id, employee);
return "redirect:/query";
}
必须要注意的是:
必须使用@ModelAttribute编写一个方法,在update调用前查询出数据,然后将要更新的值直接更新,不更新的值保持之前查询数来的值,比如:
Employee[name=spring age=10 salary=2000]这时如果只更新salary的值,我们想要的结果如下:
Employee[name=spring age=10 salary=3000]
@ModelAttribute
public void solvePro(@RequestParam(value="id",required=false)Integer id,
Map<String,Object> map){
if (id != null) {
map.put("employee", employeeDao.get(id));
}
}
缺少以上方法,那更新结果就变成:
Employee[name=null age=null salary=3000]
整个增删改成的过程就是这样,可能表述的不是很清楚,欢迎提出您的困惑!
基于SpringMVC的增删改查的更多相关文章
- springMVC实现增删改查
首先需要准备好一张数据库表我这里用emp这张表:具体代码: /* SQLyog 企业版 - MySQL GUI v8.14 MySQL - 5.1.73-community ************* ...
- Mybatis_3.基于注解的增删改查
1.实体类User.java public class User { private int id; private String name; private int age; //getter.se ...
- ASP.NET Web API基于OData的增删改查,以及处理实体间关系
本篇体验实现ASP.NET Web API基于OData的增删改查,以及处理实体间的关系. 首先是比较典型的一对多关系,Supplier和Product. public class Product { ...
- [转]ASP.NET Web API基于OData的增删改查,以及处理实体间关系
本文转自:http://www.cnblogs.com/darrenji/p/4926334.html 本篇体验实现ASP.NET Web API基于OData的增删改查,以及处理实体间的关系. 首先 ...
- Node.js、express、mongodb 入门(基于easyui datagrid增删改查)
前言 从在本机(win8.1)环境安装相关环境到做完这个demo大概不到两周时间,刚开始只是在本机安装环境并没有敲个Demo,从周末开始断断续续的想写一个,按照惯性思维就写一个增删改查吧,一方面是体验 ...
- SpringMvc学习-增删改查
本节主要介绍SpringMVC简单的增删改查功能. 1.查询 dao中的代码 public List<WeatherPojo> getAllWeather(){ String sql=&q ...
- Mybatis_2.基于XML的增删改查
1.实体类User.java public class User { private int id; private String name; private int age; //getter.se ...
- 基于django做增删改查组件,分页器组件
增删改查组件 一.Djangoadmin的启发 二.基于Djangoadmin实现数据的增删改查 分页器组件 分页器组件的介绍以及源码解读 补充:源码下载,
- CRM基于.NET的增删改查
一.准备工作: 1.添加 microsoft.crm.sdk.proxy.dll和microsoft.xrm.sdk.dll 引用到项目中!并引用以下using! using Microsoft.Xr ...
随机推荐
- MongoDB学习:(二)MongoDB简单使用
MongoDB学习:(二)MongoDB简单使用 MongoDB使用: 执行mongodb的操作之前,我们需要运行命令,来进入操作命令界面 >mongo 提示该错误,说明我们系统缺少一个补丁,该 ...
- js操作document文档元素 节点交换交换
<input type="text" value="1" id='text1'> <input type="text" v ...
- java 中 Math.rint()
Math.rint() **形参是 double System.out.println(Math.rint(2.5)); 返回 2.0 System.out.println(Math.rint(2.5 ...
- Ubuntu root 密码 sudo passwd
用vmware安装好ubuntu后,su - 切换不到root,提示密码错误. 解决办法: 1.用当前登录用户打开终端,在终端输入命令 sudo passwd,输入当前用户的密码然后回车 2.会提示输 ...
- lphp输出控制output controll(header, ob_xxx)
关于php的output controll参考文档: http://gywbd.github.io/posts/2015/1/php-output-buffer-in-deep.html http:/ ...
- 动态加载jar包中的类(方式一)
嘛, 直接上代码 public static class TestClassLoader extends ClassLoader { @Override protected Class<?> ...
- [03]APUE:文件 I/O
[a] open #include <fcntl.h> int open(const char *path, int oflag, ... ,mode_t mode) 成功返回文件描术符, ...
- Sql Server 常用操作
--DDL触发器CREATE TRIGGER [TR_create_drop_alter_Table] ON DATABASE FOR CREATE_TABLE,DROP_table,ALTER_ ...
- 关于webstorm 对 vue的设置
1. 首先安装vue插件,安装方法: setting --> plugin ,点击plugin,在内容部分的左侧输入框输入vue,会出现两个关于vue的插件,点击安装即可.安装完成后,就可 ...
- 多次drawRect,显示重叠,需要设置背景颜色
自己写的一个控件,中途在setNeedsDisplay,重写drawRect方法,有时会把以前drawRect绘制上去,不知道为什么,后来设置了控件的背景颜色backgroundcolor,这个问题就 ...