2013-10-11 41 views
4

1.

我正在使用Spring Boot。我的主類很簡單Spring引導中的@EnableWebMVC META-INF /資源無法正常工作

@ComponentScan 
@EnableAutoConfiguration 
@Configuration 
public class Application { 

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

#2。現在我想將我的靜態內容外化爲一個jar文件。所以,下面是JAR項目

/pom.xml 
/src/main/resources/META-INF/resources/hello.json // here is my resource 

我做maven install,把依賴到主應用程序,正常運行的應用程序。現在我可以調用http://localhost:8080/hello.json來獲取我的hello.json文件

#3。然後,下一步是使用Apache瓷磚我主要的web項目,所以我創建了一個@EnableWebMvc類配置tilesViewResolver

@Configuration 
@EnableWebMvc 
public class WebMvcConfiguration extends WebMvcConfigurerAdapter { 
    public @Bean TilesViewResolver tilesViewResolver() { 
     return new TilesViewResolver(); 
    } 

    public @Bean TilesConfigurer tilesConfigurer() { 
     TilesConfigurer ret = new TilesConfigurer(); 
     ret.setDefinitions(new String[] { "classpath:tiles.xml" }); 
     return ret; 
    } 
} 

然後,我又開始應用,並嘗試hello.json,以確保一切仍在正常工作。但是,404頁面出現。刪除WebMvcConfiguration退回我的hello.json

我應該怎麼做才能解決這個問題?

非常感謝。

回答

3

在Spring MVC,使用XML配置,你必須有一個像標籤下面來服務靜態內容:

<mvc:resources mapping="/js/**" location="/js/"/> 

這影射春天開機做東西自動猜測你有靜內容並在META-INF /資源中正確設置上述示例。這不是真的「神奇」,而是他們有一個使用@EnableWebMvc的默認Java配置,它有一些非常可靠的默認值。

當你提供你自己的@EnableWebMvc時,我猜測你是在寫他們的「默認」。爲了添加回資源處理程序,你會做這樣的事情:

@Override 
public void addResourceHandlers(ResourceHandlerRegistry registry) { 
    registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(31556926); 
} 

這等同於上面的XML。

+1

正確的,我想。只需刪除'@ EnableWebMvc'即可重新啓動引導功能。 旁白:JSP支持在一分鐘相當差,所以瓷磚可能是一個非首發。考慮使用Thymeleaf,或等待JSP趕上。 –

+0

我有同樣的問題。從看代碼,默認位置是 「類路徑:/ META-INF /資源/」, 「類路徑:/資源/」, 「類路徑:/靜態/」, 「類路徑:/公/」。我在src/main/resources/static/image.jpg中放置了一個圖像,構建並運行了我的項目,並能夠在localhost上訪問該圖像:8080/image.jpg – Jay

相關問題