Spring 3, with Java based configuration and resource access issue

I am using Spring 3, a Java based configuration, with BootStrap.

I downloaded the bootloader and put css and js in the resource directory.

The problem is that I cannot use these .css on the freemarker page. Be that as it may, I imported them. Since I am using java based configuration, I added "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/"); } 

The Tomcat log says:

"WARNING: No mapping found for HTTP request with URI

[/springway/resources/css/bootstrap-responsive.css] in a DispatcherServlet named '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> 
+6
source share
3 answers

You have identified an invalid resource.

Instead

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

you should have done

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

Since your css folder is in the resources folder, you need to add an extra ** after / after it determines the css folder, otherwise it will only be downloaded from resource folders, which will not include any subfolders.

Hope this helps you.

Greetings.

+9
source

If you use Spring Security, you might consider adding webjars to the list of authorized requests by adding this line of code to the 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(); } 
+2
source

If you use Spring Security, you might consider adding webjars to the list of authorized requests by adding this line of code to the 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(); } 
+1
source

Source: https://habr.com/ru/post/912034/


All Articles