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
My 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.
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.
– Jorge B.
@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ã
Jocsâ Yes you can mark your answer as sure, you have to wait 2 days if I’m not mistaken.
– Jorge B.