Salve,
sto realizzando un e-commerce con Spring 5.
Ho un problema che non riesco a risolvere: come inserire le risorse statiche (immagini, js, css) nel progetto.
Fino adesso ho realizzato due classi di configurazione come in figura:
codice:
package com.emoda.webapp.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.emoda.webapp")
public class WebApplicationContextConfig implements WebMvcConfigurer
{
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/public", "classpath:/static/")
.setCachePeriod(31556926);
}
@Bean
public InternalResourceViewResolver getInternalResourceViewResolver()
{
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/WEB-INF/view/");
resolver.setSuffix(".jsp");
return resolver;
}
}
package com.emoda.webapp.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer
{
@Override
protected Class<?>[] getRootConfigClasses()
{
// TODO Auto-generated method stub
return null;
}
@Override
protected Class<?>[] getServletConfigClasses()
{
return new Class[]
{
WebApplicationContextConfig.class
};
}
@Override
protected String[] getServletMappings()
{
return new String[] { "/" };
}
}
poi ho inserito le risorse dentro src/main/resources creando una cartella static e inserendo le immagini in una sottocartella images.
Le immagini però non vengono caricate. Perchè?
saluti
Tulipan