springMVC-java简单配置-不要web.xml

项目结构

image.png

pom文件


    4.0.0
    com.ghg
    msp02
    0.0.1-SNAPSHOT
    war

    
        UTF-8
        1.7
        4.2.9.RELEASE
        3.1.0
        2.2.1-b03
        1.2
    


    
        
        
            javax.servlet
            jstl
            ${version.jstl}
        
        
        
            javax.servlet.jsp
            jsp-api
            ${version.jsp}
            provided
        
        
        
            javax.servlet
            javax.servlet-api
            ${version.servlet}
            provided
        
        
        
            junit
            junit
            4.12
            test
        
        
        
            org.springframework
            spring-context
            ${version.springframework}
        
        
            org.springframework
            spring-web
            ${version.springframework}
        
        
            org.springframework
            spring-webmvc
            ${version.springframework}
        

        
            org.springframework
            spring-test
            ${version.springframework}
            test
        
        

        
            org.apache.commons
            commons-lang3
            3.7
        
        
            org.hibernate
            hibernate-validator
            6.0.8.Final
        
    


    
        msp02
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.7.0
                
                    ${version.jdk}
                    ${version.jdk}
                    ${project.build.sourceEncoding}
                
            
            
                org.apache.maven.plugins
                maven-war-plugin
                3.2.0
            

            
                org.eclipse.jetty
                jetty-maven-plugin
                9.4.9.v20180320
                
                    10
                    
                        /msp02
                    
                    
                        9091
                    
                
            
        
    

SpittrWebAppInitializer代替web.xml

package com.ghg.msp02.init;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import com.ghg.msp02.config.RootConfig;
import com.ghg.msp02.config.WebConfig;

public class SpittrWebAppInitializer  extends AbstractAnnotationConfigDispatcherServletInitializer{

    @Override
    protected Class[] getRootConfigClasses() {
        
        return new Class[] {RootConfig.class};
    }

    /**
     * 配置类
     */
    @Override
    protected Class[] getServletConfigClasses() {
        return new Class[] {WebConfig.class};
    }
    /**
     * url-partten
     */
    @Override
    protected String[] getServletMappings() {
        return new String[] {"/"};
    }

}

RootConfig

package com.ghg.msp02.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;

@Configuration
@ComponentScan(basePackages= {"com.ghg.msp02"},
excludeFilters= {
        @Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)
})
public class RootConfig {

}

WebConfig

package com.ghg.msp02.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages= {"com.ghg.msp02"})
public class WebConfig extends WebMvcConfigurerAdapter {

    
    /**
     * 配置JSP视图解析器
    * @Title: viewResolver
    * @Description: TODO(这里用一句话描述这个方法的作用)
    * @param @return    设定文件
    * @return ViewResolver    返回类型
    * @throws
     */
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/view/");
        resolver.setSuffix(".jsp");
        
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }
    
    
    
    /**
     * 配置静态资源处理
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

你可能感兴趣的:(springMVC-java简单配置-不要web.xml)