Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 3 ,with Java based configuration, and Resources access issue

I am using Spring 3 ,java based configuration, with BootStrap.

I have downloaded the bootstrap and put the css and js under resources directory.

The issue that I cann't use these .css from within the freemarker page. Howeve that I imported them. As I am using the java based configuration,I have added the "addResourceHandler" as follows:

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

"WARNING: No mapping found for HTTP request with URI

[/springway/resources/css/bootstrap-responsive.css] in DispatcherServlet with name 'spring'

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>
like image 754
Echo Avatar asked Mar 30 '12 08:03

Echo


3 Answers

You have defined wrong resourceLocation.

Instead of

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

you should have done

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

Because your css folder is inside resources folder you need to put the extra ** after the / only after that it will identify the css folder otherwise it will load only from the resources folders no subfolders will be considered.

Hope it helped you.

Cheers.

like image 61
Japan Trivedi Avatar answered Oct 22 '22 13:10

Japan Trivedi


If you are using Spring Security, you might consider adding the webjars to the list of authorized requests by adding this line of code to your SecurityConfiguration Class:

@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();
} 
like image 28
Jules0707 Avatar answered Oct 22 '22 14:10

Jules0707


If you are using Spring Security, you might consider adding the webjars to the list of authorized requests by adding this line of code to your SecurityConfiguration Class:

@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();
} 
like image 1
jules martel Avatar answered Oct 22 '22 12:10

jules martel