1. 程式人生 > >SpringMVC 框架系列之初識與入門實例

SpringMVC 框架系列之初識與入門實例

uip val patch cti 分發 作用 getc 問題 bdb

微信公眾號:compassblog

歡迎關註、轉發,互相學習,共同進步!

有任何問題,請後臺留言聯系!

1、SpringMVC 概述

(1)、 MVC:Model-View-Control

Control 層屬於框架性質,完成的主要工作是:封裝 web 請求為一個數據對象、調用業務邏輯層來處理數據對象、返回處理數據結果及相應的視圖給客戶端。

(2)、什麽是 SpringMVC

Spring mvc 和 Struts2 都屬於表現層的框架,是 Spring 框架的一部分,Spring 框架的 Control 層的核心是 DispatcherServlet,它的作用是將請求分發給不同的後端處理器。

Spring 的 Control 層框架使用了後端控制器來映射處理器和視圖解析器來共同完成 Control 層框架的主要工作。並且 spring 的 Control 層框架還真正地把業務層處理的數據結果和相應的視圖拼成一個對象,即 ModelAndView 對象。

SpringMVC 本身就是 Spring 的子項目,對 Spring 兼容性很好,因此也不需要做過多的配置。

(3)SpringMVC 框架處理流程

技術分享圖片

2、SpringMVC 入門實例

(1)、創建 web 項目,導入所需要的 jar 包,如圖下圖所示:

技術分享圖片

(2)、創建 SpringMVC 的核心配置文件,具體配置如下:

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">



<!-- 掃描@Controler @Service -->
<context:component-scan base-package="com.springmvc"/>

<!-- 處理器映射器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> -->
<!-- 處理器適配器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> -->
<!-- 註解驅動 -->
<mvc:annotation-driven/>

<!-- 視圖解釋器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>

</beans>

(3)、配置 SpringMVC 的前端控制器 DispatcherServlet,具體配置如下:

web.xml

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">
<display-name>SpringmvcProject</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<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>
</web-app>

(4)、在 /WEB-INF/ 目錄下新建 jsp 目錄,新建 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">
<p></p>
商品列表:
<p></p>
<table width="100%" border=1>
<tr>
<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>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>

(5)、創建 POJO 類,具體代碼如下:

Items.java

package com.springmvc.pojo;

import java.util.Date;

public class Items {

public Items(Integer id, String name, Float price, Date createtime, String detail) {
super();
this.id = id;
this.name = name;
this.price = price;
this.createtime = createtime;
this.detail = detail;
}

private Integer id;

private String name;

private Float price;

private String pic;

private Date createtime;

private String detail;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name == null ? null : name.trim();
}

public Float getPrice() {
return price;
}

public void setPrice(Float price) {
this.price = price;
}

public String getPic() {
return pic;
}

public void setPic(String pic) {
this.pic = pic == null ? null : pic.trim();
}

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 == null ? null : detail.trim();
}
}

(6)、創建 Controller 類,具體代碼如下:

ItemController.java

package com.springmvc.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.springmvc.pojo.Items;

/**
* 商品管理
*
* @author lx
*
*/
@Controller
public class ItemController {

@RequestMapping(value = "/itemlist.action")
public ModelAndView itemList(){

// 創建頁面需要顯示的商品數據
List<Items> list = new ArrayList<Items>();
list.add(new Items(1, "1華為 榮耀8", 2399f, new Date(), "質量好!1"));
list.add(new Items(2, "2華為 榮耀8", 2399f, new Date(), "質量好!2"));
list.add(new Items(3, "3華為 榮耀8", 2399f, new Date(), "質量好!3"));
list.add(new Items(4, "4華為 榮耀8", 2399f, new Date(), "質量好!4"));
list.add(new Items(5, "5華為 榮耀8", 2399f, new Date(), "質量好!5"));
list.add(new Items(6, "6華為 榮耀8", 2399f, new Date(), "質量好!6"));

ModelAndView mav = new ModelAndView();
//數據
mav.addObject("itemList", list);
mav.setViewName("itemList");
return mav;
}

}

(7)、啟動 Tomcat,並到瀏覽器地址欄測試,結果如下:

技術分享圖片

3、SpringMVC 框架訪問流程圖

技術分享圖片

註:部分知識源於網絡,侵權聯刪。

關註微信公眾號compassblog,後臺回復 “SpringMVC系列一” 獲取本項目源碼

推薦閱讀:

  • PyCharm 2017.3 下載與安裝

  • IntelliJ IDEA 2017.3下載與安裝

  • Spring框架系列之AOP思想

本系列後期仍會持續更新,歡迎關註!

如果你認為這篇文章有用,歡迎轉發分享給你的好友!

本號文章可以任意轉載,轉載請註明出處!

掃描關註微信公眾號,了解更多

技術分享圖片

SpringMVC 框架系列之初識與入門實例