Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to access the HTML page in SpringBoot project

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how to access the HTML page in the SpringBoot project". Many people will encounter this dilemma in the operation of the actual case, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

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

1. Access to store HTML pages in the resources/static directory

Store the HTML page in the static directory under the resources (resource directory).

[example] create a test1.html page under the static directory, then create a view directory under the static directory, and create a test2.html page under the view directory to achieve access in the browser. The project structure is shown in the following figure:

(1) create a test1.html page under the static directory with the following code:

Test Page 1 Test Page 1

Hello, welcome to pan_junbiao 's blog.

Https://blog.csdn.net/pan_junbiao

Execution result:

(2) create a view directory under the static directory and a test2.html page under the view directory with the following code:

Test Page 2 Test Page 2

Hello, welcome to pan_junbiao 's blog.

Https://blog.csdn.net/pan_junbiao

Execution result:

2. Access to store HTML pages in the resources/templates directory

Store the HTML page in the templates directory under the resources (resource directory).

[example] create a test3.html page under the templates directory and access it in the browser.

Create a test3.html page under the templates directory with the following code:

Test Page 3 Test Page 3

Hello, welcome to pan_junbiao 's blog.

Https://blog.csdn.net/pan_junbiao

2.1 solution: SpringBoot cannot directly access static resources in the templates directory (not recommended)

The resources of the templates directory under the SpringBoot project are protected by default and do not have open access. This is because the templates folder places the template file, so you need a view parser to parse it. So it must be accessed from within the server, that is, through the process of the controller → service → view parser. At the same time, there are security problems, for example, if you put your background html files into templates, and this folder is open to the outside world, there will be security risks.

Solution: open access in application.yml or application.properties configuration files (not recommended)

Application.yml file configuration:

Spring: resources: static-locations: classpath:/META-INF/resources/, classpath:/resources/, classpath:/static/, classpath:/public/, classpath:/templates/

Application.properties file configuration:

Spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/templates/

After the configuration is complete, start SpringBoot and enter the address in the browser to directly access the static resources in the templates directory.

Execution result:

Mode 2. Resources accessed through the Controller controller layer are redirected (recommended)

Create a controller directory (controller layer) in the source code layer, and create an IndexController (home controller class) in the controller directory. The project structure is shown below:

(1) configuration of pom.xml files

Note: be sure to add thymeleaf dependencies.

Org.springframework.boot spring-boot-starter-thymeleaf

(2) the method of writing controller

Create an IndexController (home controller class) with the following code:

Package com.pjb.springboothtml.controller; import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping; / * Home Controller * @ author pan_junbiao * * / @ Controllerpublic class IndexController {@ RequestMapping ("/ test3") public String test3 () {return "test3";}}

Execution result:

This is the end of the content of "how to access the HTML page in the SpringBoot project". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report