简体   繁体   中英

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.

Besides creating a @Controller defining all methods with @RequestMapping s 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 eg 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");
    }

}

使用@RequestMapping(“ / admin”)注释控制器方法,然后返回“ admin”并将admin.html放在模板目录中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM