1.mcv框架要做哪些事情
(a)将url映射到java类或者Java类的方法
(b)封装用户提交的数据
(c)处理请求---调用相关的业务处理,封装响应的数据
(d)将封装的数据进行渲染,jsp,html等
2.SpringMvc是一个轻量级的基于请求响应的mvc框架
3.为什么要学习SpringMvc
性能比较好
简单、易学
与Spring无缝结合(使用Spring 的 IOC,AOP)
能够进行简单Junit测试
支持Restful风格
异常处理
本地化、国际化
数据验证、类型转换等
拦截器
等等
-------使用的人和公司多最主要
4.简单了解结构

5.做一个简单的SpringMvc 例子 (以下例子为 annotation例子)
(a)导入相关jar包
commons-logging-1.1.3.jar
spring-aop-4.2.2.RELEASE.jar (注解的时候需要)
spring-beans-4.2.2.RELEASE.jar
spring-context-4.2.2.RELEASE.jar
spring-context-support-4.2.2.RELEASE.jar
spring-core-4.2.2.RELEASE.jar
spring-expression-4.2.2.RELEASE.jar
spring-web-4.2.2.RELEASE.jar
spring-webmvc-4.2.2.RELEASE.jar
(b)配置分发器
<servlet>
<servlet-name>SpringMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
(c)添加SpringMvc配置文件 默认在web-inf下添加mvc.xml

(d)编写controller
package com.spring.hello;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.stereotype.Controller;
@Controller
public class HelloController {
@RequestMapping("/hello")
public ModelAndView hello(HttpServletRequest req,HttpServletResponse resp)
{
ModelAndView mv=new ModelAndView();
mv.addObject("msg", "第一个annotation");
mv.setViewName("Hello");
return mv;
}
}
(e)编写SpringMvc文件即 mvc.xml文件
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置渲染器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<!--配置结果视图的前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--配置结果视图的后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
<!-- 配置请求和处理器 -->
<context:component-scan base-package="com.spring.hello"></context:component-scan>
</beans>