b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

spring mvc dispatcherservlet 07:Spring MVC用JavaConfig代替xml搭(2)

电脑杂谈  发布时间:2018-01-21 00:17:51  来源:网络整理

相当于web.xml

package pers.hanchao.configuration;

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

/**
 * <p>实现了WebApplicationInitializer接口,是web.xml的替代方式</p>
 * @author hanchao 2018/1/20 19:40
 **/
public class MyWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    /**
     * <p>用于获取Spring应用容器的配置文件</p>
     * @author hanchao 2018/1/20 19:55
     **/
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{MySpringMVCConfiguration.class};
    }

    /**
     * <p>获取Spring MVC应用容器</p>
     * @author hanchao 2018/1/20 19:55
     **/
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[0];
    }

    /**
     * <p>负责指定需要由DispatcherServlet映射的路径</p>
     * @author hanchao 2018/1/20 19:56
     **/
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}

相当于spring-mvc-servlet.xml

package pers.hanchao.configuration;

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.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

//声明这是一个配置文件,类似于applicationcontext.xml
@Configuration
//开启注解,等同于<mvc:annotation-driven/>
@EnableWebMvc
//开启自动装配,等同于 context:component-scan base-package="pers.hanchao.*"
@ComponentScan(basePackages = "pers.hanchao.*")
public class MySpringMVCConfiguration {

    /**
     * 视图解析器,相当于:
     * <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     *      <property name="prefix" value="/"/>
     *      <property name="suffix" value=".jsp"/>
     * </bean>
     */
    @Bean
    public ViewResolver viewResolver(){
        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
        internalResourceViewResolver.setPrefix("/");
        internalResourceViewResolver.setSuffix(".jsp");
        return internalResourceViewResolver;
    }
}


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-61616-2.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    • 爱育黎拔力八达
      爱育黎拔力八达

      我在台服玩过网游

    热点图片
    拼命载入中...