首页 > 代码库 > WebMvcConfigurerAdapter

WebMvcConfigurerAdapter

 

import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.http.MediaType;import org.springframework.web.multipart.MultipartResolver;import org.springframework.web.multipart.commons.CommonsMultipartResolver;import org.springframework.web.servlet.ViewResolver;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;import org.springframework.web.servlet.view.InternalResourceViewResolver;import org.springframework.web.servlet.view.JstlView;import org.springframework.web.servlet.view.json.MappingJackson2JsonView;import org.springframework.web.servlet.view.tiles2.TilesConfigurer;import org.springframework.web.servlet.view.tiles2.TilesViewResolver;import java.util.*;/** * Created by MyWorld on 2016/11/13. */@Configuration@EnableWebMvcpublic class WebMvcConfig extends WebMvcConfigurerAdapter {    @Override    public void addInterceptors(InterceptorRegistry registry) {        super.addInterceptors(registry);    }    @Bean    public ViewResolver viewResolver() {        InternalResourceViewResolver viewResolver=new InternalResourceViewResolver();        viewResolver.setPrefix("/WEB-INF/views/");        viewResolver.setSuffix(".jsp");        viewResolver.setViewClass(JstlView.class);//将视图解析为JstlView而不是InternalResourceView        return viewResolver;    }    @Bean    public TilesConfigurer tilesConfigurer() {        final TilesConfigurer configurer = new TilesConfigurer();//        configurer.setDefinitions(new String[] { "WEB-INF/views_common.xml" });        configurer.setDefinitions(new String[]{"/WEB-INF/views_*.xml"});        configurer.setCheckRefresh(true);        return configurer;    }    @Bean    public TilesViewResolver tilesViewResolver() {        return new TilesViewResolver();    }    @Bean    public MappingJackson2JsonView mappingJackson2JsonView() {        return new MappingJackson2JsonView();    }    @Bean    public ContentNegotiatingViewResolver contentNegotiatingViewResolver(TilesViewResolver tilesViewResolver, MappingJackson2JsonView mappingJackson2JsonView) {        ContentNegotiatingViewResolver viewResolver = new ContentNegotiatingViewResolver();        viewResolver.setIgnoreAcceptHeader(true);        viewResolver.setDefaultContentType(MediaType.TEXT_HTML);        Map<String, String> map = new HashMap<>();        map.put("atom", "application/atom+xml");        map.put("html", "text/html");        map.put("json", "application/json");        viewResolver.setMediaTypes(map);        viewResolver.setFavorParameter(false);        List<ViewResolver> list = new ArrayList<>();        list.add(tilesViewResolver);        viewResolver.setViewResolvers(list);        viewResolver.setDefaultViews(Collections.singletonList(mappingJackson2JsonView));        return viewResolver;    }    @Bean    public MultipartResolver multipartResolver() {        CommonsMultipartResolver  commonsMultipartResolver=new CommonsMultipartResolver();        commonsMultipartResolver.setDefaultEncoding("utf-8");        commonsMultipartResolver.setMaxUploadSize(10485760000L);        commonsMultipartResolver.setMaxInMemorySize(40960);        return commonsMultipartResolver;    }}

 

WebMvcConfigurerAdapter