SpringMVC系列之基本配置
一、概述
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系列之基本配置的更多相关文章
- springmvc系列一 之配置介绍(包含官网doc)
1.springmvc 官网参考地址: https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html 2 ...
- (转)springMVC+mybatis+ehcache详细配置
一. Mybatis+Ehcache配置 为了提高MyBatis的性能,有时候我们需要加入缓存支持,目前用的比较多的缓存莫过于ehcache缓存了,ehcache性能强大,而且位各种应用都提供了解决方 ...
- 【SpringMVC】SpringMVC系列11之Restful的CRUD
11.Restful的CRUD 11.1.需求 11.2.POST转化为PUT.DELETE的fileter 11.3.查询所有 11.4.添加 11.5.删除 优雅的 REST 风格的资 ...
- 【SpringMVC】SpringMVC系列10之视图与视图解析器
10.视图与视图解析器 10.1.概述 请求处理方法执行完成后,最终返回一个 ModelAndView处理方法,Spring MVC 也会在内部将它们装配成一个ModelAndView 对象, ...
- ANDROID Porting系列二、配置一个新产品
ANDROID Porting系列二.配置一个新产品 详细说明 下面的步骤描述了如何配置新的移动设备和产品的makefile运行android. 1. 目录//vendor/创建一个公 ...
- Spring Web MVC中的页面缓存支持 ——跟我学SpringMVC系列
Spring Web MVC中的页面缓存支持 ——跟我学SpringMVC系列
- JavaEE开发之SpringMVC中的路由配置及参数传递详解
在之前我们使用Swift的Perfect框架来开发服务端程序时,聊到了Perfect中的路由配置.而在SpringMVC中的路由配置与其也是大同小异的.说到路由,其实就是将URL映射到Java的具体类 ...
- Spring-MVC开发步骤(入门配置)
Spring-MVC开发步骤(入门配置) Step1.导包 spring-webmvc Step2.添加spring配置文件 Step3.配置DispatcherServlet 在web.xml中: ...
- Robotframework-Appium系列:安装配置
1. Robotframework-android系列:安装配置 1.1. 安装环境 64位win10家庭中文版 1.1. 安装说明 网上robotframework-appium安装资料也不少, ...
随机推荐
- [iOS] 使用xib做为应用程序入口 with Code
[iOS] 使用xib做为应用程序入口 with Code 前言 开发iOS APP的时候,使用storyboard能够快速并且直觉的建立用户界面.但在多人团队开发的情景中,因为storyboard是 ...
- django使用笔记
django的具体使用可以看官方手册http://djangobook.py3k.cn,这里主要记录使用django中遇到的问题. 1.中文编码问题. 因为我们用到的东西基本上都有中文,在settin ...
- javascript函数中的三个技巧【二】
技巧二: [惰性载入函数] 因为浏览器之间的行为的差异,我们经常会在函数中包含了大量的if语句,以检查浏览器特性,解决不同浏览器的兼容问题,比如,我们最常见的为dom节点添加时间的函数 functio ...
- 解决SharePoint文档库文件在搜索结果页面显示的标题和文档的标题不一致问题(search result)
问题表现: SharePoint 2013 爬网后,搜索一个文档,虽然搜到了,但是显示有点问题,如图: 原因分析: 造成该问题的原因是,该文档除了本身有一个名称外,在文档metadata的title属 ...
- IO流-输入输出
java的I/O技术可以将数据保存到文本.二进制.ZIP压缩文件中,下面来说说一些基本的常识(今天只讲理论).先来说说流,何为流?“流就是一组有序的数据序列,根据操作的类型,可以分为输入(Input) ...
- IOS沙盒
可以先在程序打印沙盒路径: NSLog(@"路径%@",NSHomeDirectory()); ------------------------------------------ ...
- 初始block,关于定义的几个小题目
block的定义和C语言指针函数非常相似,就可以照着指针函数的方法去依葫芦画瓢就可以了 block中的^只是用来表示这是一个block对象,和函数指针中的*作用一样,只是一个标识符 下面有三个小例子来 ...
- MyBatis入门(一)---基本使用
一.MyBatis简介 1.1.概述 MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架. MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集. M ...
- CoreAnimation-03-隐式动画
简介 每个UI控件,默认自动创建一个图层(根图层),即每个UI控件对应于至少一个图层 可以手动创建图层,这些图层为非根图层 对非根图层的某些属性(标记为Animatable的属性)进行修改,默认会自动 ...
- iOS之地理位置及定位系统 -- 入门笔记(用Swift)
前言:关于地理位置及定位系统,在iOS开发中也比较常见,比如美团外面的餐饮店铺的搜索,它首先需要用户当前手机的位置,然后在这个位置附近搜索相关的餐饮店铺的位置,并提供相关的餐饮信息,再比如最常见的就是 ...