springMvc41-restful的编辑功能实现

简介: springMvc41-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.xsd
             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
             http://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;
         //@Autowired
         static{
             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();
         }
         @Override
         public 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;
         }
         @Override
         public 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)" οnclick="deleteById(${user.id})">删除</a>
     <a href="javascript:void(0)" οnclick="updateById(${user.id})">修改</a>
     </td>
     </tr>
     </c:forEach>
     </table>
     <%-- <a href="javascript:void(0)" οnclick="deleteById(${user.id})">删除</a>
     <a href="javascript:void(0)" οnclick="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>

image.png

相关文章
|
11月前
|
Java API 数据库
构建RESTful API已经成为现代Web开发的标准做法之一。Spring Boot框架因其简洁的配置、快速的启动特性及丰富的功能集而备受开发者青睐。
【10月更文挑战第11天】本文介绍如何使用Spring Boot构建在线图书管理系统的RESTful API。通过创建Spring Boot项目,定义`Book`实体类、`BookRepository`接口和`BookService`服务类,最后实现`BookController`控制器来处理HTTP请求,展示了从基础环境搭建到API测试的完整过程。
149 4
|
前端开发 Java Apache
JAVAEE框架技术之6-springMVC拦截器和文件上传功能
JAVAEE框架技术之6-springMVC拦截器和文件上传功能
167 0
JAVAEE框架技术之6-springMVC拦截器和文件上传功能
|
缓存 NoSQL Java
在 SSM 架构(Spring + SpringMVC + MyBatis)中,可以通过 Spring 的注解式缓存来实现 Redis 缓存功能
【6月更文挑战第18天】在SSM(Spring+SpringMVC+MyBatis)中集成Redis缓存,涉及以下步骤:添加Spring Boot的`spring-boot-starter-data-redis`依赖;配置Redis连接池(如JedisPoolConfig)和连接工厂;在Service层使用`@Cacheable`注解标记缓存方法,指定缓存名和键生成策略;最后,在主配置类启用缓存注解。通过这些步骤,可以利用Spring的注解实现Redis缓存。
148 2
|
JSON 数据格式
MysbatisPlus-核心功能-IService开发基础业务接口,MysbatisPlus_Restful风格,新增@RequestBody指定是为了接收Json数据的,使用swagger必须注解
MysbatisPlus-核心功能-IService开发基础业务接口,MysbatisPlus_Restful风格,新增@RequestBody指定是为了接收Json数据的,使用swagger必须注解
|
前端开发 Java Maven
如何在Spring MVC中实现图片的上传和下载功能
如何在Spring MVC中实现图片的上传和下载功能
|
移动开发 Java 测试技术
Spring MVC+Spring+Mybatis实现支付宝支付功能(附完整代码)
Spring MVC+Spring+Mybatis实现支付宝支付功能(附完整代码)
328 1
|
存储 JSON 前端开发
SpringMVC04 实现简单的留言墙功能
SpringMVC04 实现简单的留言墙功能
115 0
|
JSON 安全 API
Flask-Login与Flask-RESTful:扩展你的应用功能
【4月更文挑战第16天】本文介绍了两个实用的Flask扩展——Flask-Login和Flask-RESTful。Flask-Login提供用户认证和会话管理,简化了登录、注销和保护路由的逻辑。而Flask-RESTful则助力构建RESTful API,支持多种HTTP方法和请求解析。通过这两个扩展,开发者能轻松增强Flask应用的功能性,实现安全的用户认证和高效的API交互。
|
JSON 前端开发 Java
Spring MVC响应结合RESTful风格开发,打造具有强大功能和良好体验的Web应用!
Spring MVC响应结合RESTful风格开发,打造具有强大功能和良好体验的Web应用!
|
前端开发 Java 应用服务中间件
SpringMVC项目图片处理功能总结
SpringMVC项目图片处理功能总结
131 1

热门文章

最新文章