四、@PathVariable

rest风格

  • 资源定位及资源操作的风格
  • 不是协议,可以遵循,也可以不遵循

REST风格请求

  • REST 即 Representational State Transfer (资源)表现层状态转化
  • 用URL定位资源,用HTTP描述操作
  • 是目前最流行的一种互联网软件架构
  • 它结构清晰、符合标准、易于理解、扩展方便,所以正得到越来越多网站的采用
  • 使用POST, DELETE, PUT, GET 分别对应 CRUD
  • Spring3.0 开始支持 REST 风格的请求

传统的操作资源

http://localhost:8080/get.action?id=10        查询 get
http://localhost:8080/add.action                        新增 post
http://localhost:8080/update.action                   修改 post
http://localhost:8080/delete.action?id=10          删除 post

restful操作资源(使用1直接作为查询id)

http://localhost:8080/goods/1       查询GET
http://localhost:8080/goods     新增POST
http://localhost:8080/goods     更新PUT
http://localhost:8080/goods/1       删除DELETE

使用@PathVariable接收RestFul风格参数:

restful风格第一个Demo

用URL定位资源

<a href="${pageContext.request.contextPath}/rest/1">restful风格</a>
@Controller
public class MyController {
    @RequestMapping("/rest/{id}")
    public String restful01(@PathVariable Integer id) {
        System.out.println("rest风格:id="+id);
        return "/second.jsp";
    }
}
// rest风格:id=1

发送put与delete请求

  • 存在问题

默认情况下Form表单是不支持PUT请求和DELETE请求的!

  • 解决方案一:

spring3.0添加了一个过滤器HiddenHttpMethodFilter,可以将post请求转换为PUT或DELETE请求。

  • 配置过滤器web.xml
<!--解决restful风格发送put和delete请求的问题-->
<filter>
    <filter-name>hiddenmethod</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>hiddenmethod</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
  • 发送请求
<form action="${pageContext.request.contextPath}/rest/2" method="post">
    <%--定义一个隐藏表单,name值必须为_method,value则是请求方式--%>
    <input type="hidden" name="_method" value="put">
    <input type="submit" value="rest" >
</form>
  • 服务器接收处理
@Controller
public class MyController {
    @RequestMapping(value = "/rest/{id}",method = RequestMethod.PUT)
    public String restful01(@PathVariable Integer id) {
        System.out.println("rest风格:id="+id);
        // JSPs only permit GET POST or HEAD,使用重定向
        return "redirect:/second.jsp";
    }
}
  • 解决方案二:不常用

在JSP页面第一行添加:isErrorPage="true"

注入事项

  • 从tomcat8开始如果直接返回jsp页面,会报405错误 JSPs only permit GET POST or HEAD
  • 使用重定向的形式跳转到对应jsp
  • 或者是直接把对应jsp的 isErrorPage="true"

@RequestHeader

在方法中接收请求头当中的信息。

<a href="${pageContext.request.contextPath}/first">demo</a>
@Controller
public class MyController {
    @RequestMapping(value = "first")
    public String restful01(@RequestHeader("Host") String host){
        System.out.println(host);
        // localhost:8080
        return "/second.jsp";
    }
}

@CookieValue

用来接收浏览发送过来的cookies值

  • 普通的方式(获取所有cookie)
@Controller
public class MyController {
    @RequestMapping(value = "first")
    public String restful01(@RequestHeader("Cookie") String cookie){
        System.out.println(cookie);
        return "/second.jsp";
    }
}
  • 新的方式(获取本次访问的cookie)
<a href="${pageContext.request.contextPath}/first">demo</a>
@Controller
public class MyController {
    @RequestMapping(value = "first")
    public String restful01(@CookieValue("JSESSIONID") String sessionId){
        System.out.println(sessionId);
        return "/second.jsp";
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_33816821/article/details/87777831