【spring in action 学习--springMVC搭建】在不使用web.xml的情况下,配置Srping MVC工程
一、Spring MVC 简介
DispatcherServlet是Spring MVC的核心,他负责将请求路由到其他的组件中。 在servlet3.0 之前,传统的搭建Spring MVC工程时,像DispatcherServlet这样Servlet都会配置在web.xml文件中。
在servlet3.0 中对功能进行了增强, 所以不需要将DispatcherServlet配置在web.xml中。
二、 相关代码
1、webconfig---> 这个文件中主要是用来几个功能:a、启用 Spring MVC ;b、配置视图解析; c、配置静态资源之类;
package spittr.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 : 表明这个是spring的配置文件 * @EnableWebMvc : 启用Spring MVC * @ComponentScan : 组件扫描 */ @Configuration @EnableWebMvc @ComponentScan("spittr") public class WebConfig extends WebMvcConfigurerAdapter{ /* * 配置jsp视图解析器 */ @Bean public ViewResolver viewResolver(){ InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); resolver.setExposeContextBeansAsAttributes(true); return resolver; } /* * 配置静态资源的处理 * @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter#configureDefaultServletHandling(org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer) */ @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } }
2、RootConfig --->
package spittr.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.web.servlet.config.annotation.EnableWebMvc; @Configuration @ComponentScan(basePackages={"spittr"}, excludeFilters={ @Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class)}) public class RootConfig { }
3、 SpittrWebAppInitializer ---> 该文件也是就是配置了DispatcherServlet
package spittr.config; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{ @Override protected Class<?>[] getRootConfigClasses() { // TODO Auto-generated method stub return new Class<?>[] {RootConfig.class}; } @Override protected Class<?>[] getServletConfigClasses() { // TODO Auto-generated method stub return new Class<?>[] {WebConfig.class}; } @Override protected String[] getServletMappings() { // TODO Auto-generated method stub return new String[] {"/"}; } }
4、声明一个控制器
package spittr.web; import static org.springframework.web.bind.annotation.RequestMethod.*; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /* * 声明一下控制器 */ @Controller public class HomeController { @RequestMapping(value="/", method=GET) public String home(){ return "home"; } }