Config sub-folder in Thymelaf Spring Boot - spring-boot

How can I configure thymeleaf to recognize view/htmls under sub-folder?
I have templates/ folder right now and all my view HTML pages are found in this single folder.

By default spring-boot searches for all files and folders under resources. So feel free to create subfolder for example under templates/.
You just have to set the folder structure to your returning view or of your fragments in html.
Just an example of a project from me:
Controller:
#GetMapping(value = "/getAnyWellBox")
public String getAnyWellBox(Model model)
//any code
return "thirdparty/booking/map/well-box";
}

Related

Spring MVC Controller opens wrong .jsp file

my project structure looks like below:
The issue is when I return in HomeController .jsp file named "home" and run this project on Tomcat server I receive website but not from home.jsp, but index.jsp (1 screenshot).
My question is is there any file where starting .jsp file is defined as a launcher ? If not so why index.jsp always launches while Controller Class returns a different file ?
Solved
I have created a new project (Maven project + Web archetype) and works fine. I assume that a problem was with location of web folder. Now I have it inside src folder unlike in previous structure.

Thymleaf picks mainLayout.html instead of home.html when string "home" is returned from controller

I have a spring boot project where ‘/’ will be redirected to ‘/home’ in HomeController bcz of the setting in WebConfig class and from there the return string is “home” so the thymeleaf template engine will look for home.html inside the templates folder but instead of it is looking for mainLayout.html which is inside the layout folder which is inside the template folder.
I have seen some blogs on how to use thymleaf with spring but didn't get the solution.
This is the link for complete project:
https://github.com/sivaprasadreddy/jcart
Go to jcart-site then look for WebConfig class then look for HomeController class and then look for templates folder there check mainLayout.html
Do this in your index.html file
<script>
window.location="/home";
</script>
it will redirect to your homecontroller.

Spring: How can I keep routes in sync with URLs on the page?

How can I keep links in my UI templates (e.g. Thymeleaf templates) in sync with the corresponding request mappings in my Spring application?
I've seen that e.g. the Play framework uses the #router-Object within its templates. How is it solved by Spring?
One example:
Spring Controller - simple
#Controller
public class UserController {
#GetMapping("/users/{username}")
public String getUser(#PathParam String username) {
// do some stuff....
return "user";
}
}
HTML-Page
<body>
User details
</body>
Now I want to change "/users" to "/accounts". I'm pretty sure that I've got to update every html page by hand to update the link. Is there an easier solution for this?
As far as I know, there is no simple way to do this with built-in tools from Spring. However, I don't think that this would be too hard to build. You would need the following:
A YAML file with all of your URL templates defined
A Properties Spring bean that contains your URL mappings (read from the YAML file)
All of your #RequestMapping annotations would have to be prop values; i.e.
#GetMapping("${urls.users.byUsername}")
A custom tag that knows about the Properties bean and can create URLs from the templates that were defined in your YAML file.

Thymeleaf: can't load js and css when direct access the page

Updated: to describe the question more clearly
I create a web applicaiton with spring boot and thymeleaf, everything works fine if I open the login page, then login, then head for the management module or reports module sequently.
The proleam occurs when I type the url locahost:8080/send/kf/index(needs to authenticate, but I have open access to all in customized filter) in the browser, the page loads without js and css. In debug mode, I saw /send/kf was unexpectly put into the path like the following. I can get the resource if I access localhost:8080/assets/avatars/avatar.png.
The following is the folder structure I put my static resources. How could /send/kf automatically added into the url, please help me solve this problem. Thanks!
you can use spring.resources.static-locations in your application.properties file
spring.resources.static-locations=classpath:/resources/css/
spring.mvc.static-path-pattern=/resources/**
this is taken form documentation
Note:Static resources, like JavaScript or CSS, can easily be served from your Spring Boot application just be dropping them into the right place in the source code. By default Spring Boot serves static content from resources in the classpath at "/static" (or "/public")
Using /assets/ instead of assets/ fixes the issue, as otherwise it's a relative url that's supposed to get added to the end of existing url.
I find a solution for this question. I don't know how /send/kf is put into the url for the static resources, but I think if I put the controller mapping under the root path, this problem should avoid. As I think, the url is correct and resources can load successfully.
#Controller
public class ManualMessageController {
#Autowired
private MsgTemplateRepository msgTemplateRepository;
#RequestMapping("/manualMsg")
public String manualMsg(Model model){
model.addAttribute("msgTemplateList", msgTemplateRepository.findByStatus(1));
return "manualMessage";
}
}
updated:
The right solution is to use absolute path rather than relative path in the project. So change assets/css to /assets/css works.

Configure index.html location in Spring initializr project

I created a Spring boot project using Spring initializr.
The project structure is below. /resources/client is the folder I added manually.
After I ran DemoApplication, I hit localhost:8080 and I saw the home page was pointed to /resources/index.html. I wanted to set the home page to be /resources/client/build/index.html, so I added something in application.properties:
spring.mvc.view.prefix=/resources/client/build/ ### also tried /client/build/
spring.mvc.view.suffix=.html.
However, it did not work and the home page was still pointed to /resources/index.html.
Also, The application is using dispatcherServlet but I did not find a dispatcherServlet file.
Is there any way I can use custom index.html location? Thanks.
Maybe you can try to add a HomeController using java code as following:
#Controller
public class HomeController {
#RequestMapping("/")
public String index() {
return "client/build/index.html";
}
}

Resources