Home >Java >javaTutorial >What is the process for java SpringBoot to access HTML?

What is the process for java SpringBoot to access HTML?

WBOY
WBOYforward
2023-05-12 18:16:131991browse

Introduction

SpringBoot's default page mapping path (that is, the location where template files are stored) is "classpath:/templates/*.html". The static file path is "classpath:/static/", which can store static files shared by JS, CSS and other templates

Default file path access

Store the HTML page in the resources/static directory Access

java SpringBoot访问HTML的流程是什么

Put the html file in the resources/static directory and access it directly through the ip port number file path

 文件放在resources/static/view目录下

java SpringBoot访问HTML的流程是什么

 文件放在resources/static目录下

java SpringBoot访问HTML的流程是什么

Customized file path access

The resources in the templates directory under the SpringBoot project are protected by default and do not have open access permissions. This is because the templates

folder is where template files are placed, so a view parser is needed to parse it. Therefore, it must be accessed through the inside of the server, which means going through the process of controller→ service→ view resolver. At the same time, there are security issues. For example, if you put your background

html files in templates, and this folder is open to the outside world, there will be security risks.

Method: Open access permissions in application.yml or application.properties configuration file

    ???? application.yml file configuration:
  • spring:
      resources:
        static-locations: classpath:/META-INF/resources/, classpath:/resources/, classpath:/static/, classpath:/public/, classpath:/templates/
  • or

##???? application.yml file configuration:
  • spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/templates/

    After the configuration is completed, start SpringBoot , you can directly access the static resources in the templates directory by entering the address in the browser.
Access through Controller layer jump

Introduce thymeleaf dependency

        <!-- thymeleaf依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

Define interface return page path

@Controller
public class testController {
    @RequestMapping("/test")
    public String test() {
        return "/login1";
    }
}

Access through interface

The above is the detailed content of What is the process for java SpringBoot to access HTML?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete