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

Asked

Viewed 1,601 times

0

I’m a beginner with Springmvc and I’m learning through the book Spring MVC (Master the main java web framework, by Alberto Souza, code house).

Before starting with Spring, I created a Maven project and implemented the frontend and persistence part with jpa + Ibernate. So far so good, I imported the project to the STS (Spring Tool Suite) and started to realize the settings explained in the book. But when I finished setting up Appwebconfiguration (following code below), my CSS and JS were no longer being applied.

I already googled, in the book, here in stackoverflow; I even implemented some possible solutions and the error persists. The project goes up, works normally, but without css and javascript. Below is what I implemented as attempts to solve the problem.

Below are the implemented codes

Class code with 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 a better understanding

My package explorer

Meu package explorer

My head

Meu head

In the url I tried to put the/Resources/' in front, but it did not result in anything.

The project is for the last activity of a discipline in college. Now, I appreciate any help, so it’s kind of urgent because I need to finish this week.

  • 1

    Welcome to Stack Overflow! Do not put the solved word in the text/title of the question, check instead the answer that solved your problem as certain.

  • @Jorgeb. As I was myself who managed to solve the problem, I can’t mark my answer as a right answer. What to do?

  • Jocsâ Yes you can mark your answer as sure, you have to wait 2 days if I’m not mistaken.

1 answer

4


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

1º Create the 'Resources' folder inside Webcontent/webapp

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

3º Import Spring tags

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

4º Include 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}" />

5th Implement the following class with your 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 searched the 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 basic initial 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 it to solve the problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.