【问题标题】:How to enable directory listing in Spring Boot with Thymeleaf or JSP如何使用 Thymeleaf 或 JSP 在 Spring Boot 中启用目录列表
【发布时间】:2017-12-06 06:19:17
【问题描述】:

我在我的 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 文件将按预期工作(尽管目录列表不起作用......)。

【问题讨论】:

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


    【解决方案1】:

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

    spring.thymeleaf.prefix=classpath:/test/
    

    对于 jsp,这也应该可以工作(没有测试过):

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

    【讨论】:

    • 您是否为此删除了servletRegistrationBean()?我现在不知道它将如何影响配置。 spring.thymeleaf.prefix 100% 有效。我将它用于我的应用程序。
    • 我只添加了属性。
    猜你喜欢
    • 2022-01-25
    • 2017-10-30
    • 2021-06-28
    • 2019-09-24
    • 2016-12-01
    • 2019-03-12
    • 2019-03-25
    • 2017-02-14
    相关资源
    最近更新 更多