SpringMVC学习笔记之---RESTful风格

RESTful风格

(一)什么是RESTful

(1)RESTful不是一套标准,只是一套开发方式,构架思想

(2)url更加简洁

(3)有利于不同系统之间的资源共享

(二)概述

RESTful具体来讲就是HTTP协议的四种形式,四种基本操作

GET:获取资源

POST:新建资源

PUT:修改资源

DELETE:删除资源

(三)实例

(1)功能

1.数据的增删改查

2.controller层的应用

3.HTTP四种基本操作的应用

(2)代码实现

1.pom.xml

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.3.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
  </dependency>
</dependencies>

 

2.web.xml

<web-app>

  <display-name>Archetype Created Web Application</display-name>

  <servlet>

    <servlet-name>SpringMVC</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>

  </servlet>

  <servlet-mapping>

    <servlet-name>SpringMVC</servlet-name>

    <url-pattern>/</url-pattern>

  </servlet-mapping>



  <!--过滤器,将请求转换为标准的http方法,使得支持GET,POST,PUT,DELETE请求-->

  <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>

 

3.springmvc.xml

<?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"

       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.xsd">

<!--包扫描-->

    <context:component-scan base-package="controller,dao"></context:component-scan>

    <!--视图解析器-->

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <!--前缀-->

        <property name="prefix" value="/"></property>

        <!--后缀-->

        <property name="suffix" value=".jsp"></property>

    </bean>



</beans>

 

4.User.java

package entiry;



public class User {

    private int id;

    private String name;

    private String password;



    public int getId() {

        return id;

    }



    public void setId(int id) {

        this.id = id;

    }



    public String getName() {

        return name;

    }



    public void setName(String name) {

        this.name = name;

    }



    public String getPassword() {

        return password;

    }



    public void setPassword(String password) {

        this.password = password;

    }



    @Override

    public String toString() {

        return "User{" +

                "id=" + id +

                ", name='" + name + '\'' +

                ", password='" + password + '\'' +

                '}';

    }

}

 

5.UserDao.java

package dao;



import entiry.User;

import org.springframework.stereotype.Repository;



import java.util.Collection;

import java.util.HashMap;

import java.util.Map;

@Repository

public class UserDao {

    private Map<Integer, User> map=new HashMap<Integer, User>();



    /**

     * 增加

     * @param user

     */

    public void add(User user){

        map.put(user.getId(),user);

    }



    /**

     * 查询所有

     * @return

     */

    public Collection<User> selectAll(){

        return map.values();

    }



    /**

     * 通过id查询

     * @param id

     * @return

     */

    public User select(int id){

        return map.get(id);

    }



    /**

     * 修改

     * @param user

     */

    public void update(User user){

        map.put(user.getId(),user);

    }



    /**

     * 删除

     * @param id

     */

    public void delete(int id){

        map.remove(id);

    }

}

 

6.UserController.java

package controller;



import dao.UserDao;

import entiry.User;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;



@Controller

public class UserController {

    @Autowired

    private UserDao userDao;

    @PostMapping("/add")

    public String add(User user){

       userDao.add(user);

       //重定向到selectAll

       return "redirect:/selectAll";

    }

    @GetMapping("/selectAll")

    public ModelAndView selectAll(){

        ModelAndView modelAndView=new ModelAndView();

        modelAndView.addObject("users",userDao.selectAll());

        modelAndView.setViewName("select");

        for(User user:userDao.selectAll()){

            System.out.println(user);

        }

        return modelAndView;

    }

    @GetMapping("/select/{id}")

    public ModelAndView select(@PathVariable(value="id") int id){

        ModelAndView modelAndView=new ModelAndView();

        modelAndView.setViewName("update");

        modelAndView.addObject("user",userDao.select(id));

        return modelAndView;

    }

    @PutMapping("/update")

    public String update(User user){

        userDao.update(user);

        return "redirect:/selectAll";

    }

    @DeleteMapping("/delete/{id}")

    public String delete(@PathVariable(value="id")int id){

        userDao.delete(id);

        return "redirect:/selectAll";

    }

}

 

7.add.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

<head>

    <title>Title</title>

</head>

<body>

<form action="${pageContext.request.contextPath}/add" method="post">

    id:<input type="text" name="id"><br>

    用户名:<input type="text" name="name"><br>

    密码:<input type="text" name="password"><br>

    <input type="submit">

</form>

</body>

</html>

 

8.select.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@page isELIgnored="false" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<html>

<head>

    <title>Title</title>

</head>

<body>

<table>

    <tr>

        <td>id</td>

        <td>用户名</td>

        <td>密码</td>

        <td></td>

        <td></td>

    </tr>

    <c:forEach items="${users}" var="user">

        <tr>

            <td>${user.id}</td>

            <td>${user.name}</td>

            <td>${user.password}</td>

            <td>

                <form action="${pageContext.request.contextPath}/select/${user.id}"method="get">

                    <button type="submit">修改</button>

                </form>

            </td>

            <td>

                <form action="${pageContext.request.contextPath}/delete/${user.id}" method="post">

                    <!--将请求的方式设为DELETE-->

                    <input type="hidden" name="_method" value="DELETE">

                <button type="submit" >删除</button>

                </form>

            </td>

        </tr>

    </c:forEach>

</table>

</body>

</html>

 

9.update.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

<head>

    <title>Title</title>

</head>

<body>

<form action="${pageContext.request.contextPath}/update" method="post">

<table>

    <tr>

        <td>id</td>

        <td><input type="text" name="id" value="${user.id}" readonly="readonly"></td>

    </tr>

    <tr>

        <td>用户名</td>

        <td><input type="text" name="name" value="${user.name}"></td>

    </tr>

    <tr>

        <td>密码</td>

        <td><input type="text" name="password" value="${user.password}"></td>

    </tr>

</table>

    <!--将请求的方式设为PUT-->

    <input type="hidden" name="_method" value="PUT">

    <input type="submit">

</form>

</body>

</html>

 

posted @ 2019-08-09 15:50  豆丁zzz  阅读(436)  评论(0编辑  收藏  举报