xml零配置之WebMvcConfigurationSupport
发布时间:2020-12-16 09:13:57 所属栏目:百科 来源:网络整理
导读:之前 //配置dispatcher servlet,如果在root config指定了该转发规则就可以忽略 @Override protected Class?[] getServletConfigClasses() { return new Class?[] {WebMvcConfig.class}; } 我们了解了mvc在启动时利用servlet3.0"干了那些事",下面是如何编写
之前 //配置dispatcher servlet,如果在root config指定了该转发规则就可以忽略 @Override protected Class<?>[] getServletConfigClasses() { return new Class<?>[] {WebMvcConfig.class}; } 我们了解了mvc在启动时利用servlet3.0"干了那些事",下面是如何编写转发规则 package org.springframework.source.config; import static org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.support.ReloadableResourceBundleMessageSource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.validation.Validator; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; import org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect; import org.thymeleaf.spring4.SpringTemplateEngine; import org.thymeleaf.spring4.view.ThymeleafViewResolver; import org.thymeleaf.templateresolver.ServletContextTemplateResolver; import org.thymeleaf.templateresolver.TemplateResolver; import org.springframework.source.Application; @Configuration @ComponentScan(basePackageClasses = Application.class,includeFilters = @Filter(Controller.class),useDefaultFilters = false) class WebMvcConfig extends WebMvcConfigurationSupport { private static final String MESSAGE_SOURCE = "/WEB-INF/i18n/messages"; private static final String VIEWS = "/WEB-INF/views/"; private static final String RESOURCES_LOCATION = "/resources/"; private static final String RESOURCES_HANDLER = RESOURCES_LOCATION + "**"; //请求url(spring的url)映射到control的配置 @Override public RequestMappingHandlerMapping requestMappingHandlerMapping() { RequestMappingHandlerMapping requestMappingHandlerMapping = super.requestMappingHandlerMapping(); requestMappingHandlerMapping.setUseSuffixPatternMatch(false); requestMappingHandlerMapping.setUseTrailingSlashMatch(false); return requestMappingHandlerMapping; } //messageSource国际 @Bean(name = "messageSource") public MessageSource messageSource() { ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); messageSource.setBasename(MESSAGE_SOURCE); messageSource.setCacheSeconds(5); return messageSource; } //模版解析器 @Bean public TemplateResolver templateResolver() { TemplateResolver templateResolver = new ServletContextTemplateResolver(); templateResolver.setPrefix(VIEWS); templateResolver.setSuffix(".html"); templateResolver.setTemplateMode("HTML5"); templateResolver.setCacheable(false); return templateResolver; } //spring的模版引擎 @Bean public SpringTemplateEngine templateEngine() { SpringTemplateEngine templateEngine = new SpringTemplateEngine(); templateEngine.setTemplateResolver(templateResolver()); templateEngine.addDialect(new SpringSecurityDialect()); return templateEngine; } //ThymeleafViewResolver页面解析器 @Bean public ThymeleafViewResolver viewResolver() { ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver(); thymeleafViewResolver.setTemplateEngine(templateEngine()); thymeleafViewResolver.setCharacterEncoding("UTF-8"); return thymeleafViewResolver; } //Validator校验器 @Override public Validator getValidator() { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.setValidationMessageSource(messageSource()); return validator; } //add resource handlers for serving static resources. //静态资源Handlers @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(RESOURCES_HANDLER).addResourceLocations(RESOURCES_LOCATION); } //配置servlet处理 @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } /** * Handles favicon.ico requests assuring no <code>404 Not Found</code> error is returned. */ @Controller static class FaviconController { @RequestMapping("favicon.ico") String favicon() { return "forward:/resources/images/favicon.ico"; } } }这是doc文档 public class WebMvcConfigurationSupport
extends Object
implements ApplicationContextAware,ServletContextAware
This is the main class providing the configuration behind the MVC Java config. It is typically imported by adding
@EnableWebMvc to an application
@Configurationclass. An alternative more advanced option is to extend directly from this class and override methods as necessary remembering to add
@Configurationto the subclass and
@Beanto overridden
@Beanmethods. For more details see the Javadoc of
@EnableWebMvc.
This class registers the followingHandlerMappings:
Registers theseHandlerAdapters:
Registers aHandlerExceptionResolverCompositewith this chain of exception resolvers:
Registers anAntPathMatcherand aUrlPathHelperto be used by:
Both theRequestMappingHandlerAdapterand theExceptionHandlerExceptionResolverare configured with default instances of the following by default:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |