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

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 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) {//执行更新System.out.println("edit");return "redirect:/user/edit";}@RequestMapping(value="/user/{id}",method=RequestMethod.PUT)public String update(@PathVariable(value="id")Integer id) {//执行更新System.out.println("update");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>

运行结果

点击删除

点击增加

点击保存

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

  1. spring mvc学习(60):ssm项目整合

    SSM整合 建立springmvc项目,先跑起来,再整合spring和mybatis 一.SpringMVC建立 1.新建maven工程,安装tomcat 2.导入pom <!-- spring ...

  2. Spring MVC学习总结(5)——SpringMVC项目关于安全的一些配置与实现方式

    目前越来越多的应用和网站,开始注重安全性的问题,关于我们的web项目的几个安全知识点,不得不讲解一下,这里我主要讲述关于tomcat如何支持HTTPS连接访问,RSA公钥和私钥的制作.这个对于我们整个 ...

  3. Spring MVC学习(8)—HandlerInterceptor处理器拦截器机制全解

    基于最新Spring 5.x,详细介绍了Spring MVC的HandlerInterceptor处理器拦截器机制,以及它的一系列拦截方法. 本次我们来学习Sring MVC的HandlerInter ...

  4. Spring MVC 学习总结(九)——Spring MVC实现RESTful与JSON(Spring MVC为前端提供服务)...

    Spring MVC 学习总结(九)--Spring MVC实现RESTful与JSON(Spring MVC为前端提供服务) 目录 一.JSON 1.1.概要 1.2.使用ModelAndView ...

  5. Spring MVC 学习总结(一)——MVC概要与环境配置 转载自【张果】博客

    Spring MVC 学习总结(一)--MVC概要与环境配置 目录 一.MVC概要 二.Spring MVC介绍 三.第一个Spring MVC 项目:Hello World 3.1.通过Maven新 ...

  6. Spring MVC 学习总结(五)——校验与文件上传 转自 张果 博客;已经编程校验;正确无误;...

    Spring MVC 学习总结(五)--校验与文件上传 目录 一.Spring MVC验证器Validator 1.1.定义验证器 1.2.执行校验 1.3.在UI中添加错误标签 1.4.测试运行 二 ...

  7. Spring MVC 学习笔记 对locale和theme的支持

    Spring MVC 学习笔记 对locale和theme的支持 Locale Spring MVC缺省使用AcceptHeaderLocaleResolver来根据request header中的 ...

  8. Spring MVC 学习总结(二)——控制器定义与@RequestMapping详解

    Spring MVC 学习总结(二)--控制器定义与@RequestMapping详解 目录 一.控制器定义 1.1.实现接口Controller定义控制器 1.2.使用注解@Controller定义 ...

  9. 使用Spring MVC 4构建Restful服务

    使用Spring MVC 4构建RESTful服务相对于其它框架来说,有很多优势.首先,Spring MVC 4作为Spring的框架之一,可以很好地与Spring进行集成.其次,Spring MVC ...

最新文章

  1. The 13th Zhejiang Provincial Collegiate Contest(2016年浙江省赛)
  2. linux配置ssh免密码,Linux下配置SSH免密通信 - “ssh-keygen”的基本用法
  3. 【pmcaff】产品经理必知改善用户体验的基本方法
  4. .Net Core小技巧 - Swagger适配虚拟目录及二级目录
  5. 用友服务器系统版本低,客户端版本低于服务器端,请升级后再登录
  6. 【转】DB2 常用命令
  7. mingw64+msys2下使用cmake问题
  8. origin对数据进行操作
  9. 在浏览器用域名访问发现跳转到IIS Windows 界面
  10. Php真太阳时计算公式,李顺祥:真太阳时的计算与应用
  11. Python里面这些点,据说80%的新手都会一脸懵逼
  12. WIN10 如何隐藏桌面图标
  13. 人工智能驱动的智能制造(人工智能系列)
  14. win10笔记本cpu不高内存也不高但就是很卡,一个办法帮你解决!
  15. iOS-申请邓白氏编码的超详细流程介绍
  16. 【2019-游记】中山纪念中学暑期游Day5
  17. 阿里云服务器安装图形化界面(CentOS)
  18. 计算机高级技师证怎么考?
  19. 爬虫实践:爬取搜狗图片
  20. 单反常用的快门速度大概是多少

热门文章

  1. 2个字节能存多少个16进制_Java语言中最大的整数再加1等于多少?看完秒懂
  2. sql server 里面怎么支持数字使用双引号_国查:用中文编写SQL
  3. oracle 11gogg,【OGG】Oracle GoldenGate 11g (二) GoldenGate 11g 单向同步配置 上
  4. 双鉴探测器是哪两种探测方式结合_老师傅带你看懂火灾探测器的种类和基本原理,看完涨知识了...
  5. element手机验证格式_vue封装 element-ui form表单验证 正则匹配手机号 自定义校验表格内容...
  6. matlab图像增强分段线性函数_图像增强、锐化,利用 PythonOpenCV 来实现 4 种方法!...
  7. MIT算法导论(一)——算法分析和引论
  8. 计算机动画的主要应用领域,简述计算机的主要特点和主要应用领域
  9. 静态库和动态库的分析
  10. 深入了解crc32算法