简述
- SpringMVC技术与Servlet功能相同,均属于web层开发技术
- SpringMVC是一种基于Java实现MVC模型的轻量级Web框架
复习Servlet开发
@WebServlet("/user/save")
public class UserSaveServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.请求参数
String name = request.getParameter("name");
System.out.println("servlet save name ==>" + name);
//2.产生影响
response.setContentType("text/json;charset=utf-8");
PrintWriter pw = response.getWriter();
pw.write("{'module':'servlet save'}");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
快速入门案例
①使用SpringMVC技术需要先导入SpringMVC坐标与Servlet坐标
4.0.0
org.itheima
spring_01_quickstart
1.0-SNAPSHOT
war
UTF-8
1.8
1.8
- 然后自己导入坐标,注意这里版本号不能乱改,不然会有版本冲突不能运行!
javax.servlet
javax.servlet-api
3.1.0
provided
org.springframework
spring-webmvc
5.2.10.RELEASE
- 可以看到spring-webmvc已经将之前学习的包都有所导入,所以不用导入那些包
②创建SpringMVC控制器类(等同于Servlet功能)
//2.定义Controller
//2.1使用@Controller定义bean
@Controller
public class UserController {
//2.2设置当前操作的访问路径
@RequestMapping("/save")
//2.3设置当前操作的返回值类型,相当于将函数类型的返回值作为一个整体返回
@ResponseBody
public String save() {
System.out.println("user save ... ");
return "{'info':'springmvc'}";
// return "Hello";
}
//设置映射路径为/delete,即外部访问路径
@RequestMapping("/delete")
@ResponseBody
public String delete() {
System.out.println("user delete ...");
return "{'info':'springmvc'}";
}
}
③初始化SpringMVC环境(同Spring环境),设定SpringMVC加载对应的bean
//3.创建springmvc配置文件,加载controller对应的bean
@Configuration
@ComponentScan("com.itheima.controller")
public class SpringMvcConfig {
}
④初始化Servlet容器,加载SpringMVC环境,并设置SpringMVC技术处理的请求
//4.定义一个servlet容器启动的配置类,在里面加载spring配置,要继承一个接口,实现三个方法
public class ServletContainerInitConfig extends AbstractDispatcherServletInitializer {
// 加载springMVC容器配置
@Override
protected WebApplicationContext createServletApplicationContext() {
// AnnotationConfigApplicationContext ctx =
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringMvcConfig.class);
return ctx;
// return null;
}
//设置哪些请求归属于SpringMVC处理
@Override
protected String[] getServletMappings() {
//表示所有请求交给SpringMVC处理
return new String[]{"/"};
}
//加载Spring容器配置
@Override
protected WebApplicationContext createRootApplicationContext() {
return null;
}
}
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
80
/
- 添加运行配置,添加运行:tomcat7:run以及修改工作目录
相关知识点复习
- 名称:@Controller
- 类型:类注解
- 位置:SpringMVC控制器类定义上方
- 作用:设定SpringMVC的核心控制器bean
- 范例
@Controller
public class UserController{
}
- 名称:@ResponseBody
- 类型:方法注解
- 位置:SpringMVC控制器方法定义上方
- 作用:设置当前控制器方法相应内容为当前返回值,无需解析
- 范例:
@RequestMapping("/save")
@ResponseBody
public String save(){
System.out.println("user save ... ");
return "{'info':'springmvc'}";
}
入门案例工作流程
- 启动服务器初始化过程
- 1.服务器启动,执行ServletContainerInitConfig类,初始化web容器
- 2.执行createServletApplicationContext方法,创建了WebApplicationContext对象
- 3.加载SpringMvcConfig
- 4.执行@ComponentScan加载对应的bean
- 5.加载UserController,每个@RequestMapping的名称对应一个具体的方法
- 6.执行getServletMapping方法,定义所有的请求都通过SpringMVC
SpringMVC-bean加载控制
- 名称:@ComponentScan
- 类型:类注解
- 范例:
@Configuration
//1.指定特殊包
//@ComponentScan({"com.itheima.service","com.itheima.dao"})
//2.全局扫描,排除部分
//表示按照注解排除Controller
@ComponentScan(value = "com.itheima"
, excludeFilters = @ComponentScan.Filter(
type = FilterType.ANNOTATION,
classes = Controller.class
)
)
public class SpringConfig {
}
- 属性
- excludeFilters:排除扫描路径中加载的bean,需要指定到类别(type)与具体项(classess)
- includeFilters:加载指定的bean,需要指定到类别(type)与具体项(classess)
Controller加载控制与业务bean加载控制
public class ServletContainerInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};
}
@Override
protected Class>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
// // 加载springMVC容器配置
// @Override
// protected WebApplicationContext createServletApplicationContext() {
// AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
// ctx.register(SpringMvcConfig.class);
// return ctx;
// }
//
// //设置哪些请求归属于SpringMVC处理
// @Override
// protected String[] getServletMappings() {
// //表示所有请求交给SpringMVC处理
// return new String[]{"/"};
// }
//
// //加载Spring容器配置
// @Override
// protected WebApplicationContext createRootApplicationContext() {
// AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
// ctx.register(SpringConfig.class);
// return ctx;
//
// }
}