0

Ok, I developed a small spring boot website using thymleaf and now realized that I can't use the webapp folder if I want to package everything with the maven plugin.

To fix this I moved all my resources to src/main/resources. However, I keep getting FileNotFoundExceptions when I try to display any site (simple RequestMapping returning a String):

This is the error I get:

Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/index.html] at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:157) ~[spring-web-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT] at org.thymeleaf.spring5.templateresource.SpringResourceTemplateResource.reader(SpringResourceTemplateResource.java:103) ~[thymeleaf-spring5-3.0.3.M1.jar:3.0.3.M1] at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:223) ~[thymeleaf-3.0.3.RELEASE.jar:3.0.3.RELEASE] ... 75 common frames omitted 

And then I get the same error again when Spring tries to load my error page. Full http://pastebin.com/raw/Csw5akHJ

Explorer

(Yes I know that only the static folder is available. Good enough for testing.)

Can anyone help me? This is getting a bit frustrating.

3 Answers 3

1

If you are using Thymleaf as Template Engine you should add all .html files inside resources/templates

Sign up to request clarification or add additional context in comments.

2 Comments

In static directory you put ONLY static resources like css, js or images. You should not put there files I mean error.html. Copy all *.html files to resources/templates. Errors should be in error folder with 4xx.html name for 4xx errors handling.
By now I already have an index.html file pretty much everywhere. Nothing works. And btw, if I read the source code right all three folders should be handled right since they are all added by addResourceHandlers(). You can also use error.html if you want to have a default one. At least it worked when everything still was in the webapps folder.
0

i am not sure if this is your problem but normally i would put all the html pages inside templates directory under resources and all js and css files under static directory.

by doing so js and css files can easily accessed. for eg if i have css directory and test.css inside it. i can simply access it doing

so coming to your problem on my controller i will return pages like this.

 @RequestMapping(value="/viewusers",method = RequestMethod.GET) public String viewUsers(){ return "users/viewusers"; } 

in above sample i have viewusers.html under users directory. my users directory is inside templates directory.

3 Comments

Tried to copy my test index file to src/main/resources/templates but it didn't really change anything.
can you post your controller here?
@Controller public class HomeController { @RequestMapping(value="/") public String index(){ return "index"; }
0

OK, I made some headway. While it works fine if I use the default template Engine It stops working as soon as I start using the Thymeleaf one. Apparently the default template Engine can handle classpaths automatically while I needed to switch from SpringResourceTemplateResolver to ClassLoaderTemplateResolver if I want to use thymeleaf.

So far it looks like everything is working fine. Halleluja!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.