spring mvc学习(42):restful的编辑功能实现

 

上图·是目录结构,本节是有问同学的,当好好总结

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.geyao</groupId><artifactId>springmaven</artifactId><version>0.0.1-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.2.8.RELEASE</version>
</dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.8.RELEASE</version>
</dependency>
<dependency>  <groupId>javax.servlet</groupId>  <artifactId>servlet-api</artifactId>  <version>2.5</version>  </dependency>  <dependency>  <groupId>javax.servlet</groupId>  <artifactId>jsp-api</artifactId>  <version>2.0</version>  </dependency>  <dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.2.8.RELEASE</version>
</dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>4.2.8.RELEASE</version>
</dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.2.8.RELEASE</version>
</dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>4.2.8.RELEASE</version>
</dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-expression</artifactId><version>4.2.8.RELEASE</version>
</dependency>
<dependency><groupId>org.apache.taglibs</groupId><artifactId>taglibs-standard-spec</artifactId><version>1.2.5</version></dependency><dependency><groupId>org.apache.taglibs</groupId><artifactId>taglibs-standard-impl</artifactId><version>1.2.5</version></dependency><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.1.2</version></dependency></dependencies>
</project>

web.xml

<?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_3_0.xsd" id="WebApp_ID" version="3.0"><display-name>Springmaven01</display-name><welcome-file-list><welcome-file>/WEB-INF/jsp/home.jsp</welcome-file></welcome-file-list><!-- 处理中文乱码 --><filter><filter-name>encodingFilter</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></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- SpringMVC控制器 --><servlet><servlet-name>dispatcherServlet</servlet-name><!-- 主要就是DispatcherServlet这个servlet起到分发的作用,对请求进行控制分发 --><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><!-- 每个springmvc项目都要一个springmvc项目配置位置,下面配置springmvc配置文件的路径 --><param-name>contextConfigLocation</param-name><param-value>classpath*:springMVC-servlet.xml</param-value></init-param><!-- 当容器启动时立即启动 --><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><!-- 下面配置springmvc的过滤分发请求类型,可以是/ 或者*.action等 --><url-pattern>/</url-pattern></servlet-mapping><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>

springMVC-servlet

<?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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"><!-- 定义要扫描 controller的包--><context:component-scan base-package="wormday.springmvc.helloworld" /><!-- 定义要扫描 controller的包--><context:component-scan base-package="wormday.springmvc.dao" /><!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 --><!--指定视图解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 视图的路径 --><property name="prefix" value="/WEB-INF/jsp/"/><!-- 视图名称后缀  --><property name="suffix" value=".jsp"/></bean></beans>

userDao类

package wormday.springmvc.dao;import java.util.Collection;
import java.util.HashMap;
import java.util.Map;import org.springframework.stereotype.Repository;import wormday.springmvc.pojo.Address;
import wormday.springmvc.pojo.User;
@Repository
public class UserDao {private static Map<Integer, User> users =null;//@Autowiredstatic{users = new HashMap<Integer, User>();users.put(1001, new User(1001,"helen","123","123@qq.com",12,new Address(1,"黑龙江","哈尔冰" )));users.put(1002, new User(1002,"helen","123","123@qq.com",12,new Address(1,"黑龙江","哈尔冰" )));users.put(1003, new User(1003,"helen","123","123@qq.com",12,new Address(1,"黑龙江","哈尔冰" )));}private static Integer initId =1004;public void save(User user) {if(user.getId()==null) {user.setId(initId++);}users.put(user.getId(),user);}public Collection<User> getAll(){return users.values();}public User get(Integer id) {return users.get(id);}public void delete(Integer id) {users.remove(id);}
}

indexController类

package wormday.springmvc.helloworld;import javax.servlet.http.HttpSession;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller
@RequestMapping("/test")  
public class IndexController {@RequestMapping("/index")  public String say(HttpSession session) { System.out.print(session.getId());return "home";}
}

UserController类

package wormday.springmvc.helloworld;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 org.springframework.web.bind.annotation.SessionAttributes;import com.sun.javafx.sg.prism.NGShape.Mode;import wormday.springmvc.dao.UserDao;
import wormday.springmvc.pojo.User;
@Controller
///@RequestMapping("/user")
//@SessionAttributes(value= {"user"},types= {String.class})
public class UserController {@Autowired
private UserDao userDao;@RequestMapping(value="/users",method=RequestMethod.GET)public String list(Model model) {Collection<User> userList=userDao.getAll();model.addAttribute("userList",userList);return "user/list";}@RequestMapping(value="/user/{id}",method=RequestMethod.DELETE)public String delete(@PathVariable(value="id")Integer id) {//执行删除System.out.println("delete"+id);userDao.delete(id);return "redirect:/users";}@RequestMapping(value="/user",method=RequestMethod.GET)public String add() {//执行删除System.out.println("add");return "user/add";}@RequestMapping(value="/user",method=RequestMethod.POST)public String save(User user) {//执行保存System.out.println("save");System.out.print(user);return "redirect:/users";}//编辑@RequestMapping(value="/user/{id}",method=RequestMethod.GET)public String edit(@PathVariable(value="id")Integer id,Model model) {//执行更新System.out.println("edit");User user=userDao.get(id);model.addAttribute("user", user);return "user/edit";}@RequestMapping(value="/user/{id}",method=RequestMethod.PUT)public String update(@PathVariable(value="id")Integer id,User user) {System.out.println("update");user.setId(id);userDao.save(user);return "redirect:/users";}}

Address类

package wormday.springmvc.pojo;public class Address {private Integer id;private String province;public String getProvince() {return province;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public void setProvince(String province) {this.province = province;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}private String city;public Address() {super();}@Overridepublic String toString() {return "Address [id=" + id + ", province=" + province + ", city=" + city + "]";}public Address(Integer id, String province, String city) {super();this.id = id;this.province = province;this.city = city;}}

user类

package wormday.springmvc.pojo;public class User {public User() {super();// TODO Auto-generated constructor stub}private Integer id;private String username;private String password;private String email;private Integer age;private Address address;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address;}@Overridepublic String toString() {return "User [id=" + id + ", username=" + username + ", password=" + password + ", email=" + email + ", age="+ age + ", address=" + address + "]";}public User(Integer id, String username, String password, String email, Integer age, Address address) {super();this.id = id;this.username = username;this.password = password;this.email = email;this.age = age;this.address = address;}}

form.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!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>
${sessionScope.strvalue}
${sessionScope.intvalue}
<form action="save" method="post">
<label for="">用户名:<input type="text" name="username" value="${RequestScope.user.username}"/></label>
<label for="">密码:<input type="text" name="password" value="${user.password}"/></label>
<label for="">年龄:<input type="text" name="age" value="${sessionScope.user.age}"/></label>
<label for="">邮箱:<input type="text" name="email" value="${user.email}"/></label>
<label for="">省份:<input type="text" name="address.province" value="${user.address.province}"/></label>
<label for="">城市:<input type="text" name="address.city" value="${user.address.city}"/></label>
<button>登录</button>
</form>
</body>
</html>

home.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!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>
<h1>nihaohao</h1>
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!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>
<h1>成功</h1>
</body>
</html>

list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>
<h1>用户列表</h1>
<a href="user">增加</a>
<table border="1">
<tr>
<td>id</td>
<td>姓名</td>
<td>年龄</td>
<td>操作</td>
</tr>
<c:forEach items="${userList}" var="user"><tr>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.age}</td>
<td>
<a href="javascript:void(0)" onclick="deleteById(${user.id})">删除</a>
<a href="javascript:void(0)" onclick="updateById(${user.id})">修改</a>
</td>
</tr>
</c:forEach>
</table>
<%-- <a href="javascript:void(0)" onclick="deleteById(${user.id})">删除</a>
<a href="javascript:void(0)" onclick="updateById(${user.id})">修改</a> --%>
<form action="" method="post" id="deleteForm">
<input type="" name="_method" value="DELETE"/>
<button>delete提交</button>
</form>
<form action="" method="post" id="updateForm">
<input type="text" name="_method" value="PUT"/>
<button>update提交</button>
</form><script>
function deleteById(id){var form= document.getElementById('deleteForm');form.action = "user/"+id;document.getElementById('deleteForm').submit(); }
function updateById(id){var form= document.getElementById('updateForm');form.action = "user/"+id;document.getElementById('updateForm').submit();
}
</script>
</body>
</html>

add.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!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 action="${pageContext.request.contextPath}/user/" method="post">
<label for="">用户名:<input type="text" name="username" value="${user.username}"/></label><br>
<label for="">密码:<input type="text" name="password" value="${user.password}"/></label><br>
<label for="">年龄:<input type="text" name="age" value="${user.age}"/></label><br>
<label for="">邮箱:<input type="text" name="email" value="${user.email}"/></label><br>
<label for="">省份:<input type="text" name="address.province" value="${user.address.province}"/></label><br>
<label for="">城市:<input type="text" name="address.city" value="${user.address.city}"/></label><br>
<button>保存</button>
</form>
</body>
</html>

edit.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!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 action="${pageContext.request.contextPath}/user/${user.id}" method="post">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="id" value="${user.id}">
<label for="">用户名:<input type="text" name="username" value="${user.username}"/></label><br>
<label for="">密码:<input type="text" name="password" value="${user.password}"/></label><br>
<label for="">年龄:<input type="text" name="age" value="${user.age}"/></label><br>
<label for="">邮箱:<input type="text" name="email" value="${user.email}"/></label><br>
<label for="">省份:<input type="text" name="address.province" value="${user.address.province}"/></label><br>
<label for="">城市:<input type="text" name="address.city" value="${user.address.city}"/></label><br>
<button>保存</button>
</form>
</body>
</html>

运行结果

点击修改

点击保存

 

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/424004.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

那些年用过的Redis集群架构(含面试解析)

引言 今天&#xff0c;我接到了高中同学刘有码面试失利的消息。 他面试的时候&#xff0c;身份是某知名公司的小码农一枚&#xff0c;却因为不懂自己生产上Redis是如何部署的&#xff0c;导致面试失败&#xff01; 人间惨剧&#xff0c;莫过于此。 接到他面试失利的消息&#x…

再谈BERT

三次讲到了BERT。第一次是nlp中的经典深度学习模型(二)&#xff0c;第二次是transformer & bert &GPT&#xff0c;这是第三次。 文章目录1 关于预训练模型1.1预训练概念1.2 再谈语言模型1.3 ELMo1.4 GPT2 BERT2.1 BERT特点2.2架构2.3 预训练任务2.3.1 masked language …

第一百三十三期:MySQL锁会不会,你就差看一看咯

本文章向大家介绍MySQL锁详细讲解&#xff0c;包括数据库锁基本知识、表锁、表读锁、表写锁、行锁、MVCC、事务的隔离级别、悲观锁、乐观锁、间隙锁GAP、死锁等等&#xff0c;需要的朋友可以参考一下。 作者&#xff1a;php自学中心 本文章向大家介绍MySQL锁详细讲解&#xff…

[导入][你必须知道的.NET]第十回:品味类型---值类型与引用类型(下)-应用征途...

摘要: 本文将值类型和引用类型的讨论从应用示例角度来进一步做以延伸&#xff0c;可以看作是对前两回的补充性探讨。我们从类型定义、实例创建、参数传递、类型判等、垃圾回收等几个方面来简要的对上两回的内容做以剖析&#xff0c;并以一定的IL语言和内存机制来说明&#xff0…

云开发新能力,支持 HTTP 调用 API

今天来上班打开电脑&#xff0c;总感觉微信开发文档哪里有点不太一样&#xff0c;研究了半天原来是云开发又多了神级功能——HTTP API&#xff01; HTTP API是什么&#xff1f;简单来说就是通过云开发HTTP API&#xff0c;可以不需要通过微信小程序或云开发控制台&#xff0c;就…

mac下pip install 安装只能选择python2.7的问题

mac自带python2.7。 我用brew install安装过python3:brew install python3 我安装了anaconda3。因为它自带了很多包。所以我无论是用pycharm编程还是jupyter notebook一直都用的是anaconda3带的python。 后来发现用pip install 安装的包&#xff0c;在pycharm工程中用不了。更有…

第一百三十四期:MySQL分页查询方法及优化

在MySQL中&#xff0c;分页查询一般都是使用limit子句实现&#xff0c;limit子句声明如下&#xff1a;LIMIT子句可以被用于指定 SELECT 语句返回的记录数。 作者&#xff1a;青芽草 分页查询方法&#xff1a; 在MySQL中&#xff0c;分页查询一般都是使用limit子句实现&#x…

One2One主键关联的实现

主键关联&#xff0c;产生主键的是A类&#xff0c;与A相同主键的是B类 主键对应的A类&#xff0c;hbm.xml文件与.cs文件均无任何特别&#xff0c;与单表相同 与A相同的主键B类&#xff0c; Hbm.xml文件&#xff1a; <id name"Id"column"ID"type"Gu…

scroll

因为想赶紧开始敲考核任务的&#xff0c;所以就跳着来学 1. window.pageYOffset可以获取滚动了的高度 2.转载于:https://www.cnblogs.com/lijingjaj/p/11206841.html

第一百三十五期:如何模拟一次阿里双11秒杀场景的实现?程序员必看

秒杀活动可以说在互联网上随处可见&#xff0c;从12306抢票&#xff0c;到聚划算抢购&#xff0c;我们生活的方方面面都可以看到秒杀的身影。 作者&#xff1a;IT技术管理那些事儿 秒杀活动可以说在互联网上随处可见&#xff0c;从12306抢票&#xff0c;到聚划算抢购&#xf…

什么是“中台”

“中台”概念起源于军事范畴&#xff0c;其精髓是“大平台支撑精兵作战”&#xff0c;即前线小团队作战&#xff0c;后方建设强大的火力平台和信息化指挥系统机动支撑。企业中台战略随着Supercell、阿里巴巴、腾讯、华为等国内外科技巨头的应用和宣传&#xff0c;近年来在国内被…

第一百三十六期:详细讲解 Redis 的两种安装部署方式

Redis 是一款比较常用的 NoSQL 数据库&#xff0c;我们通常使用 Redis 来做缓存&#xff0c;这是一篇关于 Redis 安装的文章&#xff0c;所以不会涉及到 Redis 的高级特性和使用场景&#xff0c;Redis 能够兼容绝大部分的 POSIX 系统。 作者&#xff1a;平头哥 Redis 是一款比…

知识图谱需要解决的问题

1 知识图谱应用场景 1、数据可视化 2、基于图谱的问答系统 3、基于图谱的关系推理 4、便捷的关系查询&#xff0c;给模型提供更多数据特征 2 知识图谱的构建 非结构化数据源中的实体识别&#xff1a;一般来说是一个sequence labeling的任务。 非结构化数据源中的关系抽取&am…

spring mvc学习(43):处理静态资源

上图是目录结构&#xff0c;本节是有问同学的&#xff0c;当好好总结 pom.xml <project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation"http://maven.apache.org/POM/4.0.…

每天学一点flash(14) as3.0 处理xml (官方)

把官方的教程贴上来&#xff0c;我觉得还是不错的说得很有条理&#xff1a; 转载于:https://www.cnblogs.com/guoyiqi/archive/2007/08/21/2069585.html

Spring AOP学习笔记

需明确的几个概念: l 通知(Advice)&#xff1a;用于告知系统将有哪些新的行为。l 切入点(Pointcut):定义了通知应该在应用到那些连接点。l 目标对象(Target)&#xff1a;被通知的对象。l 代理(Proxy)&#xff1a;将通知应用到目标对象后创建的…

spring mvc学习(44):springMVC运行原理

springMVC处理请求的流程 SpringMVC的工作原理图&#xff1a; SpringMVC流程 1、 用户发送请求至前端控制器DispatcherServlet。 2、 DispatcherServlet收到请求调用HandlerMapping处理器映射器。 3、 处理器映射器找到具体的处理器(可以根据xml配置、注解进行查找)&…

一段按页自动滚动文字或图片的Js代码

<div iddemo style"position:relative;padding:10px;border:solid 1px green;BACKGROUND: #ffffff; OVERFLOW: hidden; WIDTH: 510px; COLOR: red; HEIGHT: 100px"><div id"demo1"style"position:relative;">您的图片或者要滚动的内…

spring mvc学习(45):springMVC的三大组件

SpringMvc框架结构图 处理器映射器&#xff1a;用户请求路径到Controller方法的映射处理器适配器&#xff1a;根据handler(controlelr类&#xff09;的开发方式&#xff08;注解开发/其他开发&#xff09; 方式的不同区寻找不同的处理器适配器 视图解析器&#xff1a;根据hand…

[NLP-CNN] Convolutional Neural Networks for Sentence Classification -2014-EMNLP

1. Overview 本文将CNN用于句子分类任务 (1) 使用静态vector CNN即可取得很好的效果&#xff1b;> 这表明预训练的vector是universal的特征提取器&#xff0c;可以被用于多种分类任务中。 (2) 根据特定任务进行fine-tuning 的vector CNN 取得了更好的效果。 (3) 改进模型架…