SSM-SpringMVC-MVC设计概述
MVC设计概述
在许多应用中,前端,PHP,.NET等语言都应用MVC设计,根本原因是在于解耦各个模块。
Spring MVC的架构
Spring MVC最大的特点就是结构松散,几乎可以在SpringMVC中使用各类视图,如JSON,JSP,XML等,能满足手机端,页面段,平板电脑端的各个请求,
SpringMVC组件与流程
核心在于其流程,它是基于Service的技术,提供核心控制器DispatcherServlet和相关组件,并制定了松散的结构,以及各种灵活的需求
上面给出了SpringMVC的服务流程以及各个组件运行顺序,是SpringMVC的核心,流程中的大部分组件并不需要我去实现 ,只需要熟悉使用就可以构造强大的互联网系统。
SpringMVC入门的实例
- 首先配置WEB工程的web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--配置SpringIoC配置文件路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applictionContext.xml</param-value>
</context-param>
<!--配置contextConfigLocation用以初始化SpringIOC容器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置DispatcherServlet-->
<servlet>
<!--SpringMVC会根据servlet-name配置,找到/WEB/dispatcher-servlet.xml-->
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--使得Dispatcher在服务器启动时候初始化-->
<load-on-startup>2</load-on-startup>
</servlet>
<!--servlet拦截配置-->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
分析:
- 系统变量contextConfigLocation的配置:告诉SpringMVC其SpringIoC的配置文件在哪,如果是多个配置文件,使用逗号分隔开,且还支持正则表达式,进行模糊匹配。
- ContextLoaderListener实现了接口ServletConextListener,ServletConextListener的作用在整个Web工程前后加入自定义代码,可以再Web工程初始化之前,它先完成对SpringIoC容器的初始化,也可以在Web工程关闭之时完成SpringIOC容器的资源释放
- 配置Dispatcher:首先配置servlet-name为dispatcher,意味着一个/WEB-INF/dispatcher-servlet.xml文件,与之对应,并且配置了服务器启动期间就初始化它
- 配置dispatcher拦截以后缀 do 结束的请求,所有的do结尾的请求都会被拦截
-
applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> </beans>
-
dispatcher-servlet.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--使用注解驱动--> <mvc:annotation-driven/> <!--定义扫描装载的包--> <context:component-scan base-package="com"/> <!--定义视图解析器--> <!--找到web工程/WEB-INF/JSP文件夹,且文件结尾为jsp的文件映射--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INf/JSP" p:suffix=".jsp"/> <!--如果有配置数据库事务,需要开启注解事务的,需要开启这段代码--> <!-- <tx:annotation-driven transaction-manager="transactionManager"/>--> </beans>
- < mvc:annotation-driven >:使用注解驱动
- 定义一个个扫描的包,用它来扫描对应的包,用以加载对应的控制器和其他的一些组件
- 定义视图解析器,解析器中定义了前缀和后缀,这样视图就知道去 Web 工程的/WEB-INf/JSP文件夹找到对应的JSP文件作为相应视图响应用户请求
-
控制器配置了注解驱动,并且定义了扫描的包,开发一个简单的Controller:
package com.ssm.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
//注解@Controller表示他是一个控制器
@Controller("myController")
//表明当请求的url在/my下的时候才有该控制器响应
@RequestMapping("/my")
public class MyController {
//表明URI是/index的时候该方法才请求
@RequestMapping("/index")
public ModelAndView index(){
//模型和视图
ModelAndView mv=new ModelAndView();
//视图逻辑名称为index
mv.setViewName("index");
//返回模型和视图
return mv;
}
}
- 注解@Controller是一个控制器,SpringMVC扫描的时候会把它作为控制器加载进来,
- @RequestMapping:指定了对应的请求URI,SpringMVC初始化的时候将这些信息解析存放
- index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>Hello,Spring</h1>
</body>
</html>
- 启动tomcat服务器,输入对应的地址:http://localhost:8080/xxx/my/index.do 可以查看效果
- 运行流程: