日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

(转)SpringMVC学习(一)——SpringMVC介绍与入门

發布時間:2024/9/21 javascript 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (转)SpringMVC学习(一)——SpringMVC介绍与入门 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://blog.csdn.net/yerenyuan_pku/article/details/72231272

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,回車,就能看到如下效果:?

讀者如需源碼,可點擊SpringMVC學習(一)——SpringMVC入門小程序下載!

總結

以上是生活随笔為你收集整理的(转)SpringMVC学习(一)——SpringMVC介绍与入门的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。