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

SpringMVC介紹

SpringMVC是什麼?

SpringMVC和Struts2都屬於表現層的框架,它是Spring框架的一部分,咱們能夠從Spring的總體結構中看得出來:
這裏寫圖片描述html

SpringMVC處理流程

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

SpringMVC入門程序

本系列教程使用的是SpringMVC4.1.3這個版本。下面我就來教你們如何入門SpringMVC這個框架。
現有這樣一個需求:使用SpringMVC這個框架實現商品列表的展現。這是我對這個需求的分析:我這裏假設請求的url爲/itemList.action,因爲我想要展現商品列表,因此是並不須要傳遞參數的,再次是這裏僅僅是一個SpringMVC的一個入門小程序,並不會與MyBatis進行整合,也就不會從數據庫表裏面查詢商品列表信息,故查詢商品列表數據也僅僅只是一些靜態數據。下面正式開始SpringMVC的入門小程序。java

SpringMVC入門程序的開發步驟

【第一步】,建立一個javaweb工程,例如springmvc-first。
【第二步】,導入SpringMVC獨立運行的jar包,以下:
這裏寫圖片描述
【第三步】,建立一個jsp頁面——itemList.jsp,內容以下:web

<%@ 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類,用於描述商品信息,其內容以下:spring

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,回車,就能看到以下效果:spring-mvc

這裏寫圖片描述

相關文章
相關標籤/搜索