spring基於java配置DispatcherServlet和view

yingxian_Fei發表於2017-04-16

本文基於java 的配置實現DispatcherServlet的配置。

1、建立初始化類

建立繼承AbstractAnnotationConfigDispatcherServletInitializer的初始化類,該類在容器初始化時會自動呼叫建立一個java類,該類繼承AbstractAnnotationConfigDispatcherServletInitializer,並實現其中必須實現的方法以及自己關心的方法,原始碼如下:

package cn.hifei.spring.demo.config;

import javax.servlet.Filter;

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

import cn.hifei.spring.demo.base.BaseConfiguration;
import cn.hifei.spring.demo.web.DefaultWebConfiguration;

public class DefaultWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{

	@Override
	protected Class<?>[] getRootConfigClasses() {
		// TODO Auto-generated method stub
		return new Class<?>[] { BaseConfiguration.class };
	}

	@Override
	protected Class<?>[] getServletConfigClasses() {
		// TODO Auto-generated method stub
		return new Class<?>[] { DefaultWebConfiguration.class };
	}

	@Override
	protected String[] getServletMappings() {
		// TODO Auto-generated method stub
		return new String[]{"/"};
	}

	@Override
	protected Filter[] getServletFilters() {
		HiddenHttpMethodFilter fl = new HiddenHttpMethodFilter();
		return new Filter[]{fl};
	}
}

2、建立用於配置web的配置類

建立一個繼承於WebMvcConfigurerAdapter的類用於配置servlet,一個工程中可以有多個servlet的配置。BaseConfiguration就是一個使用了@Configuration註解實現的類,裡面按照自己的需要新增bean的例項化即可,這裡就不給原始碼了。WebMvcConfigurerAdapter的原始碼如下:

package cn.hifei.spring.demo.web;

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.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan("cn.hifei.spring.demo.web")
public class DefaultWebConfiguration extends WebMvcConfigurerAdapter {
	@Bean
	public ViewResolver viewResolver() {
		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
		resolver.setPrefix("/WEB-INF/views/");
		resolver.setSuffix(".jsp");
		return resolver;
	}

	@Override
	public void configureDefaultServletHandling(
			DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
        super.addResourceHandlers(registry);
        registry.addResourceHandler("/static/**","/css/**");    
	}
}

如果需要引用基於xml的配置,可以使用如下類似的註解:

@ImportResource("classpath:applicationContext-*.xml")

3、修改web.xml

完成上述兩個配置的步驟之後,需要修改web.xml檔案去除同樣的配置,否則可能會造成衝突,如本文去除如下幾個部分的配置:

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/classes/spring-servlet.xml</param-value>
    </init-param>
    <init-param>  
        <param-name>spring.profiles.default</param-name>  
        <param-value>dev</param-value>  
    </init-param>     
    <load-on-startup>1</load-on-startup>
  </servlet>  
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

  <filter>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <servlet-name>spring</servlet-name>
  </filter-mapping>


4、建立測試的controller

建立一個用於測試的controller,用於測試配置是否生效。如本文使用的原始碼如下:

package cn.hifei.spring.demo.web.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

	@RequestMapping(value="hello",method=RequestMethod.GET)
	public String test() {
		return "hello,world";
	}
}

4、部署測試

部署工程到web伺服器,然後開啟瀏覽器訪問如下連線即可看到"hello,world"的輸出:

http://127.0.0.1:8080/SpringTest1/hello


相關文章