一、概述

  Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱动指的就是使用请求-响应模型。

  1、什么是MVC?

  模型-视图-控制器(MVC)是一个以设计界面应用程序为基础的设计模式。它主要通过分离模型、视图及控制器在应用程序中的角色将业务逻辑从界面中解耦。通常,模型负责封装应用程序数据在视图层展示。视图仅仅只是展示这些数据,不包含任何业务逻辑。控制器负责接收来自用户的请求,并调用后台服务来处理业务逻辑。处理后,后台业务层可能会返回了一些数据在视图层展示。控制器收集这些数据及准备模型在视图层展示。MVC模式的核心思想是将业务逻辑从界面中分离出来,允许它们单独改变而不会相互影响。

  

  在Spring MVC中,模型通常由POJO对象组成,它在业务层中被处理,在持久层被持久化,视图通常是用JSP标准标签库(JSTL)编写的JSP模板,控制器部分是由dispatcher servlet负责。

2、Spring MVC架构

  SpringMVC是一个基于请求驱动的Web框架,使用前端控制器模式来进行设计,在根据映射规则分发给相应的页面控制器进行处理。其请求处理流程如下图所示:

  

  具体执行步骤如下:

  1、客户端发出一个HTTP请求,Web应用服务器接收到这个请求,如果匹配DispatcherServlet的请求映射路径(web.xml中指定),Wen容器就会将该请求转交给DispatcherServlet处理。

  2、DispatcherServlet接收到这个请求后,将根据请求的信息和HandlerMapping的配置找到处理请求的处理器(Handler)。

  3、得到Handler后,通过HandlerAdapter对Handler进行封装,再以统一的适配器接口调用Handler。

  4、处理器完成业务逻辑的处理后返回一个ModelAndView给DispatcherServlet,ModelAndView包含了视图逻辑名和模型数据信息。

  5、DispatcherServlet借由ViewResolver完成逻辑视图名到真实视图对象的解析工作。

  6、当得到真实的视图对象view后,DispatcherServlet就使用这个View对象对ModelAndView中模型数据进行渲染。

  7、客户端最终得到的响应消息可能是一个普通的HTML页面,也可能是一个XML或者是JSON串。

二、基本配置(非注解)

  1、新建工程,导入构建SpringMVC工程所需的jar包

  

  2、配置前端控制器

  在web.xml中配置前端控制器:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>day_0301_springMVC</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation配置springmvc加载的配置文件(配置处理器映射器、适配器等等)
如果不配置contextConfigLocation,默认加载的是/WEB-INF/servlet名称-servlet.xml
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--
第一种:*.action,访问以.action结尾由DispatcherServlet进行解析
第二种:/, 所有访问的地址都由DispatcherServlet进行解析,对于静态文件的解析需要配置不让DispatcherServlet进行解析
-->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>

  3、配置处理器映射器

  在classpath下的springmvc.xml中配置处理器映射器

  

<!-- 处理器映射器  ,将bean的name作为URL进行查找,需要在配置Handler时指定beanName(就是URL)-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>

  4、配置处理器适配器

<!-- 处理器适配器,所有的处理器适配器都实现HandlerAdapter接口 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter">
</bean>

  5、配置视图解析器

<!-- 配置视图解析器
解析jsp视图,默认使用jstl标签
CLASSPATH下面要有jstl jar包
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>

  6、编写Handler

  需要实现Controller接口,才能由SimpleControllerHandlerAdapter适配器执行。

  先要创建POJO对象:

public class Items
{
private String name;
private int price;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getPrice()
{
return price;
}
public void setPrice(int price)
{
this.price = price;
}
}

  创建Handler:

public class TestController implements Controller
{
@Override
public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception
{
List<Items> itemsList=new ArrayList<Items>();
Items items1=new Items();
items1.setName("联想笔记本");
items1.setPrice(2500);
Items items2=new Items();
items2.setName("三星笔记本");
items2.setPrice(5000);
itemsList.add(items1);
itemsList.add(items2);
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("itemsList", itemsList);
modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
return modelAndView;
}
}

  7、编写视图jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>查询商品列表</title>
</head>
<body>
<form action="${pageContext.request.contentLength}/item/queryItem.action" method="post">
查询条件:
<table width="100%" border="1">
<tr>
<td><input type="submit" value="查询"/></td>
</tr>
</table>
商品列表:
<table width="100%" border="1">
<tr>
<td>商品名称</td>
<td>商品价格</td>
<td>操作</td>
</tr>
<c:forEach items="${itemsList}" var="item">
<tr>
<td>${item.name }</td>
<td>${item.price }</td>
<td><a href="${pageContext.request.contextPath}/item/editItem.action?name=${item.name}">修改</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>

  8、配置Handler

<bean name="/queryItems.action" class="com.demo.ssm.controller.TestController"></bean>

  到此,springmvc.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
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-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"> <bean name="/queryItems.action" class="com.demo.ssm.controller.TestController"></bean> <!-- 处理器适配器,所有的处理器适配器都实现HandlerAdapter接口 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter">
</bean> <!-- 处理器映射器 ,将bean的name作为URL进行查找,需要在配置Handler时指定beanName(就是URL)-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <!-- 配置视图解析器
解析jsp视图,默认使用jstl标签
CLASSPATH下面要有jstl jar包
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
</beans>

  9、部署调式

  访问的地址:http://localhost:8080/day_0301_springMVC/queryItems.action

  结果如下图所示:

三、基于注解的映射器和适配器配置

  再基于注解的映射器和适配器配置中,注解Handler的编写如下:

@Controller
public class TestController2
{
@RequestMapping("/queryItemsTest")
public ModelAndView queryItems()
{
List<Items> itemsList=new ArrayList<Items>();
Items items1=new Items();
items1.setName("联想笔记本");
items1.setPrice(2500);
Items items2=new Items();
items2.setName("apple");
items2.setPrice(5000);
itemsList.add(items1);
itemsList.add(items2);
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("itemsList", itemsList);
modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
return modelAndView;
}
}

  springmv.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
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-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<bean class="com.demo.ssm.controller.TestController2"></bean>
<!-- 注解映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!-- 注解适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!--使用下面的mvc:annotation-driven可以代替上面的注解映射器和注解适配器-->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- <context:component-scan base-package="com.demo.ssm.controller"></context:component-scan> -->
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
</beans>

  部署后,访问地址:http://localhost:8080/day_0301_springMVC/queryItemsTest.action,运行结果同上。

四、工程源代码

  点击工程源代码下载链接

SpringMVC系列之基本配置的更多相关文章

  1. springmvc系列一 之配置介绍(包含官网doc)

    1.springmvc 官网参考地址: https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html 2 ...

  2. (转)springMVC+mybatis+ehcache详细配置

    一. Mybatis+Ehcache配置 为了提高MyBatis的性能,有时候我们需要加入缓存支持,目前用的比较多的缓存莫过于ehcache缓存了,ehcache性能强大,而且位各种应用都提供了解决方 ...

  3. 【SpringMVC】SpringMVC系列11之Restful的CRUD

      11.Restful的CRUD 11.1.需求 11.2.POST转化为PUT.DELETE的fileter 11.3.查询所有 11.4.添加 11.5.删除     优雅的 REST 风格的资 ...

  4. 【SpringMVC】SpringMVC系列10之视图与视图解析器

    10.视图与视图解析器 10.1.概述     请求处理方法执行完成后,最终返回一个 ModelAndView处理方法,Spring MVC 也会在内部将它们装配成一个ModelAndView 对象, ...

  5. ANDROID Porting系列二、配置一个新产品

    ANDROID Porting系列二.配置一个新产品 详细说明 下面的步骤描述了如何配置新的移动设备和产品的makefile运行android. 1.         目录//vendor/创建一个公 ...

  6. Spring Web MVC中的页面缓存支持 ——跟我学SpringMVC系列

    Spring Web MVC中的页面缓存支持 ——跟我学SpringMVC系列

  7. JavaEE开发之SpringMVC中的路由配置及参数传递详解

    在之前我们使用Swift的Perfect框架来开发服务端程序时,聊到了Perfect中的路由配置.而在SpringMVC中的路由配置与其也是大同小异的.说到路由,其实就是将URL映射到Java的具体类 ...

  8. Spring-MVC开发步骤(入门配置)

    Spring-MVC开发步骤(入门配置) Step1.导包 spring-webmvc Step2.添加spring配置文件 Step3.配置DispatcherServlet 在web.xml中: ...

  9. Robotframework-Appium系列:安装配置

    1.   Robotframework-android系列:安装配置 1.1. 安装环境 64位win10家庭中文版 1.1. 安装说明 网上robotframework-appium安装资料也不少, ...

随机推荐

  1. jQuery实现图片伦播效果(淡入淡出+左右切换)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. C++之多态的一个例子

    [例12.1] 先建立一个Point(点)类,包含数据成员x,y(坐标点).以它为基类,派生出一个Circle(圆)类,增加数据成员r(半径),再以Circle类为直接基类,派生出一个Cylinder ...

  3. 聊聊GISer的职业发展

    一.前言 去年写了一篇名为<GISer们还有机会屌丝逆袭吗?>的博文,希望能和广大GISer一起探讨地理信息产业留给小团队和个人的机会.文章发布后,很多GISer通过网络和我进行了交流,其 ...

  4. Setting up your App domain for SharePoint 2013

    from:http://sharepointchick.com/archive/2012/07/29/setting-up-your-app-domain-for-sharepoint-2013.as ...

  5. Silverlight项目笔记2:.svc处理程序映射缺失导致的WCF RIA Services异常

    在确定代码.编译结果和数据库都正常的情况下,无法从数据库取到数据.错误提示:Sysyem.Net.WebException:远程服务器返回了错误:NotFound,监听发现请求数据库的服务异常,访问相 ...

  6. iOSQuartz2D-02-绘制炫酷的下载进度条

    效果图 实现思路 要实现绘图,通常需要自定义一个UIView的子类,重写父类的- (void)drawRect:(CGRect)rect方法,在该方法中实现绘图操作 若想显示下载进度,只需要实例化自定 ...

  7. android 之 animations 动画

    android 提供的了两种机制你可以用来创建简单的动画:tweedned animation(渐变动画) 和 frame-by-frame animation(逐帧动画)(有道翻译的,汗!!!) . ...

  8. Win10 下使用 ionic 框架开发 android 应用之搭载开发环境

    转载请注明出处:http://www.cnblogs.com/titibili/p/5102035.html 谢谢~ 1.下载JDK并配置Java运行环境 http://www.oracle.com/ ...

  9. Android地图开发之地图的选择

    做lbs开发差不多快2年了,地图相关的产品也差不多做了3个了,用到过的地图包括google地图.高德地图.百度地图.图吧.Osmdroid,今天总结下,方便大家开发时选择合适的地图. 首先说定位模块选 ...

  10. Windows下查看端口占用

    最近在重新安装Mysql的时候,发现3306默认端口被占用了.类似的情况常常遇到,想查看到底是哪个程序把这个端口占用了. 下面是我google找到的方法,和大家分享. 1. 首先,使用netstat ...