需要idea ultimate版本,使用社区版本不支持spring开发
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>4.3.8.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>4.3.8.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>4.3.8.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>4.3.8.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>4.3.8.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>4.3.8.RELEASEversion>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>4.3.8.RELEASEversion>
dependency>
<dependency>
<groupId>com.github.stefanbirknergroupId>
<artifactId>system-rulesartifactId>
<version>RELEASEversion>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
dependency>
在AbstractContextLoaderInitializer.onStartup()函数中初始化createRootApplicationContext(),
关注的getRootConfigClasses就是这步进行注册的。
在AbstractDispatcherServletInitializer.onStartup()函数中初始化createServletApplicationContext(),
getServletConfigClasses在这步注册上的。
需要继承AbstractAnnotationConfigDispatcherServletInitializer,并实现getRootConfigClasses(),
getServletConfigClasses()和getServletMappings()三个函数
代码如下:
package spittr.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
/**
* Created by Administrator on 2017/5/30.
*/
public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class>[] getRootConfigClasses() {
return new Class>[] { RootConfig.class};
}
@Override
protected Class>[] getServletConfigClasses() {
return new Class>[] { WebConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
}
RootConfig的实现:
package spittr.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
/**
* Created by Administrator on 2017/5/30.
*/
@Configuration
@ComponentScan(basePackages={"spittr"},
excludeFilters = {
@ComponentScan.Filter(type= FilterType.ANNOTATION, value= EnableWebMvc.class)
})
public class RootConfig {
}
WebConfig的实现:
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;
/**
* Created by Administrator on 2017/5/30.
*/
@Configuration
@EnableWebMvc
@ComponentScan("spittr.web")
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver =
new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
public void configureDefaultServletHandler(
DefaultServletHandlerConfigurer configurer
) {
configurer.enable();
}
}
package spittr.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
/**
* Created by Administrator on 2017/5/30.
*/
@Controller
public class HomeController {
@RequestMapping(value = "/", method = GET)
public String home() {
System.out.println("get request!!!");
return "home";
}
}
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Spittrtitle>
<link rel="stylesheet"
type="text/css"
href=" " >
head>
<body>
<h1>Welcome to Spittrh1>
<a href=" ">Spittlesa> |
<a href=" ">Registera>
body>
html>