一、SpringMVC概念
(一)概念
1.Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架
2.使用了MVC架构模式的思想,将web层进行职责解耦
3.基于请求驱动指的就是使用请求-响应模型
4.框架的目的就是帮助我们简化开发,
Spring Web MVC也是要简化我们日常Web开发的。
(二)优点
1.性能比struts2好
2.简单、便捷,易学
3.和spring无缝衔接【IOC,AOP】
4.使用约定优于配置
5.支持Restful
6.异常处理,国际化,数据验证,类型转换等
7.使用的人多,使用的公司多
二、XML文件配置SpringMVC(配置内容较多)
1、创建动态web项目,导入jar包
2.自定义一个控制层类,实现Controller接口
public class HelloController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
System.out.println("本方法被调用了");
ModelAndView mav = new ModelAndView();
mav.setViewName("/index.jsp");
return mav;
}
}
3.在src包下创建一个SpringMVC.xml文件,配置处理器映射器、处理器适配器和自定义的controller
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<!-- 配置处理器映射器 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!-- 配置Controller适配