一、创建工程,引入依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.6.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency>
二、添加Spring配置类
@Configuration @ComponentScan(basePackages = "com.choi", useDefaultFilters = true, excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Controller.class)}) public class SpringConfig { }
- @Configuration:表示该类是一个配置类
- @ComponentScan:表示配置包扫描,useDefaultFilters表示使用默认的过滤器,如需关闭,则值为false,在Spring配置类中需要除去@Controller注解,因此需要使用excludeFilters来进行过滤,即在Spring容器中除了@Controller之外的都要进行扫描
三、配置SpringMVC配置类
@Configuration
@ComponentScan(basePackages = "com.choi",useDefaultFilters = false,includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Controller.class)})
public class SpringMVCConfig {
}
在这个SpringMVC配置类中,我们就可以像XML文件那样配置开发所需要的额外配置,如视图解析器、文件上传、路径映射==
注意:在真实开发中,可以将Spring配置跟SpringMVC合在一起,也就是只用SpringMVC配置,只需将@ComponentScan注解中的过滤删除即可,所以我就把上面两个配置合在一起,如下所示
@Configuration @ComponentScan(basePackages = "com.choi") public class SpringMVCConfig extends WebMvcConfigurationSupport { }
四、配置web.xml
在以前开发web.xml文件主要是用来加载spring配置文件和springmvc配置文件的,同样的我们也可以使用Java的方式来进行加载,只需要实现WebApplicationInitializer接口,实现该接口下的onStartup方法即可
/** * web.xml配置类 */ public class WebInit implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { //加载spring mvc配置类 AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(SpringMVCConfig.class); //添加dispatcherServlet ServletRegistration.Dynamic springmvc = servletContext.addServlet("springmvc", new DispatcherServlet(ctx)); //给dispatcherServlet添加映射路径 springmvc.addMapping("/"); //给dispatchServlet添加启动时机 springmvc.setLoadOnStartup(1); } }
当项目一启动时,onStartup方法就会被触发,我们就可以在该配置类中做一些初始化操作,如加载SpringMVC容器、添加过滤器、添加***
五、进行测试
配置Tomcat进行测试
六、在SpringMVC配置类中配置其他配置
在xml文件中我们配置静态资源过滤、视图解析器、路径映射的方式如下:
<mvc:resources mapping="/**" location="/"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:view-controller path="/hello" view-name="hello" status-code="200"/>
而使用纯Java同样也可以进行配置,如下所示,只需要让SpringMVC配置类继承WebMvcConfigurationSupport ,实现相应的方法即可
@Configuration @ComponentScan(basePackages = "com.choi") public class SpringMVCConfig extends WebMvcConfigurationSupport { /** * 过滤静态资源 * @param registry */ @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/"); } /** * 配置视图解析器 */ @Override protected void configureViewResolvers(ViewResolverRegistry registry) { registry.jsp("/jsp/",".jsp"); } /** * 配置路径映射 */ @Override protected void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/hello2").setViewName("hello"); } }
这里面有更具体的内容!