Я создал "на данный момент" простое и основное веб-приложение spring. Я использую дескриптор развертывания как простой файл web.xml, а затем контекст приложения как XML файл.
Хотя, теперь я хотел попытаться создать все мое веб-приложение spring, используя только java файлы. Поэтому я создал свой WebApplicationInitializer вместо обычного дескриптора развертывания и свой контекст приложения, который использует аннотацию @Configuration.
Дескриптор развертывания
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
Контекст приложения
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
Моя проблема в том, что я не могу найти способ "изолировать" мое сопоставление с папкой ресурсов, которая содержит изображения, css javascript и т.д. Когда мой контекст приложения находится в java.
В обычном контексте приложения XML я использовал этот тег, чтобы изолировать отображение в /resources/
<mvc:resources mapping="/resources/**" location="/resources/" />
Как я могу это сделать, поэтому мое веб-приложение может использовать мои изображения, css и т.д.