springBoot-springmvc automatic configuration

1. Simple function analysis

1.1 Static resource access:

In the past, when we used ssm as a framework to develop web projects, we could put static resources under the webapp. When we access, we can directly access our static resources, but our springboot framework is a jar project and does not have a webapp Directory, so we explore where the static resources of springboot are placed

  • Static resources can be placed in:
  1. /static directory
  2. /public directory
  3. /resources directory
  4. /META-INF/resources
    As long as the static resources placed in these directories, we can directly use: the
    current project root path/+static resource name can be accessed

Principle:
The request for /** will be intercepted. When the request comes in, first check whether our Controller can handle it. If it can handle it, it will be processed directly. If it cannot be processed, then all requests will be handed over to the static resource processor. Can’t handle, then 404

Change the default static resource path

#修改静态资源的访问路径,此时的访问路径为:
#项目根路径/+static-path-pattern+/静态资源名访问
spring:
  mvc:
    static-path-pattern: /res/**

#这个也是spring下的,修改默认的静态资源目录,此时静态资源就
#不能放在上边默认的四个目录中了,只能放在我们修改后的haha目录下
  resources:
    static-locations: [classpath:/haha/]
  • Another way (webjars) is no longer used now

How to use webjars:
1. Go to: www.webjars.org to find the maven coordinates of the static resource we want to import
2. Import in the pom.xml file
3. Download the static resource to the project
4. Access: Project root path/ +webjars/+Dependent package path
This method is generally not used

1.2 Homepage default visit

Springboot has set up the homepage access method for us. Just put our homepage (name must end with index.html\index) into the directory of static resources. Springboot will automatically find index.html as our Home page, we only need to visit the project root path, the home page appears

  1. Set index, html

Just put index.html into the static resource directory.
You can set the default static resource directory, but you cannot configure the access path prefix of the static resource, because the underlying springboot has already written down the request path for receiving /** for us.

  1. Set index

Set up a mapping in a controller that can be mapped to index, which can be used as a processor to jump to the homepage

Custom Favicon

favicon.ico can be placed in the static resource directory. The default static resource directory can be set, but the access path prefix of static resources cannot be configured. The request sent by the front desk is also without prefix.

Form submission REST style path

We know that servlter supports GET\POST\DELETE\PUT format request methods by default, and our rest style exactly corresponds to these methods and performs different operations. However, by default, springboot only enables the form submission of get and post, and the delete and put requests are cancelled.

Solution:

  1. In the form we submitted, if you use get and post, it will be used normally. If you use delete and put requests, the method of the form must be post, and then create a hidden field in the form, the name is'_method', value As'delete\put'.
  2. Turn on the rest function of the springboot page submission form.
    You can use the rest-style delete and put methods.

Submit code at the front desk

<form action="/user" method="get">
    <input value="get提交" type="submit">
</form>
<form action="/user" method="post">
    <input value="post提交" type="submit">
</form>
<!--这里的method必须为post-->
<form action="/user" method="post">
	<!--添加一个隐藏域-->
    <input type="hidden" name="_method" value="delete">
    <input value="delete提交" type="submit">
</form>
<form action="/user" method="post">
    <input type="hidden" name="_method" value="put">
    <input value="put提交" type="submit">
</form>

Configuration file:

#开启页面表单的Rest功能
spring:
  mvc:
    hiddenmethod:
      filter:
        enabled: true

Rest principle (when the form is submitted using REST)

• submit the form = PUT will bring _method
• request over the HiddenHttpMethodFilter intercepted PUT.DELETE.PATCH
is our request form submitted with a man named '_method = xxx' is, springboot obtained
when taking this request, will be HiddenHttpMethodFilter intercept, access to _method the
parameters, and then use the packing mode, the post the way our original request, substitutions for my
way in the value of their _method, you can use the controller xxx in our map .
Note: The rest function of springboot must be turned on manually, otherwise it cannot be used, and the post request will be used by default (because our original request is a post request).

Rest using client tools

For example, PostMan sends Put, delete and other methods directly without Filter.

springboot receives parameters

1.1 Annotation

  • @PathVariable, suitable for rest style transfer parameters
    Insert picture description here

  • @RequestHeader, suitable for receiving request header information
    Insert picture description here

  • @RequestParam, suitable for receiving ordinary passed parameters
    Insert picture description here

  • @MatrixVariable, receive matrix variable

Matrix variable: Syntax: Request path: /cars/sell;low=34;brand=byd,audi,yd
; the parameters after the number are all parameters that are carried.
SpringBoot defaults to disable the function of
matrix variables . Matrix variables must have URL path variables. To be parsed, which must be used in conjunction with rest

Insert picture description here
Configure the matrix variable function of springboot:

@Configuration
public class MyConfig implements WebMvcConfigurer {
    
    

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
    
    
        //获取到UrlPathHelper
        UrlPathHelper urlPathHelper=new UrlPathHelper();
        //把:删除路径中;后面的值去除掉变为false
        //为true,就变为下面的:
        //例如:localhost:8080/car;id=1/sall  ->  localhost:8080/car/sall
        urlPathHelper.setRemoveSemicolonContent(false);
        configurer.setUrlPathHelper(urlPathHelper);
    }
}
  • @CookieValue, receive cookie parameters

Just like ssm, get the cookie, write it and you can use it

  • @RequestBody receives request body parameters

Generally used in post requests, the parameters of the post request will be in the request body. This method can be used to obtain the content of the request body.
Note: Submit the form form by post, the data in the request body is in the form of'key=value&key1=value...', using this annotation is to get the entire request body content.

1.2 servlet api:

  • WebRequest、
  • ServletRequest
  • MultipartRequest、
  • HttpSessionInputStream、
  • Reader

Some of the parameters above ServletRequestMethodArgumentResolver

1.3 Complex parameters:

  • Map、
  • Model (data in map and model will be placed in request.setAttribute of request),
  • Errors/BindingResult、
  • RedirectAttributes (redirection carries data),
  • ServletResponse(response)

1.4 Custom type:

As long as our parameters are consistent with the attributes of our custom type class, they will be automatically encapsulated into our custom class. We can also perform cascading encapsulation (the front-end passing parameters should also be cascaded. For example: pet under user Object pass value. pet.name is fine)

##So much for the time being, I will learn later and take notes

Guess you like

Origin blog.csdn.net/weixin_43431123/article/details/112852803