Denis
Denis

Reputation: 3707

How to return some HTML file on specific URL - Spring Boot

I am new to Spring Boot. I try to return some page, when user give me specific URL.

I have two pages:

\src\main\resources\static\index.html
\src\main\resources\static\admin.html

Now I have the following pairs:

GET / - return index.html
GET /admin.html - return admin.html

I want the following pairs:

GET / - return index.html
GET /admin - return admin.html

I know, that I can create some Controller, then I can use annotation @RequestMapping("/admin") and return my admin page. But it requires so many actions. What about, if I will have more pages.

Upvotes: 0

Views: 5177

Answers (2)

daniel.eichten
daniel.eichten

Reputation: 2555

Besides creating a @Controller defining all methods with @RequestMappings there is another way which is a little more convenient and does not need a change if you add or remove html files.

Option 1 - if you don't mind that people see .html suffix

Leave your files in the static folder and add a WebMvcConfigurer to your project like this:

@Configuration
public class StaticWithoutHtmlMappingConfigurer extends WebMvcConfigurerAdapter implements WebMvcConfigurer {

    private static final String STATIC_FILE_PATH = "src/main/resources/static";

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {

        try {
            Files.walk(Paths.get(STATIC_FILE_PATH), new FileVisitOption[0])
                .filter(Files::isRegularFile)
                .map(f -> f.toString())
                .map(s -> s.substring(STATIC_FILE_PATH.length()))
                .map(s -> s.replaceAll("\\.html", ""))
                .forEach(p -> registry.addRedirectViewController(p, p + ".html"));

        } catch (IOException e) {
            e.printStackTrace();
        }

        // add the special case for "index.html" to "/" mapping
        registry.addRedirectViewController("/", "index.html");
    }

}

Option 2 – if you prefer to serve without html and parse through template engine

Move your html to the templates folder, enable e.g. thymeleaf templates and change the configuration to:

@Configuration
public class StaticWithoutHtmlMappingConfigurer extends WebMvcConfigurerAdapter implements WebMvcConfigurer {

    private static final String STATIC_FILE_PATH = "src/main/resources/static";

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {

        try {
            Files.walk(Paths.get(STATIC_FILE_PATH), new FileVisitOption[0])
                .filter(Files::isRegularFile)
                .map(f -> f.toString())
                .map(s -> s.substring(STATIC_FILE_PATH.length()))
                .map(s -> s.replaceAll("\\.html", ""))
                .forEach(p -> registry.addViewController(p).setViewName(p));

        } catch (IOException e) {
            e.printStackTrace();
        }

        // add the special case for "index.html" to "/" mapping
        registry.addViewController("/").setViewName("index");
    }

}

Upvotes: 2

euparkeria
euparkeria

Reputation: 26

Annotate a controller method with @RequestMapping("/admin") than return "admin" and put admin.html in the templates directory.

Upvotes: 1

Related Questions