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

RESTful风格

(一)什么是RESTful

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

(2)url更加简洁

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

(二)概述

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

GET:获取资源

POST:新建资源

PUT:修改资源

DELETE:删除资源

(三)实例

(1)功能

1.数据的增删改查

2.controller层的应用

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

SpringMVC学习笔记之---RESTful风格_第1张图片

(2)代码实现

1.pom.xml

<dependencies>
  <dependency>
    <groupId>junitgroupId>
    <artifactId>junitartifactId>
    <version>4.11version>
  dependency>
  <dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-webmvcartifactId>
    <version>5.1.3.RELEASEversion>
  dependency>
  <dependency>
    <groupId>javax.servletgroupId>
    <artifactId>jstlartifactId>
    <version>1.2version>
  dependency>
dependencies>

 

2.web.xml

<web-app>

  <display-name>Archetype Created Web Applicationdisplay-name>

  <servlet>

    <servlet-name>SpringMVCservlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>

    <init-param>

      <param-name>contextConfigLocationparam-name>

      <param-value>classpath:springmvc.xmlparam-value>

    init-param>

  servlet>

  <servlet-mapping>

    <servlet-name>SpringMVCservlet-name>

    <url-pattern>/url-pattern>

  servlet-mapping>



  

  <filter>

    <filter-name>hiddenHttpMethodFilterfilter-name>

    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>

  filter>

  <filter-mapping>

    <filter-name>hiddenHttpMethodFilterfilter-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 map=new HashMap();



    /**

     * 增加

     * @param user

     */

    public void add(User user){

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

    }



    /**

     * 查询所有

     * @return

     */

    public Collection 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>Titletitle>

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

head>

<body>

<table>

    <tr>

        <td>idtd>

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

                    

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

head>

<body>

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

<table>

    <tr>

        <td>idtd>

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

    

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

    <input type="submit">

form>

body>

html>

 

你可能感兴趣的:(SpringMVC学习笔记之---RESTful风格)