生活随笔
收集整理的這篇文章主要介紹了
(转)SpringMVC学习(一)——SpringMVC介绍与入门
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
http://blog.csdn.net/yerenyuan_pku/article/details/72231272
SpringMVC介紹
SpringMVC是什么?
SpringMVC和Struts2都屬于表現(xiàn)層的框架,它是Spring框架的一部分,我們可以從Spring的整體結(jié)構(gòu)中看得出來(lái):?
SpringMVC處理流程
SpringMVC處理流程如下圖所示:?
這個(gè)圖大致描述了SpringMVC的整個(gè)處理流程,乍一看有點(diǎn)暈乎,且待我一步步分析,最后弄個(gè)流程圖出來(lái)就明白了。
SpringMVC入門程序
本系列教程使用的是SpringMVC4.1.3這個(gè)版本。下面我就來(lái)教大家如何入門SpringMVC這個(gè)框架。?
現(xiàn)有這樣一個(gè)需求:使用SpringMVC這個(gè)框架實(shí)現(xiàn)商品列表的展示。這是我對(duì)這個(gè)需求的分析:我這里假設(shè)請(qǐng)求的url為/itemList.action,由于我想要展示商品列表,所以是并不需要傳遞參數(shù)的,再次是這里僅僅是一個(gè)SpringMVC的一個(gè)入門小程序,并不會(huì)與MyBatis進(jìn)行整合,也就不會(huì)從數(shù)據(jù)庫(kù)表里面查詢商品列表信息,故查詢商品列表數(shù)據(jù)也僅僅只是一些靜態(tài)數(shù)據(jù)。下面正式開(kāi)始SpringMVC的入門小程序。
SpringMVC入門程序的開(kāi)發(fā)步驟
【第一步】,創(chuàng)建一個(gè)javaweb工程,例如springmvc-first。?
【第二步】,導(dǎo)入SpringMVC獨(dú)立運(yùn)行的jar包,如下:?
【第三步】,創(chuàng)建一個(gè)jsp頁(yè)面——itemList.jsp,內(nèi)容如下:
<%@ 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>商品價(jià)格</td> <td>生產(chǎn)日期</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頁(yè)面復(fù)制到工程的/WEB-INF/jsp目錄下。?
【第四步】,創(chuàng)建一個(gè)Item類,用于描述商品信息,其內(nèi)容如下:
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; } } 并將該類復(fù)制到工程src目錄下的com.itheima.springmvc.pojo包中。?
【第五步】,創(chuàng)建ItemController,ItemController是一個(gè)普通的java類,有點(diǎn)類似于Struts2中的Action,且不需要實(shí)現(xiàn)任何接口,只需要在類上添加@Controller注解即可。@RequestMapping注解指定請(qǐng)求的url,其中“.action”可以加也可以不加。在ModelAndView對(duì)象中,將視圖設(shè)置為“/WEB-INF/jsp/itemList.jsp”。
@Controller
public class ItemController { 最后將ItemController類復(fù)制到工程src目錄下的com.itheima.springmvc.controller包中。?
【第六步】,創(chuàng)建springmvc.xml,內(nèi)容如下:
<?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類所在的包),那么它就會(huì)掃描這個(gè)包下所有帶@Controller注解的類,并創(chuàng)建對(duì)象放到springmvc容器中。?
【第七步】,配置前端控制器。在web.xml中添加DispatcherServlet的配置,即在web.xml文件中添加如下配置:
【第八步】,入門程序測(cè)試。在瀏覽器地址欄中輸入url地址——http://localhost:8080/springmvc-first/itemList.action,回車,就能看到如下效果:?
讀者如需源碼,可點(diǎn)擊SpringMVC學(xué)習(xí)(一)——SpringMVC入門小程序下載!
總結(jié)
以上是生活随笔為你收集整理的(转)SpringMVC学习(一)——SpringMVC介绍与入门的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。