1、pom文件导必要坐标和tomcat插件
javax.servlet
javax.servlet-api
3.1.0
provided
org.springframework
spring-webmvc
5.2.10.RELEASE
org.apache.tomcat.maven
tomcat7-maven-plugin
2.1
80
/
2、创建springmvc控制器
@Controller
public class UserController{
@RequestMappering("/save")
@ResponseBoay
public String save(){
system.out.pringln("user controller");
return "{'info':'springmvc'}";
}
}
3、初始化springmvc环境,设置springmvc加载环境
@Configuration
@ComonentScan("包名")
public class SpringMvcConfig{
}
4、初始化servlet容器,加载springmvc环境加载,设置sprinpmvc技术的请求
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringMvcConfig.class);
return ctx;
}
protected String[] getServletMappings() {
return new String[]{"/"};
}
protected WebApplicationContext createRootApplicationContext() {
return null;
}
}