【spring in action 學習--springMVC搭建】在不使用web.xml的狀況下,配置Srping MVC工程

1、Spring MVC 簡介  java

  DispatcherServlet是Spring MVC的核心,他負責將請求路由到其餘的組件中。 在servlet3.0 以前,傳統的搭建Spring MVC工程時,像DispatcherServlet這樣Servlet都會配置在web.xml文件中。web

  在servlet3.0 中對功能進行了加強, 因此不須要將DispatcherServlet配置在web.xml中。spring

 

  

 

 2、 相關代碼app

一、webconfig---> 這個文件中主要是用來幾個功能:a、啓用 Spring MVC ;b、配置視圖解析; c、配置靜態資源之類;jsp

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();
	}
}

 

二、RootConfig --->ide

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 {

}

  

三、 SpittrWebAppInitializer ---> 該文件也是就是配置了DispatcherServletspa

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[] {"/"};
	}

}

 

四、聲明一個控制器xml

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";
	}
}
相關文章
相關標籤/搜索