CSS and JS are not being applied to the project in Spring MVC (config via java)

0

I'm a beginner with SpringMVC and I'm learning through Spring MVC (Master the main Java web framework, by Alberto Souza, home code).

Before starting with Spring, I created a Maven project and implemented the frontend and the persistence part with jpa + hibernate. So far so good, I imported the project to the STS (Spring Tool Suite) and started to perform the settings explained in the book. But when I finished the AppWebConfiguration configuration (below code), my CSS and JS were not being applied anymore.

I already researched google, in the book, here in stackoverflow; until I implemented some possible solutions and the error persists. The project goes up, works normally, but without css and without javascript. Below is what I've implemented as attempts to fix the problem.

Below the implemented codes

Class code with the AppWebConfiguration method

@EnableWebMvc
@ComponentScan("com.projetoAnuncio")
public class AppWebConfiguration{

    @Bean
    public InternalResourceViewResolver internalResourceViewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/view/");
        resolver.setSuffix(".jsp");
        return resolver;
    }


}

1st attempt - implement WebMvcConfig with extends WebMvcConfigurationSupport.

@Configuration
@EnableWebMvc
@ComponentScan("com.projetoAnuncio")
public class WebMvcConfig extends WebMvcConfigurationSupport {

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

    @Override
    @Bean
    public HandlerMapping resourceHandlerMapping() {
        AbstractHandlerMapping handlerMapping = (AbstractHandlerMapping) super.resourceHandlerMapping();
        handlerMapping.setOrder(-1);
        return handlerMapping;
    }

    // equivalent for <mvc:default-servlet-handler/> tag
    @Override
     public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
         configurer.enable();
     }

}

2nd attempt - implement SecurityConfiguration with extends WebSecurityConfigurerAdapter

public class SecurityConfiguration extends WebSecurityConfigurerAdapter{

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/resources/**");    
    }

}

Below are some Images for better understanding

My package explorer

Myhead

In the url I already tried to put '/ resources /' in the front, but it did not work.

The project is for the last activity of a discipline in college. Thanks in advance for any help, so kind of urgent because I need to finish this week.

    
asked by anonymous 01.06.2016 / 06:15

1 answer

3

Well, I kept looking for content that would help me and I found the following steps that solved my problem.

1 Create the 'resources' folder within WebContent / webapp

2º Paste the html and / or css content and / or javascript and / or whatever

3rd Import Spring tags

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>

4 Include the import inside your page, as the example below:

<spring:url value="/resources/_css/yourStyle.css" var="yourStyleCSS"></spring:url>
<link rel="stylesheet" type="text/css" href="${yourStyleCSS}" />

5º Implement the following classes with their methods in your configuration package:

@EnableWebMvc
@ComponentScan("com.projetoAnuncio")
public class AppWebConfiguration extends WebMvcConfigurationSupport {

    // INICIO - essa parte foi a que fez o meu projeto quebrar
    @Bean
    public InternalResourceViewResolver internalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/view/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
    // FIM - essa parte foi a que fez o meu projeto quebrar

    // equivalents for <mvc:resources/> tags
    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }

    // equivalent for <mvc:default-servlet-handler/> tag
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    //retorna um objeto de HandlerMapping que é necessário (não me pergunte o pq rsrs)
    @Override
    @Bean
    public HandlerMapping resourceHandlerMapping() {
        AbstractHandlerMapping handlerMapping = (AbstractHandlerMapping) super.resourceHandlerMapping();
        handlerMapping.setOrder(-1);
        return handlerMapping;
    }

}

links from where I'll search for information:

1 - How to use spring MVC's tag in a java application context?
2 - Spring MVC 4.2.2 - Best way to Add / Integrate JS, CSS and images into JSP file using 'mvc: resources mapping'

To complete the initial basic configuration of spring via java, you need to implement the class below as well.

public class ServletSpringMCV extends AbstractAnnotationConfigDispatcherServletInitializer {

        @Override
        protected Class<?>[] getRootConfigClasses() {
            return null;
        }

        @Override
        protected Class<?>[] getServletConfigClasses() {
            return new Class[]{AppWebConfiguration.class};
        }

        @Override
        protected String[] getServletMappings() {
            // TODO Auto-generated method stub
            return new String[] { "/" };
        }


    }

I just needed this to solve the problem.

    
01.06.2016 / 07:29