In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is to share with you about how to use thymeleaf in Springboot. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.
The directory structure is as follows:
My expectation is that the path to access "/ query" should jump to hello.html
The hello.html code is as follows:
Title
Hello World!
Hey, hey, hey
The controller code is as follows:
@ RestController
Public class TAdminController {
/ * *
* Service object
, /
@ Resource
Private TAdminService tAdminService
@ RequestMapping ("/ query")
Public String query (Model model) {
Model.addAttribute ("msg", "la la")
Return "hello"
}
}
Error 1: the view parser jumps and prints it directly.
The access path is directly printed out, which is not as expected, so I added the following configuration to the application.yml file
Spring:
Thymeleaf:
Cache: false
Prefix: classpath:/static/
Suffix: .html
Encoding: UTF-8
But the effect is still the same. After careful examination, it is found that the annotations are misused.
Should be changed to @ Controller
Why should I just change the controller annotations?
@ RestController is a stereotype annotation that combines @ ResponseBody and @ Controller.
It means:
The @ RestController annotation is equivalent to the combination of @ ResponseBody + @ Controller.
1) if you only annotate Controller with @ RestController, the method in Controller cannot return the jsp page, the configured view parser InternalResourceViewResolver does not work, and the content returned is the content in Return.
For example, if you are supposed to go to the hello.html page, it displays login.
2) if you need to return to the specified page, you need to use @ Controller with the view parser InternalResourceViewResolver.
3) if you need to return json or xml or custom mediaType content to the page, you need to annotate the corresponding method with @ ResponseBody
@ ResponseBody:
Function:
This annotation is used to write the object returned by the Controller method to the body data area of the Response object after the appropriate HttpMessageConverter is converted to the specified format (such as json format).
Timing of use:
The returned data is not a page with html tags, but is used when data in some other format (such as json, xml, etc.)
When we do not need to talk about data encapsulation, but need to jump to the page, we will remove @ responseBody, and finally return to the name of the page to jump.
Error 2: 404 error
After revision, it is found that the error of 404 is still reported.
At this time, our thymeleaf template path is also set, and the suffix is also set. Why did we still report an error? go back to the controller and find out.
Prompt Cannot resolve MVC View 'hello', if the view hello cannot be found, then our thymeleaf configuration does not work. Go back to the pom file and find that the thymeleaf dependency does not seem to be correct. I checked thymeleaf directly when I created the project. His thymeleaf dependency is as follows:
Go to the maven warehouse to find Spring Boot Starter Thymeleaf dependencies
Org.springframework.boot
Spring-boot-starter-thymeleaf
2.4.0
You have to use Spring Boot Starter Thymeleaf. That simple thymeleaf cannot be used, which is similar to the difference between mybatis and mybatis-spring integration packages.
Click the Load Maven Changes in the upper right corner after replacing the new dependency
Note: there is no need to write the version number here. Spring Boot has a version arbitration center. Later, we do not need to write the version by default when we import dependencies. (dependencies not managed in dependencies naturally need to declare the version number.)
The above is how to use thymeleaf in Springboot. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.
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.