2012-03-30 72 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(); 
}