0

So I have been trying hard to get a simple app to run with populating an HTML file through a controller without using a template engine.

I thought I would take some time to post about my findings related to the subject above. See answer below

Soo the question here is How to access HTML files through a controller in Spring boot without a template engine?

5
  • /static is for static files and not related to Thymeleaf. Also you don't need a controller but you are better of registering a view controller through a WebMvcConfigurer. Also for index this isn't needed as that is already resolved by Spring Boot by default (if you have an up to date version). Commented Nov 17, 2021 at 17:17
  • Agreed, It is already handled through Spring boot BUT it is not by default accessible through a controller. If I explicitly type in the html file name in my url header it will of course populate but the goal is to produce the file when "/" was being requested. Commented Nov 17, 2021 at 17:22
  • For / this is already enabled by default (see stackoverflow.com/a/70000437/2696260) so you shouldn't even need it (unless you disabled auto-configuration), Commented Nov 17, 2021 at 17:24
  • Agreed. It works by default but only works if you are using index.html....if you wanted to use a controller to route to another static file.....perhaps extra.html you will need a controller to route to the specific file in the static folder Commented Nov 17, 2021 at 17:31
  • No you don't need a controller you can simply use a the addViewControllers method of the WebMvcConfigurer to add those. No need to write a full fledge controller for that. Next to that your answer/question states that you need this for / to resolve an index, which isn't true as that works by default. Commented Nov 18, 2021 at 6:15

1 Answer 1

0

This is a very simplistic answer to a simple issue, this is not meant to scale and does not take into account the entirety of your project

Please keep in mind this is not the norm in a business environment but it was really bothering me how I cannot get a simple HTML to populate from a controller without using a template engine. I got it to work with thymeleaf dependency but I wanted to just get it to work as bare bones as possible. I read a lot of posts with ViewResolver Config classes or things a like but nothing worked. It would conflict with the internal resolvers of Spring boot.

Simple answer is once spring boot is initialized...all your static files must be in resources/static/

Templates folder is specific to use for Thymeleaf, based on my findings and it wont work if you just put it in the resource folders.

With your files in resources/static/ you can access it by going to localhost:8080/yourfilename.html

But if you want to access it through a controller you create a controller with the following:

@Controller
public class IndexController {

    @RequestMapping("/")
    public String getIndex(){
        return " index.html";
    }
}

If you want to remove the .html in your return value then you must add the following to your application.properties file

spring.mvc.view.suffix=.html

Then you can use the following

@Controller
public class IndexController {

    @RequestMapping("/")
    public String getIndex(){
        return " index";
    }
}

Once again this is a simple research I did on my own just to get it to work.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.