1、SpringMVC入门

来源:互联网 发布:qt网络编程项目java 编辑:程序博客网 时间:2024/06/05 09:47

SpringMVC概述

      Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱动指的就是使用请求-响应模型,框架的目的就是帮助我们简化开发,Spring Web MVC也是要简化我们日常Web开发的。

      Spring web MVC框架提供了MVC(模型 - 视图 - 控制器)架构和用于开发灵活和松散耦合的Web应用程序的组件。 MVC模式导致应用程序的不同方面(输入逻辑,业务逻辑和UI逻辑)分离,同时提供这些元素之间的松散耦合。

  • 模型(Model) 封装了应用程序数据,通常它们将由POJO类组成。
  • 视图(View) 负责渲染模型数据,一般来说它生成客户端浏览器可以解释HTML输出。
  • 控制器(Controller) 负责处理用户请求并构建适当的模型,并将其传递给视图进行渲染。

SpringMVC处理流程

      Spring Web模型 - 视图 - 控制器(MVC)框架是围绕DispatcherServlet设计的,它处理所有的HTTP请求和响应。 Spring Web MVC DispatcherServlet的请求处理工作流如下图所示:

这里写图片描述

详细步骤如下:

  • 接收到请求后DispatcherServlet查询HandlerMapping以调用相应的Controller;

  • Controller接受请求并根据使用的GETPOST方法调用相应的服务方法。服务方法会按照业务设置模型数据,并将师徒名称返回给DispatcherServlet;

  • DispatcherServlet将从ViewResolver中获取请求定义的视图;

  • 当视图完成,DispatcherServlet将模型数据传递到最终的视图,并在浏览器上呈现。


SpringMVC入门程序

需求:在页面中显示商品列表。

1、创建动态Web工程

这里写图片描述

2、导入jar包

http://download.csdn.net/download/qq_25343557/10165141
这里写图片描述

3、添加配置文件
3.1、创建springmvc.xml文件:
      SpringMVC本身就是Spring的一个子项目,所以不需要做太多的配置,这里只需要配置组件扫描包,对Controller进行管理。
<?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">    <!-- 配置controller扫描包 -->    <context:component-scan base-package="cn.xpu.hcp.springmvc.controller"/></beans>

3.2、在web.xml中配置前端控制器DispatcherServlet
<?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">  <!-- 配置SpringMVC前端控制器 -->  <servlet>    <servlet-name>springmvc</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <!-- 指定SpringMVC的配置文件,SpringMVC配置文件的默认路径是/WEB-INF/${servlet-name}-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结尾的请求进入SpringMVC -->    <url-pattern>*.action</url-pattern>  </servlet-mapping></web-app>
3.3、加入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">        查询条件:        <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="${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>                    <td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>                </tr>            </c:forEach>        </table>    </form></body></html>
3.4、创建POJO
public class Item {    // 商品id    private int id;    // 商品名称    private String name;    // 商品价格    private double price;    // 商品创建时间    private Date createtime;    // 商品描述    private String detail;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public double getPrice() {        return price;    }    public void setPrice(double price) {        this.price = price;    }    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;    }    public Item() {        super();    }    public Item(int id, String name, double price, Date createtime,            String detail) {        super();        this.id = id;        this.name = name;        this.price = price;        this.createtime = createtime;        this.detail = detail;    }}
3.5、创建Contorller—–ItemContorller

      ItemController是一个普通的java类,不需要实现任何接口。需要在类上添加@Controller注解,把Controller交由Spring管理。在方法上面添加@RequestMapping注解,里面指定请求的url。其中“.action”可以加也可以不加。

@Controllerpublic class ItemController {    // @RequestMapping:里面放的是请求的url,和用户请求的url进行匹配    // action可以写也可以不写    @RequestMapping("/itemList.action")    public ModelAndView queryItemList() {        // 创建页面需要显示的商品数据        List<Item>list = new ArrayList<>();        list.add(new Item(1, "1华为 荣耀8", 2399, new Date(), "质量好!1"));        list.add(new Item(2, "2华为 荣耀8", 2399, new Date(), "质量好!2"));        list.add(new Item(3, "3华为 荣耀8", 2399, new Date(), "质量好!3"));        list.add(new Item(4, "4华为 荣耀8", 2399, new Date(), "质量好!4"));        list.add(new Item(5, "5华为 荣耀8", 2399, new Date(), "质量好!5"));        list.add(new Item(6, "6华为 荣耀8", 2399, new Date(), "质量好!6"));        // 创建ModelAndView,用来存放数据和视图        ModelAndView modelAndView = new ModelAndView();        // 设置数据到模型中        modelAndView.addObject("list", list);        // 设置视图jsp,需要设置视图的物理地址        modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");        return modelAndView;    }}
3.6、发布运行

这里写图片描述

原创粉丝点击