spring mvc学习(40):restful的crud实现增加方式

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

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 java.util.Map;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;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 org.springframework.web.servlet.ModelAndView;import com.sun.javafx.sg.prism.NGShape.Mode;import wormday.springmvc.dao.UserDao;
import wormday.springmvc.pojo.Address;
import wormday.springmvc.pojo.User;
@Controller
@RequestMapping("/user")
@SessionAttributes(value= {"user"},types= {String.class})
public class UserController {@Autowired
private UserDao userDao;@RequestMapping("/form")public String form() {return "form";}@RequestMapping("/edit")public ModelAndView edit(HttpSession session) {User user = new User();user.setUsername("helen");user.setPassword("123");user.setAge(12);user.setEmail("11@qq.com");Address address = new Address();address.setProvince("anhui");address.setCity("haha");user.setAddress(address);ModelAndView mv = new ModelAndView();mv.setViewName("form");mv.addObject("user", user);return mv;}@RequestMapping("/edit1")public String edit1(Map<String,Object> map) {User user = new User();user.setUsername("helen111");user.setPassword("123");user.setAge(12);user.setEmail("11@qq.com");Address address = new Address();address.setProvince("jiangsu");address.setCity("hah");user.setAddress(address);map.put("user",user);return "form";}@RequestMapping("/edit2")public String edit2(Model model) {User user = new User();user.setUsername("helen111");user.setPassword("123");user.setAge(12);user.setEmail("11@qq.com");Address address = new Address();address.setProvince("jiangsu");address.setCity("hah");user.setAddress(address);model.addAttribute("user",user);model.addAttribute("strvalue","gp1701");model.addAttribute("intvalue",1701);return "form";///删除}@RequestMapping(value="/list",method=RequestMethod.GET)public String list(Model model) {Collection<User> userList=userDao.getAll();model.addAttribute("userList",userList);return "user/list";}@RequestMapping(value="delete")public String delete(Integer id) {//执行删除System.out.println("delete"+id);userDao.delete(id);return "redirect:/user/list";}@RequestMapping(value="add",method=RequestMethod.GET)public String add() {//执行删除System.out.println("add");return "user/add";}@RequestMapping("/")public String save() {//执行删除System.out.println("save");return "redirect:/user/add";}@RequestMapping(value="/{id}",method=RequestMethod.PUT)public String update() {//执行更新System.out.println("update");return "redirect:/user/list";}}

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="add">增加</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="1" method="post" id="deleteForm">
<input type="text" name="_method" value="DELETE"/>
<button>delete提交</button>
</form>
<form action="1" 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 = id;document.getElementById('deleteForm').submit(); */debugger;var url='http://127.0.0.1:8066/springmaven01/user/delete?id='+id;window.location=url;
}
function updateById(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>

运行结果

点击增加

点击
保存

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

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

相关文章

spring mvc学习(41):restful的crud的项目原型介绍

上图是目录结构&#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.…

nlp中的经典模型(三)

文章目录5 NLP中的卷积神经网络5.1 卷积5.2 多通道5.2 max pooling5 NLP中的卷积神经网络 RNN的问题&#xff1a; 1 时间复杂度高 2 最后一个向量包含所有信息。有点不可靠 CNN可以通过卷积核捕捉局部特征&#xff0c;那是不是可以用于句子&#xff0c;表示特定长度的词序列呢…

第一百二十九期:阿里内部员工,排查Java问题常用的工具单

平时的工作中经常碰到很多疑难问题的处理&#xff0c;在解决问题的同时&#xff0c;有一些工具起到了相当大的作用&#xff0c;在此书写下来&#xff0c;一是作为笔记&#xff0c;可以让自己后续忘记了可快速翻阅&#xff0c;二是分享&#xff0c;希望看到此文的同学们可以拿出…

第一百三十期:14种常见编程语言的优缺点及应用范围

C语言是一门通用计算机编程语言&#xff0c;应用广泛。面向过程的&#xff0c;数据与算法分开。它的重点在于算法和数据结构。1972年由美国贝尔实验室在B语言的基础上设计出。 作者&#xff1a;编程小新 C 概述:C语言是一门通用计算机编程语言&#xff0c;应用广泛。面向过程…

第一百三十一期:2019年容器使用报告:Docker 和 Kubernetes 王者地位不倒!

近日&#xff0c;容器创业公司 Sysdig 发布了 2019 年容器使用报告。这是 Sysdig 第三年发布容器年度使用报告&#xff0c;与之前不同的是&#xff0c;今年的调查结合了更多的数据源&#xff0c;并深入挖掘了 Kubernetes 的使用模式。 作者&#xff1a;高效开发运维 近日&…

Flask 路由映射对于双斜线的处理 //a//b

例子 from flask import Flask import time from tornado.wsgi import WSGIContainer from tornado.httpserver import HTTPServer from tornado.ioloop import IOLoopapp Flask(__name__)app.route(//abc//a) def index():# time.sleep(5)return OKapp.route(/abc//a) def in…

⼤规模⽆监督预训练语⾔模型与应⽤(上)

文章目录1 单词作为语言模型的基本单位的缺点2 character level modeling3预训练句子向量3.1 skip-thought3.2 InferSent3.3 句子向量评价数据集4 预训练文档向量5 ELMO1 单词作为语言模型的基本单位的缺点 单词量有限&#xff0c;遇到没有见过的单词只能以UNK表示。 模型参数…

第一百三十二期:MySQL系列:一句SQL,MySQL是怎么工作的?

当我们在mysql窗口或者数据库连接工具中输入一句sql后&#xff0c;我们就可以获取到想要的数据&#xff0c;这中间MySQL到底是怎么工作的呢&#xff1f; 作者&#xff1a;Java架构学习交流 对于MySQL而言&#xff0c;其实分为客户端与服务端。 服务端&#xff0c;就是MySQL应…

Visual Studio 2005 Tip:编辑项目文件

原文参考自&#xff1a;http://blogs.msdn.com/shawnfa/archive/2006/04/26/582326.aspx很多时候我们需要手动修改VS的项目文件&#xff08;.csproj/.vbproj&#xff09;&#xff0c;这时大多数人会简单的使用记事本&#xff08;notepad&#xff09;打开并编辑。虽然这没什么不…

transformer bert GPT(未完)

原文标题&#xff1a;⼤规模⽆监督预训练语⾔模型与应⽤&#xff08;中&#xff09; 文章目录1 transformer1.1 encoder部分1.1.1 Attention定义1.1.2 Multi-head Attention1.1.3 position-wise feed-forward networks1.1.4 positional encoding1.1.5 残差链接1.1.6 layer norm…

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

上图是目录结构&#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.…

那些年用过的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