我已经创建了"for now"一个简单而基本的Spring Web应用程序.我习惯将部署描述符作为简单的web.xml文件,然后将应用程序上下文作为xml文件.
虽然,现在我想尝试仅使用java文件创建我的整个spring Web应用程序.因此,我创建了我的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
Run Code Online (Sandbox Code Playgroud)
应用背景
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
Run Code Online (Sandbox Code Playgroud)
我的问题是,我似乎无法找到一种方法'隔离'我的映射到包含图像,css javascript等资源文件夹.当我的应用程序上下文是在java中.
使用普通的XML应用程序上下文,我使用此标记将映射隔离到/ resources /
<mvc:resources mapping="/resources/**" location="/resources/" />
Run Code Online (Sandbox Code Playgroud)
我该怎么做,所以我的网络应用程序可以使用我的图像,CSS等.
Art*_*nov 32
为了能够在Spring MVC应用程序中提供静态资源,您需要两个XML标记:<mvc:resources/>和<mvc:default-servlet-handler/>.基于Java的Spring配置中的相同内容如下:
@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 ...
}
Run Code Online (Sandbox Code Playgroud)
请注意,由于使用了@EnableWebMvc注释,因此无需直接扩展WebMvcConfigurationSupport,您只需要扩展即可WebMvcConfigurerAdapter.有关详细信息,请参阅@EnableWebMvc的JavaDoc.
在互联网上使用数小时搜索只使用java文件的Spring MVC 3后,我查看了一些文章,这些文章使用了从WebMvcConfigurationSupport类扩展的方法,然后重写了2个方法 - addResourceHandler(ResourceHandlerRegistry)和ResourceHandlerMapping().
我现在的新应用程序上下文如下所示.
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.HandlerMapping;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.handler.AbstractHandlerMapping;
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 extends WebMvcConfigurationSupport {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Override
@Bean
public HandlerMapping resourceHandlerMapping() {
AbstractHandlerMapping handlerMapping = (AbstractHandlerMapping) super.resourceHandlerMapping();
handlerMapping.setOrder(-1);
return handlerMapping;
}
@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
Run Code Online (Sandbox Code Playgroud)
据我所知,我们必须覆盖addResourceHandler,以添加位置和资源到注册表的映射.此后我们需要一个返回HandlerMapping对象的bean.此HandlerMapping的顺序应设置为-1,因为我可以从spring文档中读取,然后-1表示
HandlerMapping以Integer.MAX_VALUE-1排序,以提供静态资源请求.
我的应用程序现在可以将css文件和图像加载到他们的视图中,我想通过答案启发其他人,以便将来的人可以从中受益.
| 归档时间: |
|
| 查看次数: |
23078 次 |
| 最近记录: |