SpringBoot静态资源

静态资源存放位置

默认静态资源存放在以下路径下,可在地址栏直接请求获取到

/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请求";
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值