I am using Spring 4, the annotation configuration. I tried different combinations of adding css and js to the jsp page, but none of them worked. Project structure:

enter image description here

Configuration:

one

@Configuration public class NewConfigure extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(final ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); } } 

2

 public class SpringMvcInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class[] { AppConfig.class }; } @Override protected Class<?>[] getServletConfigClasses() { return new Class[]{NewConfigure.class}; } @Override protected String[] getServletMappings() { return new String[] { "/" }; } } 

3

 @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired @Qualifier("userDetailsService") UserDetailsService userDetailsService; @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); } @Override public void configure(WebSecurity web) throws Exception { web.ignoring() .antMatchers("**/style/**"); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')") .antMatchers("/user/**").access("hasRole('ROLE_USER')") .antMatchers("/basket/**").access("hasRole('ROLE_USER')") .antMatchers("/resources/**", "/**").permitAll() .and().formLogin() .loginPage("/login").defaultSuccessUrl("/index").failureUrl("/login?error") .usernameParameter("username") .passwordParameter("password") .and().logout().logoutSuccessUrl("/login?logout") .and().exceptionHandling().accessDeniedPage("/403"); http.csrf().disable(); } 

AppConfig // beans for work:

 @EnableWebMvc @Configuration @ComponentScan({"com.vdp.*"}) @EnableTransactionManagement @Import({ SecurityConfig.class }) public class AppConfig { 

Last connection method:

 <link href="/resources/style/style.css" rel="stylesheet" type="text/css"/ > 

    2 answers 2

    When you write such a thing:

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

    This implies that you have the /resources directory in the /webapp directory, because Spring uses the ServletContext.getResource() method to get the path to this resource.

    It is not necessary to make it clear that you can get it back. For example, it is not necessary to accept the ClassPathXmlApplicationContext instance:

    Resource template = ctx.getResource ("some / resource / path / myTemplate.txt");

    What would be returned would be a ClassPathResource; If the same method was executed against a FileSystemXmlApplicationContext instance, you'd get back a FileSystemResource. For a WebApplicationContext, you'd get back a ServletContextResource, and so on.

    Documentation

    Having mixed up, you put everything in the standard Maven /resources directory, and thus sent the statics to the application classpath. Open the generated WAR file and see where these files appear to be - /WEB-INF/classes .

    Here is an example of how access to static resources can be configured:

     spring-mvc-java-config ยป tree . โ”œโ”€โ”€ pom.xml โ”œโ”€โ”€ src โ”‚  โ””โ”€โ”€ main โ”‚  โ”œโ”€โ”€ java โ”‚  โ””โ”€โ”€ webapp โ”‚  โ””โ”€โ”€ WEB-INF โ”‚  โ”œโ”€โ”€ fonts โ”‚  โ”œโ”€โ”€ html โ”‚  โ”œโ”€โ”€ i18n โ”‚  โ””โ”€โ”€ images โ”‚  โ””โ”€โ”€ scripts โ”‚  โ””โ”€โ”€ styles โ”‚  โ””โ”€โ”€ images 

    WebMvcConfigurerAdapter

     public class WebConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/scripts/**").addResourceLocations("/WEB-INF/scripts/"); registry.addResourceHandler("/styles/**").addResourceLocations("/WEB-INF/styles/"); registry.addResourceHandler("/images/**").addResourceLocations("/WEB-INF/images/"); registry.addResourceHandler("/fonts/**").addResourceLocations("/WEB-INF/fonts/"); } } 

    WebSecurityConfigurerAdapter

     public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override public void configure(WebSecurity web) throws Exception { web.ignoring() .antMatchers("/scripts/**") .antMatchers("/styles/**") .antMatchers("/images/**") .antMatchers("/fonts/**"); } } 

    And do not forget to add the application's context path when you register the URL of a static resource in JSP, Thymeleaf and so on.

    • I did not confuse, just tried different options. Thank you very much for the help, everything works. - Ostin

    It is not necessary to delete the resources folder and move all your static resources under WEB-INF (as indicated in the answer above). Just follow these steps:

    1) Resources folder must be under webapp.

    2) This method must be registered in your configuration:

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

    3) Personally, I prescribe the path in my JSP pages using JSTL:

     <link rel="stylesheet" href="<c:url value="/resources/css/test.css" />" /> <script src="<c:url value="/resources/js/test.js" />"></script> 

    To do this, import the JSTL Core library. Add the following to JSP:

     <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 

    Also, instead of JSTL Core, you can use $ {pageContext.request.contextPath}

    As a result, my CSS and JS resources are picked up with a bang :)