1.简介
SpringMVC是一个基于Spring开发的MVC轻量级框架,Spring3.0后发布的组件,SpringMVC和Spring可以无 缝整合,使用DispatcherServlet作为前端控制器,且内部提供了处理器映射器、处理器适配器、视图解析器等组 件,可以简化JavaBean封装,Json转化、文件上传等操作
2.搭建环境入门
1.导入Spring整合SpringMVC的坐标
spring-webmvc javax.servlet-api
2.在web.xml中配置SpringMVC的前端控制器ServletDispatche
<web-app>//DispatcherServlet 是一个Servlet servlet映射地址配置 <servlet><servlet-name>DispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>//指定springMVC配置文件位置<init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-mvc.xml</param-value></init-param> //服务器启动就创建<load-on-startup>1</load-on-startup> </servlet><servlet-mapping><servlet-name>DispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!--配置spring--> //在web.xml中配置ContextLoaderListener<context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring.xml</param-value></context-param> <listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
3.创建springMVC的核心配置文件 spring-mvc.xml,并配置组件扫描web层
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd "> <context:component-scan base-package="com.hu"></context:component-scan></beans>
4.编写一个控制器Controller,配置映射信息
@Controller public class userController {@RequestMapping("/show")public String show(){System.out.println(" this test is new spring mvc");return "/aa.jsp";} }
3.SpringMVC关键组件浅析