2017-07-03 45 views
1

我創建了以下主類在我的春節,啓動應用程序:如何啓用目錄列表在春季啓動與Thymeleaf或JSP

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文件按預期工作(儘管目錄列表不起作用...)。

回答

0

您可以通過加入這一行設定在application.properties文件Thymeleaf模板處理路徑:

spring.thymeleaf.prefix=classpath:/test/ 

對JSP這也應該工作(沒有測試過):

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

我試過但沒有變化... –

+0

你是否爲此移除了'servletRegistrationBean()'?我現在不會如何影響配置。 'spring.thymeleaf.prefix'運作100%。我用這個爲我的應用程序。 – benkuly

+0

我只添加了屬性。 –