微信公众号:compassblog

欢迎关注、转发,互相学习,共同进步!

有任何问题,请后台留言联系!

1、SpringMVC 概述

(1)、 MVC:Model-View-Control

Control 层属于框架性质,完成的主要工作是:封装 web 请求为一个数据对象、调用业务逻辑层来处理数据对象、返回处理数据结果及相应的视图给客户端。

(2)、什么是 SpringMVC

Spring mvc 和 Struts2 都属于表现层的框架,是 Spring 框架的一部分,Spring 框架的 Control 层的核心是 DispatcherServlet,它的作用是将请求分发给不同的后端处理器。

Spring 的 Control 层框架使用了后端控制器来映射处理器和视图解析器来共同完成 Control 层框架的主要工作。并且 spring 的 Control 层框架还真正地把业务层处理的数据结果和相应的视图拼成一个对象,即 ModelAndView 对象。

SpringMVC 本身就是  Spring 的子项目,对 Spring 兼容性很好,因此也不需要做过多的配置。

(3)SpringMVC 框架处理流程

2、SpringMVC 入门实例

(1)、创建 web  项目,导入所需要的 jar 包,如图下图所示:

(2)、创建 SpringMVC 的核心配置文件,具体配置如下:

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:p="http://www.springframework.org/schema/p"
   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-4.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">        <!-- 扫描@Controler  @Service   -->
       <context:component-scan base-package="com.springmvc"/>        <!-- 处理器映射器 -->
<!--         <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> -->
       <!-- 处理器适配器 -->
<!--         <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> -->
       <!-- 注解驱动 -->
       <mvc:annotation-driven/>        <!-- 视图解释器 -->
       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
           <property name="prefix" value="/WEB-INF/jsp/"/>
           <property name="suffix" value=".jsp"/>
       </bean>   </beans>

(3)、配置 SpringMVC 的前端控制器 DispatcherServlet,具体配置如下:

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>SpringmvcProject</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>
   <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:springmvc.xml</param-value>
   </init-param>
 </servlet>
 <servlet-mapping>
   <servlet-name>springmvc</servlet-name>
   <url-pattern>*.action</url-pattern>
 </servlet-mapping>
</web-app>

(4)、在 /WEB-INF/ 目录下新建 jsp 目录,新建 jsp 文件,具体代码如下:

itemList.jsp

<%@ 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"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查询商品列表</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/item/queryitem.action" method="post">
<p></p>
商品列表:
<p></p>
<table width="100%" border=1>
<tr>
   <td>商品名称</td>
   <td>商品价格</td>
   <td>生产日期</td>
   <td>商品描述</td>
</tr>
<c:forEach items="${itemList }" var="item">
<tr>
   <td>${item.name }</td>
   <td>${item.price }</td>
   <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
   <td>${item.detail }</td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>

(5)、创建 POJO 类,具体代码如下:

Items.java

package com.springmvc.pojo;

import java.util.Date;

public class Items {

    public Items(Integer id, String name, Float price, Date createtime, String detail) {
       super();
       this.id = id;
       this.name = name;
       this.price = price;
       this.createtime = createtime;
       this.detail = detail;
   }    private Integer id;    private String name;    private Float price;    private String pic;    private Date createtime;    private String detail;    public Integer getId() {
       return id;
   }    public void setId(Integer id) {
       this.id = id;
   }    public String getName() {
       return name;
   }    public void setName(String name) {
       this.name = name == null ? null : name.trim();
   }    public Float getPrice() {
       return price;
   }    public void setPrice(Float price) {
       this.price = price;
   }    public String getPic() {
       return pic;
   }    public void setPic(String pic) {
       this.pic = pic == null ? null : pic.trim();
   }    public Date getCreatetime() {
       return createtime;
   }    public void setCreatetime(Date createtime) {
       this.createtime = createtime;
   }    public String getDetail() {
       return detail;
   }    public void setDetail(String detail) {
       this.detail = detail == null ? null : detail.trim();
   }
}

(6)、创建 Controller 类,具体代码如下:

ItemController.java

package com.springmvc.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import com.springmvc.pojo.Items; /**
* 商品管理
*
* @author lx
*
*/
@Controller
public class ItemController {    @RequestMapping(value = "/itemlist.action")
   public ModelAndView itemList(){        // 创建页面需要显示的商品数据
       List<Items> list = new ArrayList<Items>();
       list.add(new Items(1, "1华为 荣耀8", 2399f, new Date(), "质量好!1"));
       list.add(new Items(2, "2华为 荣耀8", 2399f, new Date(), "质量好!2"));
       list.add(new Items(3, "3华为 荣耀8", 2399f, new Date(), "质量好!3"));
       list.add(new Items(4, "4华为 荣耀8", 2399f, new Date(), "质量好!4"));
       list.add(new Items(5, "5华为 荣耀8", 2399f, new Date(), "质量好!5"));
       list.add(new Items(6, "6华为 荣耀8", 2399f, new Date(), "质量好!6"));        ModelAndView mav = new ModelAndView();
       //数据
       mav.addObject("itemList", list);
       mav.setViewName("itemList");
       return mav;
   } }

(7)、启动 Tomcat,并到浏览器地址栏测试,结果如下:

3、SpringMVC 框架访问流程图

 

注:部分知识源于网络,侵权联删。

 

关注微信公众号compassblog,后台回复 “SpringMVC系列一” 获取本项目源码

 

推荐阅读:

 

本系列后期仍会持续更新,欢迎关注!

如果你认为这篇文章有用,欢迎转发分享给你的好友!

本号文章可以任意转载,转载请注明出处!

扫描关注微信公众号,了解更多

SpringMVC 框架系列之初识与入门实例的更多相关文章

  1. SpringMVC 框架系列之组件概述与配置详解

    在上一篇文章 SpringMVC 框架系列之初识与入门实例 的实例中,我们已经知道,SpringMVC 框架是一个 web 层的框架,本篇文章就详细解释一下 SpringMVC 框架具体文件的配置以及 ...

  2. SpringMVC详解(一)------入门实例

    本系列教程我们将详细的对SpringMVC进行介绍,相信你在学完本系列教程后,一定能在实际开发中运用自如. 1.什么是 SpringMVC ? 在介绍什么是 SpringMVC 之前,我们先看看 Sp ...

  3. 转载 SpringMVC详解(一)------入门实例

    目录 1.什么是 SpringMVC ? 2.创建 web 工程,并导入相应的 jar 包. 3.新建 SpringMVC 全局配置文件 4.在 web.xml 文件中配置前端过滤器 5.编写处理器  ...

  4. 【SSH系列】初识spring+入门demo

    学习过了hibernate,也就是冬天,经过一个冬天的冬眠,当春风吹绿大地,万物复苏,我们迎来了spring,在前面的一系列博文中,小编介绍hibernate的相关知识,接下来的博文中,小编将继续介绍 ...

  5. JQuery实战---初识JQuery+入门实例

    JQuery在小编的世界中,也就是JavaScript和查询(Query),即是辅助JavaScript开发的库,百度百科对JQuery的介绍比较详细,小伙伴可以东东自己可耐的小爪子,上网进行搜索,说 ...

  6. scrapy框架系列 (1) 初识scrapy

    Scrapy 框架 Scrapy是用纯Python实现一个为了爬取网站数据.提取结构性数据而编写的应用框架,用途非常广泛. 框架的力量,用户只需要定制开发几个模块就可以轻松的实现一个爬虫,用来抓取网页 ...

  7. SpringMVC系列之(一) 入门实例

    Spring MVC是非常优秀的MVC框架,由其是在3.0版本发布后,现在有越来越多的团队选择了Spring3 MVC了.Spring MVC结构简单,应了那句话简单就是美,而且他强大不失灵活,性能也 ...

  8. MyBatis 框架系列之基础初识

    MyBatis 框架系列之基础初识 1.什么是 MyBatis MyBatis 本是 apache 的一个开源项目 iBatis,后改名为 MyBatis,它 是一个优秀的持久层框架,对 jdbc 的 ...

  9. 项目搭建系列之二:SpringMVC框架下配置MyBatis

    1.什么是MyBatis? MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis ...

随机推荐

  1. Windows Azure Virtual Machine (34) Azure VM挂载WebDAV

    <Windows Azure Platform 系列文章目录> 之前使用Azure VM,挂载box网盘.发现不能正常挂载,这里简单记录一下. 1.WebDAV的网络映射,需要WebCli ...

  2. Material使用11 核心模块和共享模块、 如何使用@angular/material

    1 创建项目 1.1 版本说明 1.2 创建模块 1.2.1 核心模块 该模块只加载一次,主要存放一些核心的组件及服务 ng g m core 1.2.1.1 创建一些核心组件 页眉组件:header ...

  3. javascript 回到顶部 动画效果

    上代码: <!DOCTYPE html> <html> <head> <meta content="测试demo" name=" ...

  4. 阿里云学习之IOT物联网套件(配置篇)

    文档时间:2018.-1-24 首注:此文章是参照以下文章的整合与补充: https://bbs.aliyun.com/read/309106.html?amp;displayMode=1&p ...

  5. js压缩上传图片

    初学有不当之处,请多多指点, <body> <div class="cc"> <input type="file" id=&quo ...

  6. 观察者模式—jdk自带源码分析

    一:观察者模式简介 二:jdk实现观察者模式的源码 三:实际例子 四:观察者模式的优点和不足 五:总结 一:观察者模式简介 有时又被称为发布(publish )-订阅(Subscribe)模式.模型- ...

  7. Ios App上传步骤

    前言:作为一名IOS开发者,把开发出来的App上传到App Store是必须的.下面就来详细介绍下具体流程. 1.打开苹果开发者中心:https://developer.apple.com 打开后点击 ...

  8. ubuntu命令行下java工程编辑与算法(第四版)环境配置

    ubuntu命令行下java工程编辑与算法(第四版)环境配置 java 命令行 javac java 在学习算法(第四版)中的实例时,因需要安装配套的java编译环境,可是在编译java文件的时候总是 ...

  9. Nginx和php是怎么通信的?

    先来看一下搭建好PHP运行环境的Nginx配置文件. 非常重要的就是 fastcgi_pass 指令了,这个指令用于指定 fpm 进程监听的地址,Nginx 会把所有的 php 请求翻译成 fastc ...

  10. dubbox系列【二】——dubbox admin环境搭建

    1.环境 JDK7+ zookeeper 3.3.6 tomcat 7+ 2.搭建步骤 (1)安装zookeeper 单机模式:参考这里. 伪集群模式:参考这里. ①单机或伪集群模式均可. ②笔者使用 ...