web开发

2.简单资源访问

2.1静态资源访问

  1.静态资源目录

  只要将静态资源放在类路径下,/static,/public,/resources/META-INF/resources

  访问:当前项目根路径/+静态资源名   形如:localhost:8080/2.jpg

  原理:静态映射/**

  请求进来,先去找Controller看能不能处理。不能处理的所有请求又都交给静态资源处理器。静态资源也找不到,返回404.

  2.静态资源访问前缀
  默认无前缀

  spring:
   mvc:
   static-path-pattern: /res/**
  当前项目+static-path-pattern+静态资源名=静态资源文件夹下找
3.WebJar
自动映射
  www.webjars.org
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.5.1</version>
</dependency>
访问地址:localhost:8080/webjars/jquery/3.5.1/jquery.js 后面的地址要按照依赖里面的包路径

2.2欢迎页支持
静态资源路径下的index.html
  可以配置静态资源路径
  但是不可以配置静态资源访问的前缀,否则会导致index.html不能被默认访问
spring:
# mvc:
# static-path-pattern: /res/** 会导致欢迎页失效,会导致favicon失效

resources:
static-locations: [classpath:/haha/]
controller能处理/index
2.3自定义favicon
将图片重命名为favicon.icon放在静态资源目录下
2.4静态资源配置原理
springboot启动默认加载xxxAutoConfiguration类(自动配置类)
springMVC功能的自动配置类WebMvcAutoConfiguration

3.请求参数处理

Rest原理(表单提交要使用REST的时候)

  • 表单提交会带上_method=PUT
  • 请求过来被HiddenHttpMethodFilter拦截
    • 请求是否正常,并且是POST
    • 获取到_method的值。
    • 兼容以下请求;PUT.DELETE.PATCH
    • 原生request(post),包装模式requesWrapper重写了getMethod方法,返回的是传入的值。
    • 过滤器链放行的时候用wrapper。以后的方法调用getMethod是调用requesWrapper的。

 

Rest使用客户端工具,

  • 如PostMan直接发送Put、delete等方式请求,无需Filter。

 

开启页面表单的rest

spring:
mvc:
hiddenmethod:
filter:
enabled: true

自定义过滤器
@Configuration(proxyBeanMethods = false)
public class WebConfig {

@Bean
public HiddenHttpMethodFilter hiddenHttpMethodFilter(){
HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
hiddenHttpMethodFilter.setMethodParam("cg");
return hiddenHttpMethodFilter;
}
}


protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpServletRequest processedRequest = request;
HandlerExecutionChain mappedHandler = null;
boolean multipartRequestParsed = false;
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);

try {
try {
ModelAndView mv = null;
Object dispatchException = null;

try {
processedRequest = this.checkMultipart(request);
multipartRequestParsed = processedRequest != request;
          //找到当前请求使用哪个Handler(Controller的方法)处理
mappedHandler = this.getHandler(processedRequest);
          //handlerMappings 处理器映射






 
 
posted @   自信且66  阅读(31)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
点击右上角即可分享
微信分享提示