chuxue1989的专栏

自己学习历程的记录,每天记录一点点

spring boot web开发------自定义springmvc配置

1、如何自定义springmvc配置
旧的springmvc配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    <!--扫描该包及子包的所有注解 -->
    <context:component-scan base-package="com"
        use-default-filters="false"><!---->
        <!--只扫描@Controller use-default-filtes="false" 必须设置 -->
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Controller"></context:include-filter>
    </context:component-scan>

    <!--处理在方法级别上的@RequestMapping注解 -->
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <!--启动Spring MVC的注解功能,设置编码方式,防止乱码 -->
                <bean
                    class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/html;charset=UTF-8</value>
                            <value>text/plain;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>

                <bean
                    class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
                <bean
                    class="org.springframework.http.converter.ResourceHttpMessageConverter" />
                <bean
                    class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
                <bean
                    class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" />
                <bean
                    class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>application/json;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
            </list>
        </property>
    </bean>
    <!--对转向页面的路径解析 -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/pages/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- SpringMVC上传 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
        p:defaultEncoding="UTF-8">
        <property name="maxUploadSize">
            <value>104857600</value>
        </property>
        <property name="maxInMemorySize">
            <value>4096</value>
        </property>
    </bean>
</beans>

第一种方式java版本

package cn.itcast.springboot.demo;

import java.nio.charset.Charset;

import javax.servlet.MultipartConfigElement;

import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import cn.itcast.springboot.demo.intercept.Intercept1;

@Configuration
public class SpringMvcConfigure extends WebMvcConfigurerAdapter{

    /**
     * 对转向页面的路径解析 
     * 2018年4月15日
     * @return
     */
    @Bean
    public InternalResourceViewResolver viewResolver(){
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/pages/");
        viewResolver.setSuffix(".jsp");
        // viewResolver.setViewClass(JstlView.class); // 这个属性通常并不需要手动配置,高版本的Spring会自动检测
        return viewResolver;
    }

    /**  
     * 文件上传配置  
     * @return  
     */  
    @Bean  
    public MultipartConfigElement multipartConfigElement() {  
        MultipartConfigFactory factory = new MultipartConfigFactory();  
        //文件最大  
        factory.setMaxFileSize("10240KB"); //KB,MB  
        /// 设置总上传数据总大小  
        factory.setMaxRequestSize("102400KB");  
        return factory.createMultipartConfig();  
    }  

    /**
     * 消息转换器配置
     * 2018年4月15日
     * @return
     */
    @Bean
    public StringHttpMessageConverter stringHttpMessageConverter(){
        return  new StringHttpMessageConverter(Charset.forName("UTF-8"));
    }

    /**
     * 拦截器配置
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new Intercept1()).addPathPatterns("/**");
    }


}

第二种方式配置文件
在application.properties

spring.mvc.view.prefix= # Spring MVC view prefix.
spring.mvc.view.suffix= # Spring MVC view suffix.

spring.http.multipart.maxFileSize=10Mb  
spring.http.multipart.maxRequestSize=10Mb 
阅读更多
个人分类: spring boot
上一篇Spring boot自动配置
下一篇一、mybatis入门
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭