1. 程式人生 > >使用java程式碼啟動spring MVC

使用java程式碼啟動spring MVC

package cn.mg25.local.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;

/**
 * Created by weizhang on 2018/9/2.
 */
@Configuration
public class LocalAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{
//public class LocalAppInitializer {

    public LocalAppInitializer(){
        super();
        System.out.println("zzdebug-"+this.getClass().getName()+" constructing...");
    }

    @Override//攔截此url規則的所有請求
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    @Override//根配置類
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{RootConfig.class};
    }

    @Override//MVC配置類
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{WebConfig.class};
    }

    @Override//往filter中田間字串編碼轉化的過濾器
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        characterEncodingFilter.setForceEncoding(true);
        return new Filter[] {characterEncodingFilter};
    }
}