2016-08-05 60 views
2

使用上下文路徑時,我在Spring Boot中遇到了靜態內容問題。即:我希望當我運行的應用程序沒有上下文路徑上的一切以我的應用程序部署到localhost:8080/{appname}/帶上下文路徑的Spring Boot靜態內容

正常工作和春季啓動查找和運行我的.html文件(從資源/模板/,我使用Thymeleaf)和JS文件(從資源/靜態/ JS /),但是當我添加上下文路徑之一:

server.context-path=/{appname} 

OR

server.servlet-path=/{appname} 

那麼.html頁面仍然被JS文件顯示生成404錯誤

我試圖更改application.properties和壓倒一切的addResourceHandlers()方法spring.resources.static-locationsMvcConfig.class但既不似乎工作

我使用MvcConfig類,因爲我需要定義一個CookieLocaleResolverMessageSource,但是這就是我有在MvcConfig。我不使用@EnableWebMvc,只需@SpringBootApplication註釋@ComponentScan

任何幫助,將不勝感激:)

+0

可以粘貼您選擇的配置和drectory結構? –

+0

是否該js文件的預期URL是404ing?或者HTML引用的JS文件是404ing? (例如,HTML引用的JS沒有應用上下文,或其他的問題) – rhinds

+0

@rhinds HTML引用JS沒有應用上下文 – svg

回答

1

按您的評論:

HTM L引用的是沒有應用上下文的JS

這個問題不在Spring中爲js服務,它的頁面沒有正確地創建到資源的URL。

Thymeleaf提供了一種自動支持該機制的機制,只需使用th前綴標記src屬性即可。

見第2節「上下文相關的URL」:www.thymeleaf.org/doc/articles/standardurlsyntax.html

+0

這解決了問題,thx!我之前實際上已經使用@ {/ js/jquery-2.2.3.min.js}進行了測試,但是我認爲還有其他事情也被破壞了,並且我用一個正常的

-1

我的配置是:

@Configuration 
public class MvcConfig extends WebMvcConfigurerAdapter { 

    public static final Logger LOG = LoggerFactory.getLogger(MvcConfig.class); 

    @Bean 
    public LocaleResolver localeResolver() { 

     CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver(); 
     cookieLocaleResolver.setCookieName("language"); 
     cookieLocaleResolver.setCookieMaxAge(-1); 
     cookieLocaleResolver.setDefaultLocale(Locale.FRENCH); 
     return cookieLocaleResolver; 
    } 

    @Bean 
    public MessageSource messageSource() { 
     ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); 
     messageSource.setBasename("classpath:i18n/lang"); 
     messageSource.setDefaultEncoding("UTF-8"); 
     return messageSource; 
    } 

    @Override 
    public void addInterceptors(InterceptorRegistry registry) { 
     registry.addInterceptor(new Urli18nRewriterInterceptor()); 
    } 
} 

和文件夾結構:

folder structure

+1

你應該添加你的配置作爲答案,你應該添加它對你的問題。 –

相關問題