2012-03-30 68 views
6

我正在使用Spring 3,基於java的配置和BootStrap。Spring 3,基於Java的配置和資源訪問問題

我已經下載了bootstrap,並將css和js放在資源目錄下。

我不能在freemarker頁面中使用這些.css。 但是我輸入了它們。 由於我使用的是基於Java的配置,我已經加入了 「addResourceHandler」 如下:

WebAppConfig:

@Configuration 
@EnableWebMvc 
@ComponentScan("com.springway") 
public class WebConfig implements WebApplicationInitializer { 

    @Override 
    public void onStartup(final ServletContext servletContext) throws ServletException { 
     final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); 
     root.setServletContext(servletContext); 
     root.scan("com.springway"); 
     root.refresh(); 

     final ServletRegistration.Dynamic servlet = servletContext.addServlet("spring", new DispatcherServlet(root)); 
     servlet.setLoadOnStartup(1); 
     servlet.addMapping("/*"); 
    } 

    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); 
    } 

Tomcat的日誌說:

「警告:沒有映射發現帶URI的HTTP請求

[/springway/resources/css/bootstrap-responsive.css] in Dispatc herServlet名爲 '春天'

目錄:

-SpringWay 
>  -src 
>   - main 
>     -webapp 
>       -resources 
          -WEB-INF 
           -welcome.ftl 
           -springway.ftl  

welcome.ftl:

[#ftl /] 
[#include "springway.ftl" /] 


<ul class="breadcrumb"> 
    <li> 
    <a href="[@spring.url '/test'/]">Test</a> <span class="divider">/</span> 
    </li> 
    <li> 
    <a href="#">Library</a> <span class="divider">/</span> 
    </li> 
    <li class="active">Data</li> 
</ul> 

springway.ftl:

[#ftl/] 
    [#import "spring.ftl" as spring /] 

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 

     <title> 

     </title> 

     <link href="[@spring.url '/resources/css/bootstrap-responsive.css'/]" rel="stylesheet" type="text/css" media="screen"/> 
     <link href="[@spring.url '/resources/css/bootstrap-responsive.min.css'/]" rel="stylesheet" type="text/css" media="screen"/> 
     <link href="[@spring.url '/resources/css/bootstrap.css'/]" rel="stylesheet" type="text/css" media="screen"/> 
     <link href="[@spring.url '/resources/css/bootstrap.min.css'/]" rel="stylesheet" type="text/css" media="screen"/> 

     <script src="[@spring.url '/resources/js/bootstrap.js'/]" type="text/javascript"></script> 
     <script src="[@spring.url '/resources/js/bootstrap.min.js'/]" type="text/javascript"></script> 
     </head> 

    <body ></body> 
    </html> 

回答

9

您已經定義錯誤resourceLocation 。取而代之的

registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); 

你應該做

registry.addResourceHandler("/resources/**").addResourceLocations("/resources/**"); 

因爲你的css文件夾裏面的文件夾的資源,你需要把額外的**後/只後,它會識別css文件夾,否則它將僅從資源文件夾加載,不考慮子文件夾。

希望它對你有所幫助。

乾杯。

+0

感謝日本的協助。 – Echo 2012-05-07 18:12:26

1

如果你正在使用Spring Security的,你可能會考慮加入這行代碼到你的SecurityConfiguration類添加webjars到授權的請求列表:

@Configuration 
@EnableWebMvcSecurity 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http 
    .authorizeRequests() 
    .antMatchers("/resources/**", "/signup", "/about").permitAll() 
    **.antMatchers("/webjars/**").permitAll()** 
    .anyRequest().authenticated() 
    .and() 
    .formLogin().loginPage("/signin").permitAll() 
    .and() 
    .logout().permitAll(); 
} 
2

如果您在使用Spring Security,您可能考慮加入這一行的代碼添加到您SecurityConfiguration類添加webjars授權的請求列表:

@Configuration 
@EnableWebMvcSecurity 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 

@Override 
protected void configure(HttpSecurity http) throws Exception { 
http 
    .authorizeRequests() 
    .antMatchers("/resources/**", "/signup", "/about").permitAll() 
    **.antMatchers("/webjars/**").permitAll()** 
    .anyRequest().authenticated() 
    .and() 
.formLogin().loginPage("/signin").permitAll() 
    .and() 
//... 
.logout().permitAll(); 
}