基于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 ...
随机推荐
- python自省函数getattr的用法
getattr是python里的一个内建函数 getattr()这个方法最主要的作用是实现反射机制.也就是说可以通过字符串获取方法实例.这样,你就可以把一个类可能要调用的方法放在配置文件里,在需要的时 ...
- java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory的解决(碰到问题,转载答案)
自己前一段时间出现了这个问题,通过在网上搜索,大概知道了原因,整理下一,以供大家参考. 将项目部署好后,启动tomcat后报错,java.lang.NoClassDefFoundError: org/ ...
- 第六章 springboot + 事务(转载)
本篇博客转发自:http://www.cnblogs.com/java-zhao/p/5350106.html 在实际开发中,其实很少会用到事务,一般情况下事务用的比较多的是在金钱计算方面. myba ...
- 强连通分量的一二三 | | JZOJ【P1232】 | | 我也不知道我写的什么
贴题: 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间之 ...
- 使用 hexdump dump 文件内容
名词解释 [dump] dump 是指把文件的内容,每个字节用2位十六进制数来表示的方式. 缘由 最近看矢泽久雄的<How Program Works>,了解到 dump “exe文件”( ...
- MVC中的常见问题
1. The model backing the 'MusicStoreDBContext' context has changed since the database was created. ...
- Android框架之AndroidAnnotations详细讲解
一: (1)一个activity如过使用AndroidAnnotions注入时, 那么它在 AndroidManifest.xml注册时,应该加入_ 比如: MainActivity的注册时 < ...
- win10下搭建QTP测试环境
安装环境win 10 64位企业版 个人学习用1..net 3.5无法安装更新问题解决:打开windows update 服务2.win10 安装中提示为了对电脑进行保护,已经阻止此应用,请与管理员联 ...
- html table冻结列
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...
- [08]APUE:进程控制
[a] getpid / getppid / getuid / geteuid / getgid / getegid #include <unistd.h> pid_t getpid(vo ...