1. 程式人生 > >SpringMVC學習(一)——SpringMVC介紹與入門

SpringMVC學習(一)——SpringMVC介紹與入門

SpringMVC介紹

SpringMVC是什麼?

SpringMVC和Struts2都屬於表現層的框架,它是Spring框架的一部分,我們可以從Spring的整體結構中看得出來:
這裡寫圖片描述

SpringMVC處理流程

SpringMVC處理流程如下圖所示:
這裡寫圖片描述
這個圖大致描述了SpringMVC的整個處理流程,乍一看有點暈乎,且待我一步步分析,最後弄個流程圖出來就明白了。

SpringMVC入門程式

本系列教程使用的是SpringMVC4.1.3這個版本。下面我就來教大家如何入門SpringMVC這個框架。
現有這樣一個需求:使用SpringMVC這個框架實現商品列表的展示。這是我對這個需求的分析:我這裡假設請求的url為/itemList.action

,由於我想要展示商品列表,所以是並不需要傳遞引數的,再次是這裡僅僅是一個SpringMVC的一個入門小程式,並不會與MyBatis進行整合,也就不會從資料庫表裡面查詢商品列表資訊,故查詢商品列表資料也僅僅只是一些靜態資料。下面正式開始SpringMVC的入門小程式。

SpringMVC入門程式的開發步驟

【第一步】,建立一個javaweb工程,例如springmvc-first。
【第二步】,匯入SpringMVC獨立執行的jar包,如下:
這裡寫圖片描述
【第三步】,建立一個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"> 查詢條件: <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>

並把該jsp頁面複製到工程的/WEB-INF/jsp目錄下。
【第四步】,建立一個Item類,用於描述商品資訊,其內容如下:

public class Items {

    private int id;
    private String name;
    private double price;
    private Date createtime;
    private String detail;

    public Items(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;
    }

    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;
    }

}

並將該類複製到工程src目錄下的com.itheima.springmvc.pojo包中。
【第五步】,建立ItemController,ItemController是一個普通的java類,有點類似於Struts2中的Action,且不需要實現任何介面,只需要在類上新增@Controller註解即可。@RequestMapping註解指定請求的url,其中“.action”可以加也可以不加。在ModelAndView物件中,將檢視設定為“/WEB-INF/jsp/itemList.jsp”。

@Controller
public class ItemController {
    // .action可以省略  (請求的url地址)
    @RequestMapping("/itemList.action")
    public ModelAndView itemList() {
        // 查詢商品列表,使用靜態資料生成一個商品列表
        List<Items> itemList = new ArrayList<Items>();
        itemList.add(new Items(1, "imac", 20000, new Date(), "蘋果本很貴"));
        itemList.add(new Items(2, "imac1", 20000, new Date(), "蘋果本很貴"));
        itemList.add(new Items(3, "imac2", 20000, new Date(), "蘋果本很貴"));
        itemList.add(new Items(4, "imac3", 20000, new Date(), "蘋果本很貴"));
        itemList.add(new Items(5, "imac4", 20000, new Date(), "臥槽,蘋果本很貴啦!"));
        // 把商品列表傳遞給jsp
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("itemList", itemList);
        // 設定展示資料的檢視,即jsp
        modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");
        // 返回結果
        return modelAndView;
    }
}

最後將ItemController類複製到工程src目錄下的com.itheima.springmvc.controller包中。
【第六步】,建立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">

    <context:component-scan base-package="com.itheima.springmvc.controller"/>
</beans>

上面配置了掃描包(Controller類所在的包),那麼它就會掃描這個包下所有帶@Controller註解的類,並建立物件放到springmvc容器中。
【第七步】,配置前端控制器。在web.xml中新增DispatcherServlet的配置,即在web.xml檔案中新增如下配置:

<!-- 配置前端控制器 -->
<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <!-- 指定springmvc配置檔案的路徑。如果不指定,預設為:/WEB-INF/${servlet-name}-servlet.xml -->
        <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>

【第八步】,入門程式測試。在瀏覽器位址列中輸入url地址——http://localhost:8080/springmvc-first/itemList.action,回車,就能看到如下效果:
這裡寫圖片描述
讀者如需原始碼,可點選下載!