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
@EnableWebMvc
public 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;
    }

}
时间: 2024-10-28 22:07:15

WebMvcConfigurerAdapter的相关文章

使用WebMvcConfigurerAdapter 做登录,失效的一个小小原因

package com.github.carter659.spring13; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.context.annotation.Bean; import org.springframework.

spring boot拦截器WebMvcConfigurerAdapter,以及高版本的替换方案

最近项目采用spring icloud,用的spring boot版本是1.5.x的,spring boot 2.0,Spring 5.0 以后WebMvcConfigurerAdapter会取消掉.以下介绍下大体的内容,希望对大家都有所帮助. 以下WebMvcConfigurerAdapter 比较常用的重写接口 /** 解决跨域问题 **/ public void addCorsMappings(CorsRegistry registry) ; /** 添加拦截器 **/ void addI

SpringBoot---页面跳转之WebMvcConfigurerAdapter

摘要:在springboot中定义自己的方法继承WebMvcConfigurerAdapter方法可以实现扩展springMvc功能,要全面实现接管springmvc就要在自己的方法上加上@EnableWebMvc注解. 首先看WebMvcConfigurerAdapter部分源码: @Deprecated//看标色部分就是实现了WebMvcConfigurer接口 因此可以理解为什么说扩展springmvc功能 public abstract class WebMvcConfigurerAda

spring boot 2.0 WebMvcConfigurerAdapter过时解决方法

第一种: @Configuration public class WebAppConfig implements WebMvcConfigurer{ @Bean public HandlerInterceptor getLoginInterceptor(){ return new LoginInterceptor(); } @Override public void addInterceptors(InterceptorRegistry registry){ registry.addInterc

SprigBoot中的 WebMvcConfigurer与 WebMvcConfigurerAdapter和 WebMvcConfigurationSupport

WebMvcConfigurationAdapter 过时? 在SpringBoot2.0之后的版本中WebMvcConfigurerAdapter过时了,所以我们一般采用的是如下的两种的解决的方法. (1)继承WebMvcConfigureSupport 出现的问题:静态资源的访问的问题,在静态资源的访问的过程中,如果继承了WebMvconfigureSupport的方法的时候,SpringBoot中的自动的配置会失效. @ConditionalOnMissingBean({WebMvcCon

Spring MVC的WebMvcConfigurerAdapter用法收集(零配置,无XML配置)

原理先不了解,只记录常用方法 用法: @EnableWebMvc 开启MVC配置,相当于 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi

WebMvcConfigurerAdapter详解和过时后的替代方案

一.什么是WebMvcConfigurerAdapter Spring内部的一种配置方式采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制 二.WebMvcConfigurerAdapter常用的方法 /** 解决跨域问题 **/ public void addCorsMappings(CorsRegistry registry) ; /** 添加拦截器 **/ void addInterceptors(InterceptorRegistry registry); /

SpringBoot2.0及Spring 5.0 WebMvcConfigurerAdapter被废弃解决办法

1 直接实现WebMvcConfigurer @Configuration public class WebMvcConfg implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/index").setViewName("index"); } } 2 直

Integrating Thymeleaf with Spring

一.整体结构图 二.web.xml文件,这里使用了注解的方式 <?xml version="1.0" encoding="utf-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http: