Spring MVC纯注解配置工程简单实例——Hello World

阅读更多

JDK:1.8

Tomcat:8.5.x

 

工程目录结构


Spring MVC纯注解配置工程简单实例——Hello World_第1张图片
 

 

pom.xml配置


	4.0.0
	com.linzx.test
	test-springannotationconfig
	0.0.1-SNAPSHOT
	war

	
		UTF-8
		1.8
		4.3.3.RELEASE
	

	
		
		
			org.springframework
			spring-webmvc
			${org.springframework.version}
		

		
			javax.servlet
			javax.servlet-api
			4.0.1
			provided
		

		
			junit
			junit
			4.12
			test
		
	

	
		
			
				org.apache.maven.plugins
				maven-compiler-plugin
				3.7.0
				
					1.8
					1.8
				
			

			
				org.apache.maven.plugins
				maven-war-plugin
				3.2.2
			
		
	

 

web.xml的注解配置类

package com.linzx.test.springweb.config;

import javax.servlet.FilterRegistration.Dynamic;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

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

/**
 * web.xml
 * 继承AbstractAnnotationConfigDispatcherServletInitializer,会同时创建DispatcherServlet和ContextLoaderListener
 */
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class[] getRootConfigClasses() {
        return new Class[] { ApplicationConfig.class };
    }

    @Override
    protected Class[] getServletConfigClasses() {
        return new Class[] { WebMvcConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        
        Dynamic characterEncoding = servletContext.addFilter("characterEncoding", CharacterEncodingFilter.class);
        characterEncoding.setInitParameter("forceEncoding", "true");
        characterEncoding.setInitParameter("encoding", "UTF-8");
        characterEncoding.addMappingForUrlPatterns(null, true, "/*");
    }
}

 

spring-mvc.xml的注解配置类

package com.linzx.test.springweb.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * Spring-MVC配置,相当于spring-mvc.xml
 */
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.linzx.test.springweb.controller", includeFilters = @Filter(classes = Controller.class), useDefaultFilters = false)
public class WebMvcConfig extends WebMvcConfigurerAdapter {

}

 

application.xml的注解配置类

package com.linzx.test.springweb.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;

/**
 * 业务类配置,相当于application.xml
 */
@Configuration
@ComponentScan(basePackages = { "com.linzx.test.springweb" }, excludeFilters = { @Filter(classes = Controller.class) })
public class ApplicationConfig {

}

 

Controller

package com.linzx.test.springweb.controller;

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

@RestController
public class HelloController {

    @RequestMapping("/")
    public String hello() {
        return "Hello World!";
    }
}

 

访问结果



 

  • Spring MVC纯注解配置工程简单实例——Hello World_第2张图片
  • 大小: 77.7 KB
  • Spring MVC纯注解配置工程简单实例——Hello World_第3张图片
  • 大小: 6.4 KB
  • 查看图片附件

你可能感兴趣的:(springmvc,无web.xml,纯注解配置,Hello,World)