如何使用Thymeleaf或JSP在Spring Boot中启用目录列表

时间:2017-07-03 04:25:24

标签: jsp tomcat spring-boot thymeleaf embedded-tomcat-8

我在Spring Boot应用程序中创建了以下主类:

import org.apache.catalina.servlets.DefaultServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        final DefaultServlet servlet = new DefaultServlet();
        final ServletRegistrationBean bean = new ServletRegistrationBean(servlet, "/test/*");
        bean.addInitParameter("listings", "true");
        bean.setLoadOnStartup(1);
        return bean;
    }
}

这是为了在http://localhost:8080/test/下启用目录列表。

如果我访问http://localhost:8080/test/,应用程序会显示test目录中的所有文件。这是我的预期结果。但是,目录中的JSP文件不起作用(目录中的Thymeleaf HTML文件也是如此)。 如何在Thymeleaf或JSP的Spring Boot中启用目录列表?

仅供参考,如果删除servletRegistrationBean()方法,JSP文件和Thymeleaf HTML文件将按预期工作(尽管目录列表不起作用......)。

1 个答案:

答案 0 :(得分:0)

您可以通过添加以下行在application.properties文件中设置Thymeleaf模板处理的路径:

spring.thymeleaf.prefix=classpath:/test/

对于jsp,这也应该有用(没有测试过):

spring.mvc.view.prefix=classpath:/test/