spring MVC 简单实现及原理分析

一、spingMVC 简单实现

1. 创建一个动态网页项目(本例:springMVCdemo1) 采用2.5框架版本,需要的 jar 包如下

2. 在 web.xml 中配置核心控制器    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>springMVCdemo1</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>
      <!-- 指定spring.xml文件位置  默认找 /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>
      <!-- 
          1. /*  拦截所有   jsp  js png .css  真的全拦截   建议不使用
          2. *.action *.do 拦截以do action 结尾的请求     肯定能使用   ERP  
          3. /  拦截所有 (不包括jsp) (包含.js .png.css)  强烈建议使用     前台 面向消费者  www.jd.com/search   /对静态资源放行
       -->
      <url-pattern>*.action</url-pattern>
  </servlet-mapping>
  
  
</web-app>

3.  新建 config 文件夹与 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.dic"/>

        
</beans>

 4. 在 src 下 准备pojo类、controller类;在WEB-INF下建立文件夹jsp并建立文件 itemList.jsp

 1 package com.dic.pojo;
 2 
 3 import java.util.Date;
 4 
 5 public class Items {
 6     
 7     public Items(Integer id, String name, Float price, Date createtime, String detail) {
 8         super();
 9         this.id = id;
10         this.name = name;
11         this.price = price;
12         this.createtime = createtime;
13         this.detail = detail;
14     }
15 
16     private Integer id;
17 
18     private String name;
19 
20     private Float price;
21 
22     private String pic;
23 
24     private Date createtime;
25 
26     private String detail;
27 
28     public Integer getId() {
29         return id;
30     }
31 
32     public void setId(Integer id) {
33         this.id = id;
34     }
35 
36     public String getName() {
37         return name;
38     }
39 
40     public void setName(String name) {
41         this.name = name == null ? null : name.trim();
42     }
43 
44     public Float getPrice() {
45         return price;
46     }
47 
48     public void setPrice(Float price) {
49         this.price = price;
50     }
51 
52     public String getPic() {
53         return pic;
54     }
55 
56     public void setPic(String pic) {
57         this.pic = pic == null ? null : pic.trim();
58     }
59 
60     public Date getCreatetime() {
61         return createtime;
62     }
63 
64     public void setCreatetime(Date createtime) {
65         this.createtime = createtime;
66     }
67 
68     public String getDetail() {
69         return detail;
70     }
71 
72     public void setDetail(String detail) {
73         this.detail = detail == null ? null : detail.trim();
74     }
75 
76 }
Items.java
 1 package com.dic.controller;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Date;
 5 import java.util.List;
 6 
 7 import org.springframework.stereotype.Controller;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 import org.springframework.web.servlet.ModelAndView;
10 
11 import com.dic.pojo.Items;
12 
13 @Controller
14 public class ItemController {
15     
16     //指定路径的访问url .action 可写可不写
17     @RequestMapping(value = "/itemlist.action")
18     public ModelAndView itemList(){
19             
20     // 创建页面需要显示的商品数据
21     List<Items> list = new ArrayList<Items>();
22     list.add(new Items(1, "1华为 荣耀8", 2399f, new Date(), "质量好!1"));
23     list.add(new Items(2, "2华为 荣耀8", 2399f, new Date(), "质量好!2"));
24     list.add(new Items(3, "3华为 荣耀8", 2399f, new Date(), "质量好!3"));
25     list.add(new Items(4, "4华为 荣耀8", 2399f, new Date(), "质量好!4"));
26     list.add(new Items(5, "5华为 荣耀8", 2399f, new Date(), "质量好!5"));
27     list.add(new Items(6, "6华为 荣耀8", 2399f, new Date(), "质量好!6"));
28         
29     // 创建ModelAndView用来存放数据和视图
30     ModelAndView mav = new ModelAndView();
31     // 设置数据到模型中
32     mav.addObject("itemList", list);
33     // 设置视图jsp 需要设置视图的物理地址
34     mav.setViewName("/WEB-INF/jsp/itemList.jsp");
35     
36     return mav;
37     }
38 
39 }
ItemController.java
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 4 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 6 <html>
 7 <head>
 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 9 <title>查询商品列表</title>
10 </head>
11 <body> 
12 <form action="${pageContext.request.contextPath }/item/queryitem.action" method="post">
13 查询条件:
14 <table width="100%" border=1>
15 <tr>
16 <td><input type="submit" value="查询"/></td>
17 </tr>
18 </table>
19 商品列表:
20 <table width="100%" border=1>
21 <tr>
22     <td>商品名称</td>
23     <td>商品价格</td>
24     <td>生产日期</td>
25     <td>商品描述</td>
26     <td>操作</td>
27 </tr>
28 <c:forEach items="${itemList }" var="item">
29 <tr>
30     <td>${item.name }</td>
31     <td>${item.price }</td>
32     <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
33     <td>${item.detail }</td>
34     
35     <td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>
36 
37 </tr>
38 </c:forEach>
39 
40 </table>
41 </form>
42 </body>
43 
44 </html>
itemList.jsp

 5. 运行程序(Tomcat 7)。打开浏览器访问下列地址  出现商品列表即成功

    http://localhost:8080/springMVCdemo1/itemlist.action

 

二、springMVC 架构

1. 框架结构

2. 架构流程

        1. 用户发送请求到前端控制器DispatcherServlet

        2. DispatcherServlet 收到请求调用 HanderMapping 处理器映射器

        3. 处理器映射器根据请求 URL 找到具体的处理器,生成处理器对象及处理器拦截器(若有则生成)一并返回给 DispatcherServlet

        4. DispatcherServlet 通过 HandlerAdapter 处理器适配器调用处理器

        5. 执行处理器 Controller (也叫后端控制器)

        6. Controller 执行完成后返回 ModeAndView

        7. HandlerAdapter 将 controller 执行结果 ModeAndView 返回给 DispatcherServlet

        8.  DispatcherServlet 将 ModeAndView 传给 ViewReslover 视图解析器

        9. ViewReslover 解析后返回具体 View 

        10. DispatcherServlet 对 View 进行渲染视图(即将模型数据填充至视图中)

        11. DispatcherServlet 响应用户

3. springmvc 的三大组件:处理器映射器、处理器适配器、视图解析器

    需要用户开发的组件有:handler、view

 

 三、组件介绍

1. 组件扫描器

    使用 <context:component-scan> 自动扫描标记 @Controller 的控制器类

    用法:在 springmvc.xml 配置文件中配置:

<!-- 配置controller扫描包,多个包之间用,分隔 -->
<context:component-scan base-package="com.dic" />

2. 注解映射器和适配器

        1. 配置处理器映射器、配置处理器适配器

<!-- 配置处理器映射器 -->
<bean
    class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />


<!-- 配置处理器适配器 -->
<bean
    class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />

 

         2. 注册驱动  :使用注册驱动可自动加载注解处理器和适配器的配置

<!-- 注解驱动 -->
<mvc:annotation-driven />

 

 3. 视图解析器

        逻辑视图名需要在 controller 中返回 ModeAndView 指定

        用法:在 springmvc.xml 配置文件中配置如下

<!-- 配置视图解析器 -->
<bean    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 配置逻辑视图的前缀 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <!-- 配置逻辑视图的后缀 -->
    <property name="suffix" value=".jsp" />
</bean>

 

posted @ 2018-02-06 14:19  大概是个小学生  阅读(2042)  评论(0编辑  收藏  举报