Cannot Find Html Pages With Spring Boot
Solution 1:
In my opinion it is better to put your *.HTML files in your resources folder with all the static CSS FONTS and so on like in the picture below. In webapp you can have a WEB-INF folder only.
I am using this kind of structure and I have no problems finding any classes css or htmls and I do not use extensions at the end in java ("static/pages/login.html" like yours). If I want to access html I go like "user/someHtml" if it is in folder user for ex.
I know this answer is not precisely the one you are looking for but it may help you in someway.
EDIT: Now for the :
@OverridepublicvoidaddViewControllers(ViewControllerRegistry registry) {
super.addViewControllers(registry);
registry.addViewController("/savePassword").setViewName("static/pages/savePassword.html");
registry.addViewController("/login").setViewName("static/pages/login.html");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
Lets say you have a Controller:
@Controller@RequestMapping("/myController")
publicclassMyController{
...
@RequestMapping(value = "/login")
publicStringlogin(Model model) {
// Your code ....return"login"; //<- this is your login.html if it is directly in resource/templates folder
}
@RequestMapping(value = "/savePassword")
publicStringsavePassword(Model model) {
// Your code ....return"savePassword"; //<- this is your savePassword.html if it is directly in resource/templates folder
}
.....
}
Now your addViewController show look like this :
@OverridepublicvoidaddViewControllers(ViewControllerRegistry registry) {
super.addViewControllers(registry);
registry.addViewController("/savePassword").setViewName("forward:/myController/savePassword");
registry.addViewController("/login").setViewName("forward:/myController/login");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
You see in your addViewController you are trying to access a CONTROLLER not a HTML you will access your HTML through the CONTROLLER you access from the method addViewControllers.
UPDATE:
If you put your CSS into your static folder in resources like :
You can simply access them like this :
<link th:href="@{/css/bootstrap-datetimepicker.min.css}" />
Hope this helps!
Solution 2:
Ok, now I totally got it. I try to be specific as possible.
My final structure
1. Static Ressources
Spring has some default configuration where you can put your static ressources without configure it. You need to create those folders in src/main/resources
- /META-INF/resources/
- resources (yes, an additional resources folder in src/main/resources
- static
- public
Have a look here in Spring Doku. For an example, check the 2nd comment by Bob Shields:
After creating a bunch of "findme.txt" files, each of which had different "source-relative" path string inside, I found that the following locations can be fetched with "http://host/findme.txt" are:
src/main/resources/public/findme.txt
src/main/resources/static/findme.txt
src/main/resources/resources/findme.txt - (yes, resources^2!)
src/main/resources/findme.txt - not found!!
I placed my folder "scripts" and "stylesheets" in the static folder. When I want to include files from these folders in html, the source looks like src="scripts/file.js"
because the files and folders in static
are found directly rooted to host/scripts/file.js
No ResourceHandlerRegistry was necessary for me!
2. View Controller
I wanted to root my login.html to /login, savepassword.html to /savepassword and my index.html to /. Here is what I did:
registry.addViewController("/savepassword").setViewName("savepassword.html");
registry.addViewController("/login").setViewName("login.html");
registry.addViewController("/").setViewName("loggedin/index.html");
I set *.html because otherwise I had a problem that spring didn't know to differ between /login and the login(.html). As you can see I placed the index.html in a different folder called "loggedin". This folder is right in my public-folder (found by Spring, see 1.). Why do I do this? Have a look at 3.
3. Spring Security
.authorizeRequests().antMatchers("/stylesheets/**", "/scripts/**","/login", "/user/**").permitAll()
...omitted further configuration...
.loginPage("/login").permitAll();
First, I want that a user always has to be logged in before he can visit any other sites. So all ressources for loginpage must be available.
This piece of code makes every file in stylesheets and scripts accessible. Remember stylesheets and scripts-folder are placed in static. I had to set "/login" because of the custom loginpage.
Honestly I thought that the .loginPage("/login").permitAll();
would grant access, but it doesn't. Without it Spring tries to access the "/login", but can't cause of no rights and then automatically redirects to login => ERR_TOO_MANDY_REDIRECTS.
"/user/**" is just for some of my services that every user can use.
As you can see, I don't give access to "loggedin"-folder, so that every visitor can enter files in "loggedin" only if he is loggedin ;)
4. Redirecting
I just marked my whole controller with @RequestController, which implements @ResponseBody. When I return a String like return "redirect:/savepassword";
, Spring will set this as ResponseBody and it will be interpreted as String/Json(?).
I had to mark the controller as @Controller and set @ResponseBody to every service that must return json or sth. My service that shall redirect was only marked with @RequestMapping and method=RequestMethod.GET. Finally, I return a String like return "redirect:/savepassword";
This will make Spring redirect to /savepassword, which actually is placed in src/main/resources/public/savepassword.html
.
Finally, it works and it was hard enough to find how spring serves the files. I hope it helps someoneelse :)
Post a Comment for "Cannot Find Html Pages With Spring Boot"