springmvc是spring框架的一个模块,springmvc和spring无需通过中间整合层进行整合。
springmvc是一个基于mvc的web框架。
Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面
DispatcherServlet前端控制器,接收用户请求和响应
是springmvc工作流程的中心,它的存在存在降低了组件之间的耦合性
主要负责调度和控制流程
HandlerMapping 是负责根据 request 请求找到对应的 Handler 处理器及 Interceptor 拦截器,将它们封装在 HandlerExecutionChain 对象中返回给前端控制器
根据请求的 url 与 Spring 容器中定义的 bean 的 name 进行匹配
Url 地址就是处理器 Bean 的名字
配置如下:
<bean class="cn.itcast.ssm.controller.ItemController"
name="/ItemController2">
bean>
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="beanName" value="/ItemController2"/>
bean>
是 BeanNameUrlHandlerMapping 的增强版本,它可以将 url 和处理器 bean 的 id 进行统一映射配置
配置中如下:
<bean class="cn.itcast.ssm.controller.ItemController"
id="ItemController">
bean>
<bean
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.do">ItemControllerprop>
props>
property>
bean>
简单控制器处理适配器,不是通过springmvc注解开发的接口,HandlerAdapter必须是SimpleControllerHandlerAdapter
通过 HandlerAdapter 对处理器进行执行,这是适配器模式的应用
编写的Handler实现 Controller接口
<bean
class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter">
bean>
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>
Handler也叫做Controller
是继 DispatcherServlet 前端控制器的后端控制器
在DispatcherServlet 的控制下对用户的请求进行处理,
需要程序员根据业务进行开发
负责将处理结果生成 View 视图
ViewResolver 首先根据逻辑视图名解析成物理视图名即具体的页面地址
再生成 View 视图对象,最后对 View 进行渲染将处理结果通过页面展示给用户
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
bean>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>cn.companygroupId>
<artifactId>springdemoartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>warpackaging>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.2.4.RELEASEversion>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>4.0.1version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>javax.servlet.jsp-apiartifactId>
<version>2.3.3version>
<scope>providedscope>
dependency>
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.2version>
<configuration>
<port>8080port>
<hostName>localhosthostName>
<uriEncoding>UTF-8uriEncoding>
configuration>
plugin>
plugins>
build>
project>
在web.xml中配置
<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"
version="2.5">
<display-name>springdemodisplay-name>
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring-mvc.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>*.dourl-pattern>
servlet-mapping>
web-app>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">
<bean
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.do">ItemControllerprop>
props>
property>
bean>
<bean
class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter">
bean>
<bean class="cn.itcast.ssm.controller.ItemController"
id="ItemController">
bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
bean>
beans>
实体类参照
传送门
package cn.itcast.ssm.controller;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import cn.itcast.ssm.po.Items;
/**
*
* @author 86182
*
*/
public class ItemController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
// 调用service查找 数据库,查询商品列表,这里使用静态数据模拟
List<Items> items = new ArrayList<Items>();
// 向list中填充静态数据
Items items_1 = new Items();
items_1.setName("联想笔记本");
items_1.setPrice(6000f);
items_1.setDetail("ThinkPad T430联想笔记本电脑!");
Items items_2 = new Items();
items_2.setName("苹果手机");
items_2.setPrice(5000f);
items_2.setDetail("iphone6苹果手机!");
items.add(items_1);
items.add(items_2);
/*
* ModelAndView有两个常用的构造器
* ModelAndView(String viewName)
* ModelAndView(String viewName,Map data)
* 注:
* viewName:视图名
* data: 数据
*/
//返回ModelAndView
ModelAndView modelAndView=new ModelAndView();
//相当于request的setAttribute,在jsp页面中通过items获取数据
modelAndView.addObject("items",items);
//指定视图
modelAndView.setViewName("items/itemsList");
return modelAndView;
}
}
<%@ page 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"%>
<html>
<head>
<meta charset="UTF-8">
<title>查询商品列表title>
head>
<body>
<form action="${pageContext.request.contextPath }/item/queryItem.do"
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>
<td>商品描述td>
<td>操作td>
tr>
<c:forEach items="${items }" var="items">
<tr>
<td>${items.name }td>
<td>${items.price }td>
<td>
<fmt:formatDate
value="${items.createtime }" pattern="yyyy-MM-dd HH:mm:ss" />
td>
<td>${items.detail }td>
<td><a
href="${pageContext.request.contextPath }/item/editItem.do?id=${items.id}">修改a>
td>
tr>
c:forEach>
table>
form>
body>
html>
点击项目->右键鼠标,选择Run as ,选择Maven build,弹出如下窗口,在Goals输入框中输入tomcat7:run,然后点击Apply,再点击Run
控制台显示如下场景,项目启动成功!
<bean class="cn.itcast.ssm.controller.ItemController"
name="/hello2.do">
bean>
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="beanName" value="/hello2.do"/>
bean>
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>
package cn.itcast.ssm.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.handler.HandlerResolver;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import cn.itcast.ssm.po.Items;
/**
*
* @author 86182
*
*/
public class ItemController2 implements HttpRequestHandler {
public void handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 调用service查找数据库,查询商品列表,这里使用静态数据模拟
List<Items> items = new ArrayList<Items>();
// 向list中填充静态数据
Items items_1 = new Items();
items_1.setName("联想笔记本1");
items_1.setPrice(6000f);
items_1.setDetail("ThinkPad T430联想笔记本电脑!");
Items items_2 = new Items();
items_2.setName("苹果手机2");
items_2.setPrice(5000f);
items_2.setDetail("iphone6苹果手机!");
items.add(items_1);
items.add(items_2);
// 设置模型数据
request.setAttribute("items", items);
// 设置请求转发
request.getRequestDispatcher("items/itemsList").forward(request, response);
}
}
系统的访问路径为:http://localhost:8080/springdemo/hello2.do,系统运行效果图和
上面的项目运行成功视图是一样的