xml零配置之WebMvcConfigurationSupport

前端之家收集整理的这篇文章主要介绍了xml零配置之WebMvcConfigurationSupport前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

之前

//配置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 @EnableWebMvcto 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:

Note that those beans can be configured with a PathMatchConfigurer.

Both theRequestMappingHandlerAdapterand theExceptionHandlerExceptionResolverare configured with default instances of the following by default:

Since:
3.1
原文链接:https://www.f2er.com/xml/297032.html

猜你在找的XML相关文章