2012-03-30 29 views
6

Tôi đang sử dụng Cấu hình dựa trên Java 3, bằng BootStrap.Mùa xuân 3, với cấu hình dựa trên Java và sự cố truy cập Tài nguyên

Tôi đã tải xuống tệp khởi động và đặt css và js dưới thư mục tài nguyên.

Sự cố mà tôi không thể sử dụng các tệp .cs này từ trong trang freemarker. Làm thế nào tôi nhập chúng. Như tôi đang sử dụng cấu hình java dựa, tôi đã thêm "addResourceHandler" như sau:

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 log nói:

"CẢNH BÁO: Không có ánh xạ tìm thấy cho Yêu cầu HTTP với URI

[/springway/resources/css/bootstrap-responsive.css] trong Dispatc herServlet với tên 'mùa xuân'

Directory:

-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> 

Trả lời

9

Bạn đã xác định resourceLocation sai .

Thay vì

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

bạn nên làm

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

Bởi vì thư mục css của bạn là nguồn lực bên trong thư mục mà bạn cần phải đặt thêm ** sau khi chỉ/sau đó nó sẽ xác định css thư mục nếu không nó sẽ tải chỉ từ các thư mục tài nguyên không có thư mục con sẽ được xem xét.

Hy vọng nó sẽ giúp bạn.

Chúc mừng.

+0

Cảm ơn sự giúp đỡ của bạn. – Echo

1

Nếu bạn đang sử dụng Xuân An, bạn có thể xem xét thêm các webjars vào danh sách các yêu cầu ủy quyền bằng cách thêm dòng mã này để SecurityConfiguration Lớp của bạn:

@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

Nếu bạn đang sử dụng Xuân An, có lẽ bạn xem xét thêm các webjars vào danh sách các yêu cầu được ủy quyền bằng cách thêm dòng mã này vào Lớp bảo mật cấu hình của bạn:

@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(); 
} 
Các vấn đề liên quan