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

What is the process for java SpringBoot to access HTML?

WBOY
Release: 2023-05-12 18:16:13
forward
1993 people have browsed it

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目录下
Copy after login

java SpringBoot访问HTML的流程是什么

 文件放在resources/static目录下
Copy after login

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/
    Copy after login
  • or

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

    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>
Copy after login

Define interface return page path

@Controller
public class testController {
    @RequestMapping("/test")
    public String test() {
        return "/login1";
    }
}
Copy after login
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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template