水浒好汉列表展示: 查询所有好汉

水浒好汉列表展示:访问index.jsp---->直接发送/emps---->控制器查询所有好汉---->将数据放在请求域中----->转发到list页面展示

添加好汉: 在list页面点击"添加好汉"------>来到添加页面(add.jsp)------->输入员工数据--------->点击保存-------->处理器收到员工保运请求(保存员工)---------->保存完成后还是来到list页面

修改好汉信息: 点击"edit"------>查出要修改的员工信息,放在请求域中,来到修改页面进行回显---------->来到edit.jsp----->数据修改完毕,将修改的数据提交给服务器 /emp/1  PUT

web工程目录结构如下:

controller层

package com.atchina.controller;import java.util.Collection;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;import com.atchina.dao.AddressDao;
import com.atchina.dao.EmployeeDao;
import com.atchina.pojo.Address;
import com.atchina.pojo.Employee;@Controller
public class EmployeeController {@Autowiredprivate EmployeeDao employeeDao;@Autowiredpublic AddressDao addressDao;// 查询所有数据@RequestMapping("/emps")public String getEmployees(Model model){Collection<Employee> emps = employeeDao.getEmployees();model.addAttribute("emps", emps);return "list";}// 跳转到添加页面add.jsp@RequestMapping("/toaddpage")public String toAddPage(Model model){// 查询出所有地址Collection<Address>  addresses = addressDao.getAddresss();// 添加到请求域中model.addAttribute("address", addresses);// 用于数据回显model.addAttribute("employee", new Employee());return "add";}// 保存添加页面,提价过来的数据@RequestMapping(value="/emp", method=RequestMethod.POST)public String save(Employee employee){employeeDao.save(employee);//return "forward:/emps";return "redirect:/emps";}// 查询好汉信息@RequestMapping(value="/emp/{id}", method=RequestMethod.GET)public String getEmp(@PathVariable("id")String id, Model model){Employee ee = employeeDao.getEmployeeById(id);// 用于数据回显model.addAttribute("employee", ee);// 查询出所有地址Collection<Address>  addresses = addressDao.getAddresss();// 添加到请求域中model.addAttribute("address", addresses);//return "forward:/emps";return "edit";}// 修改好汉信息@RequestMapping(value="/emp/{id}", method=RequestMethod.PUT)public String updateEmp(@PathVariable("id")String id, Employee employee){Employee ee = employeeDao.getEmployeeById(id);employee.setId(id);employee.setName(ee.getName());employeeDao.save(employee);//return "forward:/emps";return "redirect:/emps";}// 删除好汉信息@RequestMapping(value="/emp/{id}", method=RequestMethod.DELETE)public String deleteEmp(@PathVariable("id")String id){employeeDao.delete(id);//return "forward:/emps";return "redirect:/emps";}
}

DAO层

package com.atchina.dao;import java.util.Collection;
import java.util.HashMap;
import java.util.Map;import org.springframework.stereotype.Repository;import com.atchina.pojo.Address;
import com.atchina.pojo.Employee;@Repository
public class AddressDao {private static Map<String,Address> addresss;static{addresss =  new HashMap<String,Address>();addresss.put("100", new Address("100","山东省菏泽市郓城县宋家村"));addresss.put("101", new Address("101","山东省菏泽市郓城县车市村"));addresss.put("102", new Address("102","渭州(今甘肃平凉)"));addresss.put("103", new Address("103","邢台市清河县"));addresss.put("104", new Address("104","陕西,华阴县史家村"));addresss.put("105", new Address("105","扈家庄"));addresss.put("106", new Address("106","北京大名府"));addresss.put("107", new Address("107","山东郓城县东溪村"));}public Collection<Address> getAddresss(){return addresss.values();}public Address getgetAddressById(String id){return addresss.get(id);}private static Integer intid = 108;public void save(Address address){if(address.getAid() == null){address.setAid(intid.toString());intid++;}addresss.put(address.getAid(), address);}public void delete(String aid){addresss.remove(aid);}
}
package com.atchina.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.atchina.pojo.Address;
import com.atchina.pojo.Employee;@Repository
public class EmployeeDao {private static Map<String,Employee> employees;@Autowiredprivate AddressDao addressDao;static{employees =  new HashMap<String,Employee>();employees.put("1", new Employee("1","宋江",1,new Address("100","山东省菏泽市郓城县宋家村")));employees.put("2", new Employee("2","吴用",1,new Address("101","山东省菏泽市郓城县车市村")));employees.put("3", new Employee("3","鲁智深",1,new Address("102","渭州(今甘肃平凉)")));employees.put("4", new Employee("4","武松",1,new Address("103","邢台市清河县")));employees.put("5", new Employee("5","史进",1,new Address("104","陕西,华阴县史家村")));employees.put("6", new Employee("6","扈三娘",0,new Address("105","扈家庄")));}public Collection<Employee> getEmployees(){return employees.values();}public Employee getEmployeeById(String id){return employees.get(id);}private static Integer intid = 7;public void save(Employee employee){if(employee.getId() == null){employee.setId(intid.toString());intid++;}employee.setAddress(addressDao.getgetAddressById(employee.getAddress().getAid()));employees.put(employee.getId(), employee);}public void delete(String id){employees.remove(id);}
}

pojo层

package com.atchina.pojo;public class Address {private String aid;public String getAid() {return aid;}public Address() {}public Address(String aid, String aname) {super();this.aid = aid;this.aname = aname;}public void setAid(String aid) {this.aid = aid;}public String getAname() {return aname;}public void setAname(String aname) {this.aname = aname;}private String aname;
}
package com.atchina.pojo;public class Employee {private String id;private String name;public String getId() {return id;}public Employee() {}public Employee(String id, String name, Integer gender, Address address) {super();this.id = id;this.name = name;this.gender = gender;this.address = address;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getGender() {return gender;}public void setGender(Integer gender) {this.gender = gender;}public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address;}private Integer gender;private Address address;}

springmvc配置文件

<?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:mvc="http://www.springframework.org/schema/mvc"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><context:component-scan base-package="com.atchina"></context:component-scan><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/pages/"></property><property name="suffix" value=".jsp"></property></bean><!-- 静态资源可以访问了 --><mvc:default-servlet-handler/><mvc:annotation-driven></mvc:annotation-driven>
</beans>

web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-appversion="2.5"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><display-name></display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><servlet><servlet-name>DispatcherServlet</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><servlet-mapping><servlet-name>DispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!--  字符编码过滤 --><filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param><init-param><param-name>isForceResponseEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- 支持restful风格请求 --><filter><filter-name>HiddenHttpMethodFilter</filter-name><filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class></filter><filter-mapping><filter-name>HiddenHttpMethodFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>
</web-app>

jsp文件

add.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fm"  uri="http://www.springframework.org/tags/form" %><%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'hello.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><!-- 表单标签:通过SpringMVC的表单标签可以实现将模型数据中的属性和html表单元素相绑定,以实现表单数据更便捷编辑和表值的回显.1). SpringMVC认为,表单 数据中的每一项最终都是要回显的。path指定的是一个属性,这个属性是从隐含模型(请求域中取出的某个对象中属性)path指定的每一个属性,请求域中必须有一个对象(这个对象就是请求域中的command对应的对象),拥有这个属性;modelAttribute="",以前我们表单标签会从请求域中获取一个command对象;把这个对象中的每一个属性对应的显示出来.modelAttribute="employee",可以告诉SpringMVC不要去取command的值了,我做了一个modelAttribute指定的值;取对象时用的key就是我modelAttribute指定的"employee"--><%=path%><%pageContext.setAttribute("ctp", request.getContextPath());%><fm:form action="${ctp}/emp" method="post" modelAttribute="employee"><!-- path就是原来html-input的name值;需要写path 1).当做原生的name项2).自动回显隐含模型中某个对象对应的这个属性的值-->姓名: <fm:input path="name"/>   <br/>性别: 男:<fm:radiobutton path="gender" value="1" /> &nbsp;&nbsp;&nbsp;&nbsp;女:<fm:radiobutton path="gender" value="0" /><br/><!-- items="": 指定要遍历的集合;自动遍历;遍历出的每一个元素是一个address对象itemLabel="属性名":指定遍历出的这个对象的哪个属性是作为option标签体的值itemValue="属性名":指定刚才遍历出来的这个对象的哪个属性是作为要提交的value值-->地址: <fm:select path="address.aid" items="${address}" itemLabel="aname" itemValue="aid"></fm:select><br/>   <input type="submit" value="提交"/>      </fm:form><!--  <body><form action="emp" method="post">姓名: <input type="text" name="name"/>   <br/>性别: 男:<input type="radio" name="gender" value="1"/> &nbsp;&nbsp;&nbsp;&nbsp;女:<input type="radio" name="gender" value="0"/> <br/>地址:     <select name="address.aid"><c:forEach items="${address}" var="ads"><option value="${ads.aid}">${ads.aname}</option></c:forEach></select> <br/><input type="submit" value="提交"/></form></body>-->
</html>

edit.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fm"  uri="http://www.springframework.org/tags/form" %><%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'hello.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><!-- 表单标签:通过SpringMVC的表单标签可以实现将模型数据中的属性和html表单元素相绑定,以实现表单数据更便捷编辑和表值的回显.1). SpringMVC认为,表单 数据中的每一项最终都是要回显的。path指定的是一个属性,这个属性是从隐含模型(请求域中取出的某个对象中属性)path指定的每一个属性,请求域中必须有一个对象(这个对象就是请求域中的command对应的对象),拥有这个属性;modelAttribute="",以前我们表单标签会从请求域中获取一个command对象;把这个对象中的每一个属性对应的显示出来.modelAttribute="employee",可以告诉SpringMVC不要去取command的值了,我做了一个modelAttribute指定的值;取对象时用的key就是我modelAttribute指定的"employee"--><%=path%><%pageContext.setAttribute("ctp", request.getContextPath());%>edit<fm:form action="${ctp}/emp/${employee.id}" method="post" modelAttribute="employee"><input type="hidden" name="_method" value="put" /><!-- path就是原来html-input的name值;需要写path 1).当做原生的name项2).自动回显隐含模型中某个对象对应的这个属性的值-->姓名: <fm:label path="name">${employee.name}</fm:label>  <br/>性别: 男:<fm:radiobutton path="gender" value="1" /> &nbsp;&nbsp;&nbsp;&nbsp;女:<fm:radiobutton path="gender" value="0" /><br/><!-- items="": 指定要遍历的集合;自动遍历;遍历出的每一个元素是一个address对象itemLabel="属性名":指定遍历出的这个对象的哪个属性是作为option标签体的值itemValue="属性名":指定刚才遍历出来的这个对象的哪个属性是作为要提交的value值-->地址: <fm:select path="address.aid" items="${address}" itemLabel="aname" itemValue="aid"></fm:select><br/>   <input type="submit" value="提交"/>       </fm:form></html>

list.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><%pageContext.setAttribute("ctp", request.getContextPath());%><head><base href="<%=basePath%>"><title>My JSP 'hello.jsp' starting page</title><script type="text/javascript" src="${ctp}/scripts/jquery-1.9.1.min.js"></script><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><table border="1" cellpadding="5" cellspacing="2" width="400px"><tr><th>id</th><th>名字</th><th>性别</th><th>地址</th><th>修改</th><th>删除</th></tr><c:forEach items="${emps}" var="emp"><tr><td>${emp.id}</td><td>${emp.name}</td><td>${emp.gender==0?"女":"男"}</td><td>${emp.address.aname}</td><td><a href="emp/${emp.id}">EDIT</a></td><td><a href="emp/${emp.id}" class="delBtn">DELETE</a></td></tr></c:forEach></table><a href="toaddpage">添加好汉</a><form id="deleteForm" action="emp/${emp.id}" method="post"><input type="hidden" name="_method" value="DELETE"/></form><script type="text/javascript">$(function(){$(".delBtn").click(function(){// 1. 改变表单的action查询$("#deleteForm").attr("action", this.href);// 2. 提交表单$("#deleteForm").submit();return false;});});</script></body>
</html>

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body>This is my JSP page. <br><a href="hello">hello</a><br><%=basePath%><br/><jsp:forward page="/emps"></jsp:forward></body>
</html>

springmvc十八:RestfulCRUD增删改查小实战相关推荐

  1. android 增删改查 源码_学生信息增删改查小程序案例(springboot服务端)

    项目描述: 该小程序实现了简单的管理员登录,学生信息添加,修改,删除,列表显示等功能,服务器端采用springboot框架提供接口,数据传输格式为json,适合新手学习小程序与服务端的交互,以及增删改 ...

  2. Java Web(十) JDBC的增删改查,C3P0等连接池,dbutils框架的使用

    前面做了一个非常垃圾的小demo,真的无法直面它,菜的抠脚啊,真的菜,好好努力把.菜鸡. --WZY 一.JDBC是什么? Java Data Base Connectivity,java数据库连接, ...

  3. SpringMVC表单数据增删改查简易梳理(含实例代码)

    使用SpringMVC创建表单进行数据的增删改查是javaEE开发的基本功,本人根据自己最近开发的基于jeecms框架的网站平台来梳理数据增删改查的思路. 首先根据所需表单页面设计数据库,定义不同字段 ...

  4. ListView的增删改查(实战)

    这是对ListView的增删改查相关操作的具体运用. package com.jxust.day05_09_listviewdemo;import android.app.Activity; impo ...

  5. 二、PHP基础——连接msql数据库进行增删改查操作 实战:新闻管理项目

    Mysql扩展 PHP针对MySQL数据库操作提供的扩展:允许PHP当做MySQL的一个客户端连接服务器进行操作. 连库基本操作 连接数据库服务器 1)资源 mysql_connect(服务器地址,用 ...

  6. Vue+SpringBoot搭建增删改查小demo

    后端实现(SpringBoot) 1.新建一个springboot项目,添加依赖如下: <dependency><groupId>org.springframework.boo ...

  7. SpringMVC+Mybatis整合的增删改查

    本文基于 SPRING注解.本文使用Oracle数据库. 项目文件下载地址:http://download.csdn.net/detail/u010634066/8188965 项目总图: 现在lib ...

  8. springmvc十三:REST风格增删改查

    如果从页面发起PUT,DELETE请求?  spring提供了对rest风格的支持. 1). SpringMVC中有一个Filter,他可以把普通的请求转换为规定形式的请求,要配置一个filter & ...

  9. html页面增删改查模板,模板引擎+(实现数据增删改查小项目)

    art-template模板引擎: 在命令行工具中使用 npm install art-template 命令进行下载 使用const template = require('art-template ...

最新文章

  1. 【英文文本分类实战】之三——数据清洗
  2. Vue入门三、过滤器filter
  3. java enum 返回list_java – 组合枚举并使用getter返回指定的枚举
  4. Maven安装与配置——手把手教程
  5. 怎么撤销定时说说_已注册商标遇到撤三申请怎么办
  6. 在画图软件中,可以画出不同大小或颜色的圆形、矩形等几何图形。几何图形之间有许多共同的特征,如它们可以是用某种颜色画出来的,可以是填充的或者不填充的。此外还有些不同的特征,比如,圆形都有半径,可以根据半
  7. IIS------项目配置到IIS后报500错误
  8. 如何使用 Cloud Insight SDK 实现 Druid 监控?
  9. android数据绑定_Android数据绑定
  10. Flink Table和SQL的基本API
  11. atitit 需求条目的自动化生成原型html h5界面ui与解决方案
  12. 文件系统性能测试工具 FIO工具
  13. tensorflow gpu环境安装
  14. 电脑dns服务器未响应该怎么操作,电脑DNS服务器未响应该怎么办
  15. 复现ReDet RTX 3090 pytorch1.8.1
  16. 情态动词+have+done用法整理
  17. 三国群英传霸业之王服务器维护,《三国群英传:霸王之业》8月27日维护更新公告...
  18. vue里的axios获取本地的json数据
  19. android中如何保存对象集合中,如何序列化对象并将其保存到Android中的文件?
  20. uni-app提交表单成功之后跳转首页

热门文章

  1. MySQL重温笔记-索引
  2. Stack Overflow:研究发现访问PHP和 Android的流量更可能来自低收入国家
  3. nginx静态资源反向代理
  4. MATLAB实现最优低通滤波器的函数
  5. jsonp解析 html
  6. Webfrom --图片验证码
  7. 汇编实验:屏幕窗口程序(代码有较为详细的注释)
  8. 转转转![Spring MVC] - 500/404错误处理-SimpleMappingExceptionResolver
  9. PCL点云库:Kd树
  10. 微软笔试题 2013暑期实习笔试题目