github还有这个功能,给力
甚至具体到了哪个依赖有问题
甚至甚至给出了解决方法
这个pom.xml是在csdn复制的,所以,我决定在去看看那篇博客,然后留个言
----------------------------------------------------------------------------------------------------------------------
REST风格。恩。。。我也不太懂,某乎上搜了下,有人喜欢有人讨厌
百度百科是这么说的
表述性状态转移是一组架构约束条件和原则。满足这些约束条件和原则的应用程序或设计就是RESTful。
REST 定义了一组体系架构原则,您可以根据这些原则设计以系统资源为中心的 Web 服务,
包括使用不同语言编写的客户端如何通过 HTTP 处理和传输资源状态。
觉得想要描述自己的理解,但是觉得怎么描述都不太准确,所以,搁置....
可能最开始我们的url是这样的
增 丨 删 丨 改 丨 查
insertxxx丨deletexxx 丨updatexxx 丨selectxxx
REST风格的url是这样的
增 丨 删 丨 改 丨 查
xxx 丨 xxx 丨 xxx 丨 xxx
恩,那我们根据什么来区分请求的是哪个呢?
根据请求方式来区分要进行的操作是什么,恩。。。
我觉得这些东西都能用来区分请求,但是大神选择了post,get,put,delete,所以,理解了要做,不理解就先做着然后再理解。
恩。。具体看代码吧。。。
首先web.xml需要配置一个filter拦截器
<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>
发起请求的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>
<!-- 发起图书请求,使用rest风格的url地址
GET 查询
DELETE 删除
PUT 更新
POST 添加
-->
<a href="book/1"> 查询</a>
<form action="book/1" method="post">
<input type="submit" value="添加"/>
</form>
<form action="book/1" method="post">
<input name="_method" value="delete">
<input type="submit" value="删除"/>
</form>
<form action="book/1" method="post">
<input name="_method" value="put">
<input type="submit" value="更新"/>
</form>
</body>
</html>
因为正常来讲只能发起POST和GET请求,正巧了springMVC有对这个风格的支持所以如下:
delete 和put必须是post请求,并且参数中有_method参数
controller中的代码如下
package com.sds.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class BookController {
@RequestMapping(value = "/book/{id}",method = RequestMethod.GET)
public String getBook(@PathVariable("id")String id) {
System.out.println("查询到了"+id);
return "success";
}
@RequestMapping(value = "/book/{id}",method = RequestMethod.DELETE)
public String deleteBook(@PathVariable("id") String id) {
System.out.println("删除了"+id);
return "success";
}
@RequestMapping(value = "/book/{id}",method = RequestMethod.PUT)
public String updateBook(@PathVariable("id") String id) {
System.out.println("更新了"+id);
return "success";
}
@RequestMapping(value = "/book/{id}",method = RequestMethod.POST)
public String postBook(@PathVariable("id") String id) {
System.out.println("添加了了"+id);
return "success";
}
}
然后跳转success页面如果出现如下,这个问题会出现在tomcat8.*的版本中,其他版本没有测试,
HTTP Status 405 – 方法不允许啊
Type Status Report
消息 JSPs only permit GET POST or HEAD
描述 请求行中接收的方法由源服务器知道,但目标资源不支持
需要在头部添加isErrorPage="true"结果如下
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" isErrorPage="true"%>
spring-servlet.xml中我配置了这些:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<context:component-scan base-package="com.sds.controller"></context:component-scan>