Buongiorno,
sto provando a realizzare un progetto con Spring 5 e poiché ho già il layout delle pagine web che si devono implementare mi chiedevo come posso inserire le immagini , i vari CSS che posseggo senza passare da Apache Tiles.
tulipan
Buongiorno,
sto provando a realizzare un progetto con Spring 5 e poiché ho già il layout delle pagine web che si devono implementare mi chiedevo come posso inserire le immagini , i vari CSS che posseggo senza passare da Apache Tiles.
tulipan
Dipende .... intanto presumo che essendoci Spring il progetto sia "Maven" con nel pom <packaging>war</packaging> . In tal caso src/main/webapp è la context "root".
Se il DispatcherServlet di Spring fosse mappato su un path specifico, es. /spring, allora puoi mettere altro sotto webapp ed è accessibile direttamente, es. src/main/webapp/css/prova.css è esposto su http://blabla/nomecontesto/css/prova.css
Se invece il DispatcherServlet è mappato per intercettare "tutto" (cosa più tipica), allora devi registrare un handler in Spring per le risorse statiche, 1.10.10. Static Resources
Vedi anche 1.10.11. Default Servlet perché c'è anche un'altra possibilità.
Ultima modifica di andbin; 20-09-2019 a 09:35
Andrea, andbin.dev – Senior Java developer – SCJP 5 (91%) • SCWCD 5 (94%)
java.util.function Interfaces Cheat Sheet — Java Versions Cheat Sheet
Ho provato a registrare un handler per le risorse statiche ma non ottengo niente.
Il DispatcherServlet è il seguente:
codice:package com.xantrix.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.DefaultServletHandlerConfigurer; 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.xantrix.webapp") public class WebApplicationContextConfig implements WebMvcConfigurer { public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Bean public InternalResourceViewResolver getInternalResourceViewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setViewClass(JstlView.class); resolver.setPrefix("/WEB-INF/view/"); resolver.setSuffix(".jsp"); return resolver; } public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/AnnunciImmobiliari/**") .addResourceLocations("/public", "classpath:/static/") .setCachePeriod(31556926); } }
Attenzione, un conto è usare il registry.addResourceHandler (il mapping delle risorse "statiche" è fatto da Spring), un altro conto è usare il configurer.enable() nel configureDefaultServletHandling (per le risorse statiche Spring "passa la palla" al default Servlet del container).
Se usi solo il registry.addResourceHandler e il DispatcherServlet di Spring è mappato su /*, allora con:
codice:registry.addResourceHandler("/AnnunciImmobiliari/**") .addResourceLocations("/public", "classpath:/static/")
Se viene chiesto http://host/nomecontesto/AnnunciImmobiliari/stili.css allora Spring va a cercare un public/stili.css nella context-root (nel progetto maven sotto src/main/webapp) e poi "in classpath" un /static/stili.css
Andrea, andbin.dev – Senior Java developer – SCJP 5 (91%) • SCWCD 5 (94%)
java.util.function Interfaces Cheat Sheet — Java Versions Cheat Sheet