SpringMVC-RestFul风格

RestFul特点&功能:

  • 每一个URL代表一种资源;
  • 客户端使用GET(获取资源)、POST(新建/更新资源)、PUT(更新资源)、DELETE(删除资源)四个操作方式对服务器资源进行操作;
  • 通过操作资源的表现形式来操作资源;
  • 资源的表现形式是XML/HTML;
  • 客户端与服务端之间的交互形式在请求之间是无状态的且客户端到服务端的每个请求都必须包含理解该请求所必需的信息。

网址对比

  • 传统方法操作资源(post/get)
http://localhost/item/qureyitem.action?id=1
http://localhost/item/saveitem.action
http://localhost/item/updateitem.action?id=1
http://localhost/item/deleteitem.action?id=1
  • RestFul风格操作资源(相同地址可实现不同功能)
http://localhost/item/1(query)
http://localhost/item(add)
http://localhost/item(update)
http://localhost/item/1(delete)

结合SpringMVC实现

传统方法传参

  • 包cn.iris.controller下编写一个Class RestFulController

package cn.iris.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
/**
 * @author Iris 2021/8/16
 */
public class RestFulController {

    @RequestMapping("/add")
    public String testMethod(int a, int b, Model model) {

        int res = a+b;
        model.addAttribute("msg","res = "+res);

        return "test";
    }

}
  • 配置Tomcat跑起来~

执行传统网址http://localhost:8080/springmvc/add?a=1&b=2,得到网页输出:res = 3

RestFul风格传参

  • Class RestFulController修改

    • @PathVariable注解将方法参数定义为页面传参,可在@RequestMapping参数添加URL后缀以实现传参(如:http://localhost:8080/springmvc/add/1/2
package cn.iris.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
/**
 * @author Iris 2021/8/16
 */
public class RestFulController {

    @RequestMapping("/add/{a}/{b}")
    public String testMethod(@PathVariable int a,@PathVariable int b, Model model) {

        int res = a+b;
        model.addAttribute("msg","res = "+res);

        return "test";
    }

}
  • 运行Tomcat

执行RestFul风格网址【http://localhost:8080/springmvc/add/99/100】,得到网页输出:res = 199

RestFul风格实现相同URL不同功能

  • 【编写Class RestFulController】

package cn.iris.controller;

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;

@Controller
/**
 * @author Iris 2021/8/16
 */
public class RestFulController {

    @RequestMapping(value = "/add/{a}/{b}", method = RequestMethod.GET)
    public String testMethod(@PathVariable int a,@PathVariable int b, Model model) {

        int res = a+b;
        model.addAttribute("msg","res = "+res);

        return "test";
    }

    @RequestMapping(value = "/add/{a}/{b}", method = RequestMethod.POST)
    public String testMethod2(@PathVariable String a, @PathVariable String b, Model model) {

        boolean res = a.equals(b);

        model.addAttribute("msg", "res : "+res);

        return "test";
    }

}
  • 【在WEB-INF/jsp编写一个提交POST请求的表单--pwd.jsp】

<%--
  Created by IntelliJ IDEA.
  User: asus
  Date: 2021/8/16
  Time: 12:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<form action="/add/abd/abd" method="post">
    <input type="submit">
</form>

</body>
</html>
  • 【启动Tomcat测试】

分别进行【http://localhost:8080/springmvc/add/1/2】&【提交表单操作】;
分别得到网页结果:res = 3&res : true

RestFul优点

  • 简洁
  • 高效(支持缓存)
  • 安全(一定程度隐藏了参数名)
posted @ 2021-08-16 17:07  菜鸢爱敲bug  阅读(54)  评论(0编辑  收藏  举报