如何使用Spring MVC的< mvc:resources>在Java应用程序上下文中标记? [英] How to use spring MVC's <mvc:resources> tag in a java application context?
问题描述
我已经暂时"创建了一个简单而基本的spring Web应用程序.我习惯将部署描述符作为简单的web.xml文件,然后将应用程序上下文作为xml文件.
I have created 'for now' a simple and basic spring web application. I am used to have a deployment descriptor as a simple web.xml file, and then an application context as a xml file.
尽管如此,现在我想尝试仅使用Java文件创建整个Spring Web应用程序.因此,我创建了WebApplicationInitializer而不是常规部署描述符,并且创建了使用@Configuration批注的应用程序上下文.
Though, now i wanted to try to create my entire spring web application using only java files. Therefore i have created my WebApplicationInitializer instead of the normal deployment descriptor, and my application context which uses the @Configuration annotation.
部署描述符
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等的资源文件夹的映射.
My problem is that I can't seem to find a way 'isolate' my mapping to resources folder which contains images, css javascript etc. When my application context is in java.
在普通的XML应用程序上下文中,我使用此标记来隔离到/resources/
With the normal XML application context I used this tag to isolate the mapping to /resources/
<mvc:resources mapping="/resources/**" location="/resources/" />
我该怎么做,这样我的Web应用程序才能使用我的图像,CSS等.
How can I do this, so my web application can use my images, css etc.
推荐答案
要能够在Spring MVC应用程序中提供静态资源,您需要两个XML标签:< mvc:resources/>
和< mvc:default-servlet-handler/>
.在基于Java的Spring配置中,相同的是:
To be able to serve static resources in Spring MVC application you need two XML-tags: <mvc:resources/>
and <mvc:default-servlet-handler/>
. The same in the Java-based Spring configuration will be:
@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {
// equivalents for <mvc:resources/> tags
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**").addResourceLocations("/css/").setCachePeriod(31556926);
registry.addResourceHandler("/img/**").addResourceLocations("/img/").setCachePeriod(31556926);
registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(31556926);
}
// equivalent for <mvc:default-servlet-handler/> tag
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
// ... other stuff ...
}
请注意,由于使用了 @EnableWebMvc
批注,因此无需直接扩展 WebMvcConfigurationSupport
,您只需扩展 WebMvcConfigurerAdapter
.请参见 @EnableWebMvc的JavaDoc 了解详情.
Note that since @EnableWebMvc
annotation is used there's no need to extend directly WebMvcConfigurationSupport
, and you should just extend WebMvcConfigurerAdapter
. See JavaDoc for @EnableWebMvc for details.
这篇关于如何使用Spring MVC的< mvc:resources>在Java应用程序上下文中标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!