In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to get the root directory of a project in SpringBoot+Freemarker. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Get the project root directory in Freemarker
A lot of people say:
Add to the configuration file
Spring.freemarker.request-context-attribute=request
You can do it on the page
But I found that the front desk is still null (maybe there is something wrong with my configuration)
It was later found that it could be used in this way.
No extra configuration is required
Several ways to set the path of Freemark template engine in SpringMVC we want to return to the view of how it is done
(1) when we want to parse the jsp view in SpringMVC, we need to have the following configuration in spring.xml
Springboot does not recommend us to use jsp templates in SpringBoot
Provides us with a freemark template engine to parse the view
1. In SpringBoot, if we don't have any configuration, then we just need to create a templates folder under the resources directory for example.
(1)。 Create a Controller class to return a view named first
(2)。 Create a file named first.ftl in the resource directory (SpringBoot parses files of type ftl by default)
(3) content in first.ftl
(4) request result
From the above results, we can see that freemark completed the view parsing for us.
So how do we customize the template engine path for freemark?
There are two ways.
The first way:
1. Create an application.properties file in the resource directory, and SpringBoot will find the file in the resource directory when starting the load
Application.properties file, the code is as follows
(1) the two dots underlined here need to be noted. We should be very familiar with suffix because we used it before when configuring the jsp view parser, which is the file type for view parsing (the default parsing file type in springboot is ftl, but we changed the file type to fid format).
(2) it is not difficult to understand that template-loader-parh is to configure the path of parsing view.
(3) Directory structure
Let's take a look at the effect after the 2.application.properties configuration is completed.
This is how to modify the freemark return path through application.properties configuration.
The second way:
1. Create a Java class to inherit WebMvcConfigurerAdapter
The code is as follows
Directory structure
Running result
Above is how to configure freemark return path in two ways
Ps: in the process of repeatedly testing the return path of freemark, you have found a few interesting points that you can take a closer look at.
(1) if springboot does not configure application.properties files or create work classes, we all know that ftl format files are returned by default, but if we change the freemark path through tool classes, we will find that although we have configured files that return fap types, after we change fap to ftl files, freemark can still parse
(2) if the return path of freemark is changed through application.properties file, only the file type set by application.properties can be parsed, and ftl is invalid.
(3) if the application.properties file and the tool class are loaded at the same time, the weight of the tool class is relatively high, and freemark will return the configuration in the tool class. But in the first point, I said that freemark can return a file in ftl format if only the tool class is used, but if both the application.properties file and the tool class are started, ftl will be invalid.
Thank you for reading! This is the end of the article on "how to get the project root directory in SpringBoot+Freemarker". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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: 285
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.