首页 > 代码库 > 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。