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安装资料也不少, ...
随机推荐
- C++之函数重载
函数重载定义: 如果同一作用域内的几个函数名字相同但形参列表不同; 重载与const形参: Record (Phone); = Record(const Phone); Record(Phone*) ...
- C++ 面向对象的三个特点--继承与封装(一)
面试的时候经常会有很多概念性的东西,许久不用都会很生疏,特意整理一下方便自己以后不记得了可以查看一下,也顺便帮助自己复习一下. 概念 继承是面向对象程序设计的一个重要特性,它允许在既有类的基础上创建新 ...
- DirectX 9 SDK安装后在vs2010里编译BaseClasses出错问题解决方法
打开你的dx的sdk安装目录,例如: D:/DX90SDK/Samples/C++/DirectShow/ 这里就有一个叫baseclasses的工程,为安全起见,请先备份此工程. 1,双击basec ...
- Android:TextView 自动滚动(跑马灯) (转)
Android:TextView 自动滚动(跑马灯) TextView实现文字滚动需要以下几个要点: 1.文字长度长于可显示范围:android:singleLine="true ...
- datepicker冲突
公司里的项目由于发展较快,很多东西都没有好好梳理一下,以至于有很多的潜在的问题. 最近就遇到了一个比较坑的问题.datepicker 有两个插件库中的datepicker插件比较有名.一个是jQuer ...
- Bootstrap-用ICheck插件给CheckBox换新装
直接来吧! 下面是添加上复选框以后的树形菜单效果: 这样看起来有种驴唇不对马嘴的感觉. 所以就要想办法给这些复选框添加1些样式,让全部界面看起来搭配1些. 通过查询得知,有个叫ICheck的第3方Bo ...
- Ettercap中间人攻击--介绍
前言 Ettercap有四种界面:Text,Curses,GTK2,Daemonize. -T 命令行界面,只显示字符.通常与配套的参数有-q(安静模式),加上该选项,则不会显示抓到的数据包 ...
- How-to: disable the web-security-check in Chrome for Mac
When I try to test one web app in coperate intranet, there is always some error like "Failed to ...
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q81-Q84)
Question 81You need to create a Web Part that creates a copy of the out-of-the-box Contribute permis ...
- linux中fork()函数详解(原创!!实例讲解) (转载)
一.fork入门知识 一个进程,包括代码.数据和分配给进程的资源.fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程, 也就是两个进程可以做完全相同的事,但如果初始参数或者传入的变量不 ...