Jak używać tagu <mvc: resources> wiosennego MVC w kontekście aplikacji Java?
Stworzyłem „na razie” prostą i podstawową aplikację wiosenną. Używam deskryptora wdrażania jako prostego pliku web.xml, a następnie kontekstu aplikacji jako pliku xml.
Chociaż teraz chciałem spróbować utworzyć całą moją wiosenną aplikację internetową, używając tylko plików Java. Dlatego utworzyłem mój WebApplicationInitializer zamiast normalnego deskryptora wdrażania i mój kontekst aplikacji, który używa adnotacji @Configuration.
Deskryptor wdrażania
package dk.chakula.config;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
/**
*
* @author martin
* @since 12-1-2012
* @version 1.0
*/
public class Initializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext)
throws ServletException {
registerDispatcherServlet(servletContext);
}
private void registerDispatcherServlet(final ServletContext servletContext) {
WebApplicationContext dispatcherContext = createContext(ChakulaWebConfigurationContext.class);
DispatcherServlet dispatcherServlet = new DispatcherServlet(dispatcherContext);
Dynamic dispatcher = servletContext.addServlet("dispatcher", dispatcherServlet);
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
private WebApplicationContext createContext(final Class<?>... annotatedClasses) {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(annotatedClasses);
return context;
}
} //End of class Initializer
Kontekst aplikacji
package dk.chakula.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.view.UrlBasedViewResolver;
import org.springframework.web.servlet.view.tiles2.TilesConfigurer;
import org.springframework.web.servlet.view.tiles2.TilesView;
/**
*
* @author martin
* @since 12-01-2013
* @version 1.0
*/
@Configuration
@EnableWebMvc
@ComponentScan("dk.chakula.web")
public class ChakulaWebConfigurationContext {
@Bean
public TilesConfigurer setupTilesConfigurer() {
TilesConfigurer configurer = new TilesConfigurer();
String[] definitions = {"/layout/layout.xml"};
configurer.setDefinitions(definitions);
return configurer;
}
@Bean
public UrlBasedViewResolver setupTilesViewResolver() {
UrlBasedViewResolver viewResolver = new UrlBasedViewResolver();
viewResolver.setViewClass(TilesView.class);
return viewResolver;
}
} //End of class ChakulaWebConfigurationContext
Moim problemem jest to, że nie mogę znaleźć sposobu „izolowania” mojego mapowania na folder zasobów, który zawiera obrazy, css javascript itp. Gdy mój kontekst aplikacji jest w java.
W normalnym kontekście aplikacji XML użyłem tego tagu do wyizolowania mapowania na / resources /
<mvc:resources mapping="/resources/**" location="/resources/" />
Jak mogę to zrobić, aby moja aplikacja internetowa mogła używać moich obrazów, css itp.