2016-11-25 106 views
0

我的項目結構如下無法映射靜態資源正確

| SRC

| - 主

| --- web應用

| ----靜態

| ----- CSS

| --- HTML HTML

| ----- js

我想通過我的控制器返回一個HTML資源,直接在根目錄下的鏈接沒有問題,但對於其他鏈接,我面臨問題。

這裏是我的控制器

@Controller 
public class HtmlServer { 


    @RequestMapping({"/", "/index", "/index.html", "/index.jsp", "/home","/home.html","/home.jsp"}) 
    public ModelAndView index() { 
     return new ModelAndView("html/index.html"); 
    } 


    @RequestMapping(method = RequestMethod.GET, value = "/support/{id}/{accessToken}") 
    public ModelAndView start(@PathVariable Long id, @PathVariable String accessToken) { 
     return new ModelAndView("html/index.html"); 
    } 

} 

這裏是我的WebMvcConfigurerAdapter擴展類

@Component 
@ConfigurationProperties 
@Configuration 
@EnableWebMvc 
public class ApplicationConfig extends WebMvcConfigurerAdapter { 

@Bean 
    public InternalResourceViewResolver internalResourceViewResolver(){ 
     InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver(); 
     internalResourceViewResolver.setPrefix("static/"); 
     return internalResourceViewResolver; 
    } 

    @Override 
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { 
     configurer.enable(); 
    } 

    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     registry.addResourceHandler("/js/**").addResourceLocations("static/js/") 
       .setCachePeriod(0); 
     registry.addResourceHandler("/css/**").addResourceLocations("static/css/") 
       .setCachePeriod(0); 
     registry.addResourceHandler("/support/**").addResourceLocations("/static/") 
       .setCachePeriod(0); 

    } 

} 

當我打開/或/index.html控制器返回給定值,作爲回報,我正在服務的正確的資源。

但是,當我嘗試使用/支持/ 1/xsdda我映射到/support/1/static/html/index.html有人可以解釋內部並邏輯上指出我的錯誤。

謝謝!解析器的

+0

我暫時通過使用重定向解決了這個問題:/static/html/index.html但這不是我正在尋找的,我希望HTML頁面呈現給定的URL。我確定必須有一些可配置的類正在執行此操作。只是無法弄清楚哪一個。 – user1615664

回答

1

創建內部WebApp文件夾WEB-INF,把你的HTML文件夾裏面解析器的

  • 集前綴/ WEB-INF/HTML/
  • 集後綴爲.html
  • 通話通過返回 「指數」

Servlet 2.4規範說這對WEB-INF(第70頁):

應用程序層次結構中存在一個名爲 WEB-INF的特殊目錄。該目錄包含所有與 應用程序相關的東西,它們不在應用程序的文檔根目錄中。 WEB-INF節點不是 應用程序的公共文檔樹的一部分。包含在WEB-INF目錄中的文件可以通過容器直接傳送給客戶端 。然而, WEB-INF目錄的內容是可見的使用getResourcegetResourceAsStream方法呼籲ServletContext到servlet代碼,並且可以 使用RequestDispatcher呼叫被暴露。

+0

我知道這一點。但是這並不能解決我的問題,它仍然會被映射到/WEBINF/HTML/static/1/asfaf.html。 這不是我正在尋找的。 – user1615664

+0

我不知道爲什麼我不知道如何,但它的工作原理與我想要的完全一樣。請在您的評論中分享您提及的確切文件的鏈接。謝謝! – user1615664

+0

http://download.oracle.com/otn-pub/jcp/servlet-2.4-fr-spec-oth-JSpec/servlet-2_4-fr-spec.pdf?AuthParam=1480319492_f198679269b3123ce049c5f752ebe525 – Zildyan