SpringBoot-MVC-官方文档解读

SpringMVC

以下是对于springboot官方文档的解读---SpringMVC,后期会继续更新

Web (spring.io)

Spring Boot provides auto-configuration for Spring MVC that works well with most applications.

自动配置

The auto-configuration adds the following features on top of Spring’s defaults:

  • Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.---视图解析器

  • Support for serving static resources, including support for WebJars (covered later in this document).---访问静态资源

  • Automatic registration of Converter, GenericConverter, and Formatter beans.---格式化等

  • Static index.html support.-----欢迎页面

定制配置

If you want to keep those Spring Boot MVC customizations and make more MVC customizations (interceptors, formatters, view controllers, and other features), you can add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc.

在配置类使用@Configuration, 同时不要使用@EnableWebMvc

If you want to provide custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter, or ExceptionHandlerExceptionResolver, and still keep the Spring Boot MVC customizations, you can declare a bean of type WebMvcRegistrations and use it to provide custom instances of those components.

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc, or alternatively add your own @Configuration-annotated DelegatingWebMvcConfiguration as described in the Javadoc of @EnableWebMvc.

1 静态资源访问

1.1.1 静态资源目录:

By default, Spring Boot serves static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath or from the root of the ServletContext. It uses the ResourceHttpRequestHandler from Spring MVC so that you can modify that behavior by adding your own WebMvcConfigurer and overriding the addResourceHandlers method.

静态资源放在以下路径

  • resources/static

  • resources/public

  • resources/resources

  • resources/META-INF/resources


1.1.2 默认访问路径

By default, resources are mapped on /**, but you can tune that with the spring.mvc.static-path-pattern property. For instance, relocating all resources to /resources/** can be achieved as follows:

默认/**,可以直接在根目录下访问静态资源

为了后期方便拦截静态资源,可以修改静态资源访问路径

注意:如果修改了此项,会导致下面的index.html和ico文件的配置失效

spring:
  mvc:
    static-path-pattern: "/res/**"

1.1.3 静态资源路径

You can also customize the static resource locations by using the spring.web.resources.static-locations property (replacing the default values with a list of directory locations). The root servlet context path, "/", is automatically added as a location as well.

使用spring.web.resources.static-locations,配置静态资源存放位置

spring:
  resources:
    static-locations: [classpath:/staticRes]

1.1.4 访问外部jar包

In addition to the “standard” static resource locations mentioned earlier, a special case is made for Webjars content. Any resources with a path in /webjars/** are served from jar files if they are packaged in the Webjars format.

路径: /webjars/**

1.2 默认页面

Welcome Page

Spring Boot supports both static and templated welcome pages. It first looks for an index.html file in the configured static content locations. If one is not found, it then looks for an index template. If either is found, it is automatically used as the welcome page of the application.

在静态资源路径下创建index.html文件即可作为欢迎页面,即访问根目录就可以跳转到此页面

注意:如果配置了静态资源路径,会使此跳转欢迎页失效(springboot版本:2.3.4.RELEASE、2.6.14)

1.3 自定义图标

Custom Favicon

As with other static resources, Spring Boot checks for a favicon.ico in the configured static content locations. If such a file is present, it is automatically used as the favicon of the application.

页面图标,将文件命名为favicon.ico放置资源目录下即可,注意失效的情况,与1.2同理

1.4 错误处理

Error Handling

By default, Spring Boot provides an /error mapping that handles all errors in a sensible way, and it is registered as a “global” error page in the servlet container. For machine clients, it produces a JSON response with details of the error, the HTTP status, and the exception message. For browser clients, there is a “whitelabel” error view that renders the same data in HTML format (to customize it, add a View that resolves to error).

There are a number of server.error properties that can be set if you want to customize the default error handling behavior. See the “Server Properties” section of the Appendix.

To replace the default behavior completely, you can implement ErrorController and register a bean definition of that type or add a bean of type ErrorAttributes to use the existing mechanism but replace the contents.

默认错误处理机制:

  • 对于客户端,会使用JSON返回错误信息;

  • 对于浏览器,会跳转到whitelable错误视图

自定义:

  • 使用ErrorController或使用@ControllerAdvice自定义JSON数据

Custom Error Pages

If you want to display a custom HTML error page for a given status code, you can add a file to an directory. Error pages can either be static HTML (that is, added under any of the static resource directories) or be built by using templates. The name of the file should be the exact status code or a series mask./error

自定义错误视图:在resource/public或resource/templates下创建error目录,在此目录下创建4xx.html和5xx.html文件,在spring boot中会自动解析文件,当遇到错误时会跳转到相应页面,注意,若没有相应的视图文件,则会跳转到whitelable

1.5 原生组件注入

Scanning for Servlets, Filters, and listeners

When using an embedded container, automatic registration of classes annotated with , , and can be enabled by using .@WebServlet@WebFilter@WebListener@ServletComponentScan

创建Servlets, Filters, and listeners,使用@WebServlet@WebFilter@WebListener进行标记,并在主程序类中使用@ServletComponentScan

Registering Servlets, Filters, and Listeners as Spring Beans

If convention-based mapping is not flexible enough, you can use the , and classes for complete control.ServletRegistrationBeanFilterRegistrationBeanServletListenerRegistrationBean

创建Servlets, Filters, and listeners,创建一个配置类

@Configuration
    public class MyConfig {

        @Bean
        public ServletRegistrationBean myServlet(){
            return new ServletRegistrationBean(new MyServlet(),"/test");
        }
        
        @Bean
        public FilterRegistrationBean myFilter(){

            MyFilter myFilter = new MyFilter();
            //        return new FilterRegistrationBean(myFilter,myServlet());
            FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(myFilter);
            filterRegistrationBean.setUrlPatterns(Arrays.asList("/test","/css/*"));
            return filterRegistrationBean;
        }

        @Bean
        public ServletListenerRegistrationBean myListener(){
            MyListener myListener = new MyListener();
            return new ServletListenerRegistrationBean(myListener);
        }
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值