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.