静态资源存放位置
默认静态资源存放在以下路径下,可在地址栏直接请求获取到
/META-INF/resources/
/resources/
/static/
/public/
修改静态资源存放位置
在application.yaml中修改静态资源存放位置(application.properties也一样可以)。出现路径正确,资源访问不到的情况,clean一下。
默认静态资源请求路径
/**:过滤所有请求
修改静态资源请求路径
访问静态资源需要加上resource
动态资源请求和静态资源请求先后
编写一个controller
@RestController
public class HelloController {
@RequestMapping("/keyboard.jpeg")
public String hello(){
return "Hello World";
}
}
动态请求名与静态资源名相同(keyboard.jpeg),请求localhost:8080/keyboard.jpeg结果:找到的是动态资源请求,返回的是Hello Word。
从这可以看出,会先找Controller,不能处理的请求都交给静态资源处理器,都不能请求返回404
Rest请求
在webMVCAutoConfiguration中注册了OrderedHiddenHttpMethodFilter
可以看到matchIfMissing默认为false,所以在实现Rest请求时需要在配置文件中修改为true:
OrderedHiddenHttpMethodFilter继承于HiddenHttpMethodFilter,直接看到doFilterInternal方法中的if条件
if ("POST".equals(request.getMethod()) && request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) == null) {
String paramValue = request.getParameter(this.methodParam);
if (StringUtils.hasLength(paramValue)) {
String method = paramValue.toUpperCase(Locale.ENGLISH);
if (ALLOWED_METHODS.contains(method)) {
requestToUse = new HttpMethodRequestWrapper(request, method);
}
}
}
1、请求必须为post,且没有错误
2、满足条件后,request会获取到名为"_method"的value值
3、将获取到的值转换为大写
4、判断是否是PUT、DELETE 、PATCH
5、将request重新封装
简单实现
index.html页面
<body>
<form action="/hello" method="post">
<input type="submit" value="post请求"/>
</form>
<form action="/hello" method="post">
<input name="_method" type="hidden" value="delete"/>
<input type="submit" value="delete请求"/>
</form>
<form action="/hello" method="post">
<input name="_method" type="hidden" value="put">
<input type="submit" value="put请求"/>
</form>
</body>
Controller
@RestController
public class HelloController {
@RequestMapping(value = "/hello", method = RequestMethod.POST)
public String hello(){
return "post请求";
}
@RequestMapping(value = "/hello", method = RequestMethod.PUT)
public String hello1(){
return "put请求";
}
@RequestMapping(value = "/hello", method = RequestMethod.DELETE)
public String hello2(){
return "delete请求";
}
}