• 四、Web开发


    1、简单功能分析

    1)静态资源

    静态资源目录:/static 或 /resources 或 /META-INF/resources 或 /public

    访问:当前项目根目录/ + 静态资源名

    原理: 静态映射/**

    请求进来,先去找Controller看能不能处理,不能处理的请求又都交给静态资源处理器,静态资源也找不到则响应404页面

    #修改网页访问静态资源文件的路径
    spring:
      mvc:
        static-path-pattern: /res/**
      #更改默认的静态资源文件夹位置
      web:
        resources:
          static-locations: [classpath:/haha/]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2)欢迎页支持

    静态资源路径下 index.html–可以配置静态资源路径,但是不可以配置静态资源的访问前缀,否则导致 index.html不能被默认访问

    controller能处理/index

    3)自定义 Favicon(图标)

    静态资源路径下放置favicon.ico图标、重启项目

    4)静态资源配置原理

    SpringBoot启动默认加载 xxxAutoConfiguration 类(自动配置类)

    SpringMVC功能的自动配置类 WebMvcAutoConfiguration生效

    
    @Configuration(
        proxyBeanMethods = false
    )
    @ConditionalOnWebApplication(
        type = Type.SERVLET
    )
    @ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
    @ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
    @AutoConfigureOrder(-2147483638)
    @AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class})
    public class WebMvcAutoConfiguration {}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2、请求参数处理

    @xxxMapping

    Rest风格支持(使用HTTP请求方式动词来表示对资源的操作)
    以前:/getUser 获取用户 /deleteUser 删除用户 /editUser 修改用户 /saveUser 保存用户
    现在: /user GET-获取用户 DELETE-删除用户 PUT-修改用户 POST-保存用户

    核心Filter;HiddenHttpMethodFilter

    用法: 表单method=post,隐藏域 _method=put

    SpringBoot中手动开启

        @RequestMapping(value = "/user",method = RequestMethod.GET)
        public String getUser(){
            return "GET-叶子航";
        }
     
        @RequestMapping(value = "/user",method = RequestMethod.POST)
        public String saveUser(){
            return "POST-叶子航";
        }
     
     
        @RequestMapping(value = "/user",method = RequestMethod.PUT)
        public String putUser(){
            return "PUT-叶子航";
        }
     
        @RequestMapping(value = "/user",method = RequestMethod.DELETE)
        public String deleteUser(){
            return "DELETE-叶子航";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    rest默认设置为flase

    	@Bean
    	@ConditionalOnMissingBean(HiddenHttpMethodFilter.class)
    	@ConditionalOnProperty(prefix = "spring.mvc.hiddenmethod.filter", name = "enabled", matchIfMissing = false)
    	public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {
    		return new OrderedHiddenHttpMethodFilter();
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    spring:
      mvc:
        hiddenmethod:
          filter:
            enabled: true   #开启页面表单的Rest功能
    
    • 1
    • 2
    • 3
    • 4
    • 5

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

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

    自定义MethodParam(_method)

    //自定义filter
        @Bean
        public HiddenHttpMethodFilter hiddenHttpMethodFilter(){
            HiddenHttpMethodFilter methodFilter = new HiddenHttpMethodFilter();
            methodFilter.setMethodParam("_m");
            return methodFilter;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    3、分析源码

    咕咕咕~Web开发这一部分源码太多了,我先入门,等我回来补源码

  • 相关阅读:
    单例模式之懒汉式和饿汉式
    数据库的基本操作(7)
    TCP和UDP有啥区别?
    DEPRECATION: The default format will switch to columns in the future
    今天睡眠质量记录63分
    QT 工具栏设置
    EasyNLP 中文文图生成模型带你秒变艺术家
    ChatGPT 案例实战趋势分析面积图制作
    c++基础:指针
    【软件教程】如何用C++交叉编译出能在Android运行的ELF程序或so动态库
  • 原文地址:https://blog.csdn.net/qq_52297656/article/details/126006657