框架應用 : Spring MVC - 開發詳述

軟件開發中的MVC設計模式

   軟件開發的目標是減少耦合,讓模塊以前關係清晰.javascript

  MVC模式在軟件開發中常常和ORM模式一塊兒應用,主要做用是將(數據抽象,數據實體傳輸和前臺數據展現)分層,這樣前臺,後臺,數據庫三層分離,減少耦合.html

  

  1)Model  :  負責抽象並存儲數據庫數據前端

  2)Controller :  負責數據的轉化和傳輸java

  3)View    :  負責展現數據mysql

  注意 框架的應用使軟件開發變得更有章可循,更規範化,軟件開發的每一個職責都落到了具體的模塊中去jquery

      ORM框架 : 將數據庫數據封裝存儲至Model類中web

      MVC框架 : 前臺數據與實體數據的互爲轉化ajax

      開發者   : 配置框架,編寫業務代碼,前臺數據展現頁面spring

SpringMVC工做流

  SpringMVC是創建在Spring framework之上的一個MVC框架,主要的組件有前端控制器處理器映射器處理器適配器視圖解析器.sql

   

  1.SpringMVC的原理就如上圖所展現的,客戶端發來HTTP請求,

  2.前端控制器做爲一個頂層執行者獲取請求,

  3.返回一個執行鏈HandlerExecutionChain{HandlerInterceptor1,HandlerInterceptor2,Handler},

  4.前端控制器請求映射器查詢對應的處理器適配器,

  5.處理器適配器代理執行處理器,

  6.處理器響應處理,返回一個ModelAndView,

  7.適配器返回一個ModelAndView至前端控制器

  8.前端控制器請求視圖解析器執行解析,

  9.解析器返回一個未經渲染的視圖

  10.前端控制器渲染視圖,將模型數據存於request域

  11.視圖發至瀏覽器進行渲染呈現

  注意,上圖分爲Model,View,Controller等模塊,其中Model裏面可封裝許多邏輯,這一部分未在步驟上進行說明.

對比Struts2

  1.SpringMVC的前端入口是一個名爲DispatchServlet的servlet,Struts2是一個Filter過濾器;

  2.SpringMVC基於方法進行開發(一個url對應一個方法),請求參數傳遞到方法的形參,能夠設計爲單例或者多例模式,Struts2是基於類開發,傳遞的參數經過類的屬性進行傳遞;

  3.Struts2採用值棧存儲請求和響應,經過OGNL存取數據,SpringMVC經過參數解析器將request內容進行解析,並給方法形參賦值,將數據和視圖封裝成一個ModelAndView對象,最後將ModelAndView的數據傳入request中以及視圖渲染,JSP使用JSTL來進行取值操做;

組件職責描述

1.前端控制器DispatcherServlet(無需開發者開發)

做用 : 接收請求,響應結果,至關於轉發器,中央處理器. 

  有了DispatcherServlet減小了其它組件之間的耦合度. 

類位置  org.springframework.web.servlet.DispatcherServlet.class

配置文件  /org/springframework/web/servlet/DispatcherServlet.properties

<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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
配置方式

 

2.處理器映射器HandlerMapping(無需開發者開發)

    做用:根據請求的url查找Handler和Interceptor攔截器,

      將它們封裝在HandlerExecutionChain 對象中給前端控制器返回.

類位置  org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping.class

<!—beanName Url映射器 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
配置方式

        org.springframework.web.servlet.handler.SimpleUrlHandlerMapping.class

<!—

簡單url映射
simpleUrlHandlerMapping是BeanNameUrlHandlerMapping的加強版本,它能夠將url和處理器bean的id進行統一映射配置
 -->
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/items1.action">controller的bean id</prop>
                <prop key="/items2.action">controller的bean id</prop>
            </props>
        </property>
    </bean>
配置方式

     org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.class

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
配置方式

 

3.處理器適配器HandlerAdapter(無需開發者開發)

做用:按照特定規則(HandlerAdapter要求的規則)去執行Handler

    類位置  org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.class

<!--
全部實現了org.springframework.web.servlet.mvc.Controller 接口的Bean經過此適配器進行適配、執行.
 -->

<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
配置方式

         org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.class 

<!-- 
全部實現了org.springframework.web.HttpRequestHandler 接口的Bean經過此適配器進行適配、執行.
-->

<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>
配置方式

         org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.class

<!--
    註解時使用的Adapter,配合註解映射器一塊兒使用
 -->

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
配置方式

 

4.處理器Handler(需開發者開發)

注意:編寫Handler時按照HandlerAdapter的要求去作,這樣適配器才能夠去正確執行Handler

    非註解方式

package com.harry.controller;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.harry.entity.Items;

/***
 * @author harry
 *    用於處理頁面請求,
 *    數據來源是經過數據庫ORM映射至實體類來獲取,
 *    這裏使用靜態獲取數據進行簡單演示.
 */
public class ItemList_1 implements Controller {

    @Override
    public ModelAndView handleRequest(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        
        //調用service查找 數據庫,查詢商品列表,這裏使用靜態數據模擬
        List<Items> itemsList = new ArrayList<Items>();
        //向list中填充靜態數據
        
        Items items_1 = new Items();
        items_1.setName("聯想筆記本");
        items_1.setPrice(6000f);
        items_1.setDetail("ThinkPad T430 聯想筆記本電腦!");
        
        Items items_2 = new Items();
        items_2.setName("蘋果手機");
        items_2.setPrice(5000f);
        items_2.setDetail("iphone6蘋果手機!");
        
        itemsList.add(items_1);
        itemsList.add(items_2);

        //返回ModelAndView
        ModelAndView modelAndView =  new ModelAndView();
        //至關 於request的setAttribut,在jsp頁面中經過itemsList取數據
        modelAndView.addObject("itemsList", itemsList);
        
        //指定視圖
        modelAndView.setViewName("Items/itemList");

        return modelAndView;
    }

}
ItemList_1.java

    註解方式

@Controller
public class ItemList_2 {
/*
    RequestMappingHandlerMapping將對類中標記@ResquestMapping的方法進行映射,根據ResquestMapping定義的url匹配ResquestMapping標記的方法,匹配成功返回HandlerMethod對象給前端控制器,HandlerMethod對象中封裝url對應的方法Method
*/
    @RequestMapping("/queryItem.action")
    public ModelAndView queryItem() {
        // 商品列表
        List<Items> itemsList = new ArrayList<Items>();

        Items items_1 = new Items();
        items_1.setName("聯想筆記本");
        items_1.setPrice(6000f);
        items_1.setDetail("ThinkPad T430 聯想筆記本電腦!");

        Items items_2 = new Items();
        items_2.setName("蘋果手機");
        items_2.setPrice(5000f);
        items_2.setDetail("iphone6蘋果手機!");

        itemsList.add(items_1);
        itemsList.add(items_2);

        // 建立modelAndView準備填充數據、設置視圖
        ModelAndView modelAndView = new ModelAndView();

        // 填充數據
        modelAndView.addObject("itemsList", itemsList);
        // 視圖
        modelAndView.setViewName("order/itemsList");

        return modelAndView;
    }

}
ItemList_2.java

注意,註解方式應使用註解包掃描<context:component-scan base-package="com.harry.controller"/>,

包掃描能夠掃描到@Bean,@Controller,@Service,@Repository等註解類.

RequestMappingHandlerMapping,HttpRequestHandlerAdapter(<mvc:annotation-driven>可自動加載這兩個類)

 

5.視圖解析器View resolver(無需開發者開發)

做用:進行視圖解析,根據邏輯視圖名解析成真正的視圖(view

類位置  org.springframework.web.servlet.view.InternalResourceViewResolver.class

<!-- ViewResolver配置 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>
配置方式

 

6.視圖View(需開發者開發)

View是一個接口,實現類支持不一樣的View類型(jspfreemarkerpdf... 

 

案例驅動開發

   springmvc的開發,須要配置上述工做流中的組件,入門程序緊接着框架應用:Mybatis - 開發詳述的案例進行開發,

  1.備份數據庫,實體類

-- MySQL dump 10.13  Distrib 5.7.18, for Linux (x86_64)
--
-- Host: localhost    Database: mybatis
-- ------------------------------------------------------
-- Server version    5.7.18-0ubuntu0.16.04.1

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `items`
--

DROP TABLE IF EXISTS `items`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `items` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(32) NOT NULL COMMENT '商品名稱',
  `price` float(10,1) NOT NULL COMMENT '商品訂價',
  `detail` text COMMENT '商品描述',
  `pic` varchar(64) DEFAULT NULL COMMENT '商品圖片',
  `createtime` datetime NOT NULL COMMENT '生產日期',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `items`
--

LOCK TABLES `items` WRITE;
/*!40000 ALTER TABLE `items` DISABLE KEYS */;
INSERT INTO `items` VALUES (1,'臺式機',3000.0,'該電腦質量很是好!!!!',NULL,'2015-02-03 13:22:53'),(2,'筆記本',6000.0,'筆記本性能好,質量好!!!!!',NULL,'2015-02-09 13:22:57'),(3,'揹包',200.0,'名牌揹包,容量大質量好!!!!',NULL,'2015-02-06 13:23:02');
/*!40000 ALTER TABLE `items` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `orderdetail`
--

DROP TABLE IF EXISTS `orderdetail`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `orderdetail` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `orders_id` int(11) NOT NULL COMMENT '訂單id',
  `items_id` int(11) NOT NULL COMMENT '商品id',
  `items_num` int(11) DEFAULT NULL COMMENT '商品購買數量',
  PRIMARY KEY (`id`),
  KEY `FK_orderdetail_1` (`orders_id`),
  KEY `FK_orderdetail_2` (`items_id`),
  CONSTRAINT `FK_orderdetail_1` FOREIGN KEY (`orders_id`) REFERENCES `orders` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `FK_orderdetail_2` FOREIGN KEY (`items_id`) REFERENCES `items` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `orderdetail`
--

LOCK TABLES `orderdetail` WRITE;
/*!40000 ALTER TABLE `orderdetail` DISABLE KEYS */;
INSERT INTO `orderdetail` VALUES (1,3,1,1),(2,3,2,3),(3,4,3,4),(4,4,2,3);
/*!40000 ALTER TABLE `orderdetail` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `orders`
--

DROP TABLE IF EXISTS `orders`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `orders` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL COMMENT '下單用戶id',
  `number` varchar(32) NOT NULL COMMENT '訂單號',
  `createtime` datetime NOT NULL COMMENT '建立訂單時間',
  `note` varchar(100) DEFAULT NULL COMMENT '備註',
  PRIMARY KEY (`id`),
  KEY `FK_orders_1` (`user_id`),
  CONSTRAINT `FK_orders_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `orders`
--

LOCK TABLES `orders` WRITE;
/*!40000 ALTER TABLE `orders` DISABLE KEYS */;
INSERT INTO `orders` VALUES (3,1,'1000010','2015-02-04 13:22:35',NULL),(4,1,'1000011','2015-02-03 13:22:41',NULL),(5,10,'1000012','2015-02-12 16:13:23',NULL);
/*!40000 ALTER TABLE `orders` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `user`
--

DROP TABLE IF EXISTS `user`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(32) NOT NULL COMMENT '用戶名稱',
  `birthday` date DEFAULT NULL COMMENT '生日',
  `sex` char(1) DEFAULT NULL COMMENT '性別',
  `address` varchar(256) DEFAULT NULL COMMENT '地址',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `user`
--

LOCK TABLES `user` WRITE;
/*!40000 ALTER TABLE `user` DISABLE KEYS */;
INSERT INTO `user` VALUES (1,'王五',NULL,'2',NULL),(10,'張三','2014-07-10','1','北京市'),(16,'張小明',NULL,'1','河南鄭州'),(22,'陳小明',NULL,'1','河南鄭州'),(24,'張三丰',NULL,'1','河南鄭州'),(25,'陳小明',NULL,'1','河南鄭州'),(26,'王五',NULL,NULL,NULL);
/*!40000 ALTER TABLE `user` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2017-10-10  0:00:04
sql
package com.harry.entity;

import java.util.Date;

public class Items {
    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();
    }
}
Items.java
package com.harry.entity;

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

public class Orders {
    private Integer id;

    private Integer userId;

    private String number;

    private Date createtime;

    private String note;
    
    //用戶信息
    private User user;
    
    //訂單明細
    private List<Orderdetail> orderdetails;

    public Integer getId() {
        return id;
    }

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

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number == null ? null : number.trim();
    }

    public Date getCreatetime() {
        return createtime;
    }

    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note == null ? null : note.trim();
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public List<Orderdetail> getOrderdetails() {
        return orderdetails;
    }

    public void setOrderdetails(List<Orderdetail> orderdetails) {
        this.orderdetails = orderdetails;
    }

    @Override
    public String toString() {
        return "Orders [id=" + id + ", userId=" + userId + ", number=" + number + ", createtime=" + createtime
                + ", note=" + note + ", user=" + user + ", orderdetails=" + orderdetails + "]";
    }

    
    
    
}
Orders.java
package com.harry.entity;

public class Orderdetail {
    private Integer id;

    private Integer ordersId;

    private Integer itemsId;

    private Integer itemsNum;
    
    //明細對應的商品信息
    private Items items;
    

    public Integer getId() {
        return id;
    }

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

    public Integer getOrdersId() {
        return ordersId;
    }

    public void setOrdersId(Integer ordersId) {
        this.ordersId = ordersId;
    }

    public Integer getItemsId() {
        return itemsId;
    }

    public void setItemsId(Integer itemsId) {
        this.itemsId = itemsId;
    }

    public Integer getItemsNum() {
        return itemsNum;
    }

    public void setItemsNum(Integer itemsNum) {
        this.itemsNum = itemsNum;
    }

    public Items getItems() {
        return items;
    }

    public void setItems(Items items) {
        this.items = items;
    }

    @Override
    public String toString() {
        return "Orderdetail [id=" + id + ", ordersId=" + ordersId
                + ", itemsId=" + itemsId + ", itemsNum=" + itemsNum + "]";
    }
    
}
Orderdetail.java
package com.harry.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.List;


@SuppressWarnings("serial")
public class User implements Serializable {
    
    //屬性名和數據庫表的字段對應
    private int id;
    private String username;// 用戶姓名
    private String sex;// 性別
    private Date birthday;// 生日
    private String address;// 地址
    
    //用戶建立的訂單列表
    private List<Orders> ordersList;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", sex=" + sex
                + ", birthday=" + birthday + ", address=" + address + "]";
    }
    public List<Orders> getOrdersList() {
        return ordersList;
    }
    public void setOrdersList(List<Orders> ordersList) {
        this.ordersList = ordersList;
    }


}
User.java

  2.導入spring ioc, aop, mvc, log4j...的jar包,這裏使用maven 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/jdbc/spring-cache.xsd
        ">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.harry</groupId>
  <artifactId>harry-ssm</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>harry-ssm Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
  <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

  <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.3.11.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>4.3.11.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.11.RELEASE</version>
</dependency>


    <!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.40</version>
</dependency>

<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->  
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
 


  </dependencies>
  <build>
    <finalName>harry-ssm</finalName>
  </build>
</project>
pom.xml  

  3.配置前端控制器,springmvc的入口組件

<!--
contextConfigLocation指定springmvc的主配置文件地址,如不指定則默認爲WEB-INF/[DispatcherServlet 的Servlet 名字]-servlet.xml

<load-on-startup>1</load-on-startup> 服務器啓動時建立

<url-pattern>*.action</url-pattern> 攔截地址指明全部.action結尾的地址
 -->

<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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
web.xml

  4.配置處理器適配器

<!--
配置處理器適配器:簡單處理器適配器
全部實現了org.springframework.web.servlet.mvc.Controller 接口的Bean做爲Springmvc的後端控制器。
-->
<bean        class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
springmvc.xml

  5.配置處理器

package com.harry.controller;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.harry.entity.Items;

/***
 * @author harry
 *    用於處理頁面請求,
 *    數據來源是經過數據庫ORM映射至實體類來獲取,
 *    這裏使用靜態獲取數據進行簡單演示.
 */
public class ItemList_1 implements Controller {

    @Override
    public ModelAndView handleRequest(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        
        //調用service查找 數據庫,查詢商品列表,這裏使用靜態數據模擬
        List<Items> itemsList = new ArrayList<Items>();
        //向list中填充靜態數據
        
        Items items_1 = new Items();
        items_1.setName("聯想筆記本");
        items_1.setPrice(6000f);
        items_1.setDetail("ThinkPad T430 聯想筆記本電腦!");
        
        Items items_2 = new Items();
        items_2.setName("蘋果手機");
        items_2.setPrice(5000f);
        items_2.setDetail("iphone6蘋果手機!");
        
        itemsList.add(items_1);
        itemsList.add(items_2);

        //返回ModelAndView
        ModelAndView modelAndView =  new ModelAndView();
        //至關 於request的setAttribut,在jsp頁面中經過itemsList取數據
        modelAndView.addObject("itemsList", itemsList);
        
        //指定視圖
        modelAndView.setViewName("Items/itemList");

        return modelAndView;
    }

}
ItemList_1.java
<!-- controller配置 -->
    <bean name="/items1.action" id="itemList1" 
class="cn.itcast.springmvc.controller.first.ItemList1"/>
springmvc.xml

  6.配置處理器映射器

<!-- 處理器映射器 -->
    <!-- 根據bean的name進行查找Handler 將action的url配置在bean的name中 -->
    <bean
        class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
springmvc.xml

  7.配置視圖解析器

<!-- 
ViewResolver配置 
JstlView 指視圖使用jstl類庫支持
prefix,前綴
suffix,後綴
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>
springmvc.xml

  8.編寫視圖

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page isELIgnored ="false" %>
<%@ 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>
商品列表:
<table width="100%" border=1>
<tr>
    <td>商品名稱</td>
    <td>商品價格</td>
    <td>商品描述</td>
</tr>
<c:forEach items="${itemsList }" var="item">
<tr>
    <td>${item.name}</td>
    <td>${item.price}</td>
    <td>${item.detail}</td>
</tr>
</c:forEach>

</table>
</body>
</html>
itemList.jsp

  9.訪問http://localhost:8080/harry-ssm/items1.action

    

源碼工做流 

  描述的工做流與上面畫的圖一致,只不過此次使用源碼來進行流程分析.

  1.讀取context中的配置,初始化各大組件

   

  2.客戶端發來請求,DispatchServlet接收請求,觸發doService方法,doService方法調用doDispatch方法

  

  

  3.doDispatch調用getHandler獲取Handler,調用getHandlerAdapter獲取Adapter

  

  

  4.getHandler經過HandlerMapping對象返回HandlerExecutionChain

  

  5.從HandlerExecutionChain中獲取Handler並交由HandlerAdapter執行

  

  6.獲取的ModelAndView對象,調用render方法渲染  

  

  7.render方法經過調用視圖解析器來獲取視圖

  

  

  8.DispatchServlet渲染視圖,將Model數據放在request域中

  

  9.客戶端讀取響應

 整合SSM

  Spring整合Mybatis這個ORM框架已經在 框架應用:Mybatis  - 開發詳述 整合過了,如今三個框架進行整合.

  須要完成的工做其實就是處理這關係線:

    User <--> View <--> Action <--> Service <--> DAO <--> DB

                ---------------------Spring-------------------

  DAO層鏈接數據庫須要要數據源信息db.properties,框架須要log4j.properties,mybatis的總配置文件sqlMapConfig.xml,使用spring來配置數據源,管理事務,配置SqlSessionFactory和mapper掃描器的applicationContext-dao.xml,Mapper對象的配置文件XXXMapper.xml.

  Service層用spring配置service接口applicationContext-service.xml,配置事務管理applicationContext-transaction.xml,Service接口 

  Action層springmvc配置文件springmvc.xml,web.xml中配置DispatchServlet,配置編碼轉換器,加載spring容器,Controller類

    

    DAO

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=XXXX
jdbc.password=XXXX
db.properties
# Global logging configuration,建議開發環境中要用debug
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
log4j.properties
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

<!—使用自動掃描器時,mapper.xml文件若是和mapper.java接口在一個目錄則此處不用定義mappers -->
<mappers>
<package name="com.harry.ssm.mapper" />
</mappers>
</configuration>
sqlMapConfig.xml
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 加載配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 數據庫鏈接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
       <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxActive" value="30"/>
        <property name="maxIdle" value="5"/>
</bean>    


<!-- 讓spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 數據庫鏈接池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 加載mybatis的全局配置文件 -->
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
    </bean>
<!-- mapper掃描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 <property name="basePackage" value="com.harry.ssm.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

</beans>
applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.harry.ssm.mapper.ItemsMapper">
    
    <!-- sql片斷 -->
    <!-- 商品查詢條件 -->
    <sql id="query_items_where">
        <if test="items!=null">
            <if test="items.name!=null and items.name!=''">
                and items.name like '%${items.name}%'
            </if>
        </if>
    </sql>
        
    <!-- 查詢商品信息 -->
    <select id="findItemsList" parameterType="queryVo" resultType="items">
        select * from items 
        <where>
            <include refid="query_items_where"/>
        </where>
    </select>
    

</mapper>
ItemsMapper.xml
public interface ItemsMapper {
    //商品列表
    public List<Items> findItemsList(QueryVo queryVo) throws Exception;
}
ItemsMapper.java

    Service

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 商品管理的service -->
<bean id="itemsService" class="com.harry.ssm.service.impl.ItemsServiceImpl"/>
</beans>
applicationContext-service.xml
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

<!-- 事務管理器 
    對mybatis操做數據庫事務控制,spring使用jdbc的事務控制類
-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!-- 數據源
    dataSource在applicationContext-dao.xml中配置了
     -->
    <property name="dataSource" ref="dataSource"/>
</bean>

<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <!-- 傳播行爲 -->
        <tx:method name="save*" propagation="REQUIRED"/>
        <tx:method name="delete*" propagation="REQUIRED"/>
        <tx:method name="insert*" propagation="REQUIRED"/>
        <tx:method name="update*" propagation="REQUIRED"/>
        <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
        <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
        <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
    </tx:attributes>
</tx:advice>
<!-- aop -->
<aop:config>
    <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.harry.ssm.service.impl.*.*(..))"/>
</aop:config>

</beans>
applicationContext-transaction.xml
package com.harry.ssm.service;

import java.util.List;

import com.harryt.ssm.po.ItemsCustom;
import com.harry.ssm.po.ItemsQueryVo;

public interface ItemsService {
    
    //商品查詢列表
    public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
    
    //根據id查詢商品信息
    /**
     * 
     * <p>Title: findItemsById</p>
     * <p>Description: </p>
     * @param id 查詢商品的id
     * @return
     * @throws Exception
     */
    public ItemsCustom findItemsById(Integer id) throws Exception;
    
    //修改商品信息
    /**
     * 
     * <p>Title: updateItems</p>
     * <p>Description: </p>
     * @param id 修改商品的id
     * @param itemsCustom 修改的商品信息
     * @throws Exception
     */
    public void updateItems(Integer id,ItemsCustom itemsCustom) throws Exception;
    

}
IItemsService.java
package com.harry.ssm.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.harry.ssm.mapper.ItemsMapper;
import com.harry.ssm.mapper.ItemsMapperCustom;
import com.harry.ssm.po.Items;
import com.harry.ssm.po.ItemsCustom;
import com.harry.ssm.po.ItemsQueryVo;
import com.harry.ssm.service.ItemsService;


public class ItemsServiceImpl implements ItemsService{
    
    @Autowired
    private ItemsMapperCustom itemsMapperCustom;
    
    @Autowired
    private ItemsMapper itemsMapper;

    @Override
    public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)
            throws Exception {
        //經過ItemsMapperCustom查詢數據庫
        return itemsMapperCustom.findItemsList(itemsQueryVo);
    }

    @Override
    public ItemsCustom findItemsById(Integer id) throws Exception {
        
        Items items = itemsMapper.selectByPrimaryKey(id);
        //中間對商品信息進行業務處理
        //....
        //返回ItemsCustom
        ItemsCustom itemsCustom = new ItemsCustom();
        //將items的屬性值拷貝到itemsCustom
        BeanUtils.copyProperties(items, itemsCustom);
        
        return itemsCustom;
        
    }

    @Override
    public void updateItems(Integer id, ItemsCustom itemsCustom) throws Exception {
        //添加業務校驗,一般在service接口對關鍵參數進行校驗
        //校驗 id是否爲空,若是爲空拋出異常
        
        //更新商品信息使用updateByPrimaryKeyWithBLOBs根據id更新items表中全部字段,包括 大文本類型字段
        //updateByPrimaryKeyWithBLOBs要求必須轉入id
        itemsCustom.setId(id);
        itemsMapper.updateByPrimaryKeyWithBLOBs(itemsCustom);
    }

}
ItemsServiceImpl.java

    Action

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
        
    <!-- 掃描controller註解,多個包中間使用半角逗號分隔 -->
    <context:component-scan base-package="com.harry.ssm.controller"/>
    
    <!--註解映射器 -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
    <!--註解適配器 -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
    
    <!-- ViewResolver -->
    <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>
springmvc.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>springmvc</display-name>

    <!-- 加載spring容器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/spring/applicationContext.xml,/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 解決post亂碼 -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <!-- springmvc的前端控制器 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- contextConfigLocation不是必須的, 若是不配置contextConfigLocation, springmvc的配置文件默認在:WEB-INF/servlet的name+"-servlet.xml" -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>

    <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>
</web-app>
web.xml
package com.harry.ssm.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.harry.ssm.po.ItemsCustom;
import com.harry.ssm.service.ItemsService;


@Controller
//爲了對url進行分類管理 ,能夠在這裏定義根路徑,最終訪問url是根路徑+子路徑
//好比:商品列表:/items/queryItems.action
@RequestMapping("/items")
public class ItemsController {

    @Autowired
    private ItemsService itemsService;

    // 商品查詢
    @RequestMapping("/queryItems")
    public ModelAndView queryItems(HttpServletRequest request) throws Exception {
        //測試forward後request是否能夠共享
        
        System.out.println(request.getParameter("id"));

        // 調用service查找 數據庫,查詢商品列表
        List<ItemsCustom> itemsList = itemsService.findItemsList(null);
        
        // 返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        // 至關 於request的setAttribut,在jsp頁面中經過itemsList取數據
        modelAndView.addObject("itemsList", itemsList);

        // 指定視圖
        // 下邊的路徑,若是在視圖解析器中配置jsp路徑的前綴和jsp路徑的後綴,修改成
        // modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
        // 上邊的路徑配置能夠不在程序中指定jsp路徑的前綴和jsp路徑的後綴
        modelAndView.setViewName("items/itemsList");

        return modelAndView;

    }

    //商品信息修改頁面顯示
    //@RequestMapping("/editItems")
    //限制http請求方法,能夠post和get
//    @RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})
//    public ModelAndView editItems()throws Exception {
//        
//        //調用service根據商品id查詢商品信息
//        ItemsCustom itemsCustom = itemsService.findItemsById(1);
//        
//        // 返回ModelAndView
//        ModelAndView modelAndView = new ModelAndView();
//        
//        //將商品信息放到model
//        modelAndView.addObject("itemsCustom", itemsCustom);
//        
//        //商品修改頁面
//        modelAndView.setViewName("items/editItems");
//        
//        return modelAndView;
//    }
    
    @RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})
    //@RequestParam裏邊指定request傳入參數名稱和形參進行綁定。
    //經過required屬性指定參數是否必需要傳入
    //經過defaultValue能夠設置默認值,若是id參數沒有傳入,將默認值和形參綁定。
    public String editItems(Model model,@RequestParam(value="id",required=true) Integer items_id)throws Exception {
        
        //調用service根據商品id查詢商品信息
        ItemsCustom itemsCustom = itemsService.findItemsById(items_id);
        
        //經過形參中的model將model數據傳到頁面
        //至關於modelAndView.addObject方法
        model.addAttribute("itemsCustom", itemsCustom);
        
        return "items/editItems";
    }
    
    //商品信息修改提交
    @RequestMapping("/editItemsSubmit")
    public String editItemsSubmit(HttpServletRequest request,Integer id,ItemsCustom itemsCustom)throws Exception {
        
        //調用service更新商品信息,頁面須要將商品信息傳到此方法
        itemsService.updateItems(id, itemsCustom);
        
        //重定向到商品查詢列表
//        return "redirect:queryItems.action";
        //頁面轉發
        //return "forward:queryItems.action";
        return "success";
    }


}
ItemsController.java

    View

<%@ 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="${itemsList }" 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 }/items/editItems.action?id=${item.id}">修改</a></td>

</tr>
</c:forEach>

</table>
</form>
</body>

</html>
itemsList.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 id="itemForm" action="${pageContext.request.contextPath }/items/editItemsSubmit.action" method="post" >
<input type="hidden" name="id" value="${itemsCustom.id }"/>
修改商品信息:
<table width="100%" border=1>
<tr>
    <td>商品名稱</td>
    <td><input type="text" name="name" value="${itemsCustom.name }"/></td>
</tr>
<tr>
    <td>商品價格</td>
    <td><input type="text" name="price" value="${itemsCustom.price }"/></td>
</tr>
<tr>
    <td>商品生產日期</td>
    <td><input type="text" name="createtime" value="<fmt:formatDate value="${itemsCustom.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>"/></td>
</tr>
<%-- <tr>
    <td>商品圖片</td>
    <td>
        <c:if test="${item.pic !=null}">
            <img src="/pic/${item.pic}" width=100 height=100/>
            <br/>
        </c:if>
        <input type="file"  name="pictureFile"/> 
    </td>
</tr> --%>
<tr>
    <td>商品簡介</td>
    <td>
    <textarea rows="3" cols="30" name="detail">${itemsCustom.detail }</textarea>
    </td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="提交"/>
</td>
</tr>
</table>

</form>
</body>

</html>
editItems.jsp

@RequestMapping

  定義於Controller類及其方法上,用於指定訪問的url的文件映射路徑和訪問方法,如

@RequestMapping("/items")
public class ItemsController {

  @RequestMapping("/queryItems",method={RequestMethod.POST,RequestMethod.GET})
  public ModelAndView queryItems(HttpServletRequest request) throws Exception {

那麼,指定的路徑爲http://localhost:8080/appName/items/queryitems.action,有POST和GET兩種訪問方式

Controller方法返回值

   ModelAndView    對象可添加model數據和指定view

  void         能夠利用內部跳轉進行響應,或者重定向,或者響應json數據

             request.getRequestDispatcher("頁面路徑").forward(request, response);

             response.sendRedirect("url")

  String        指定邏輯視圖名,重定向,內部跳轉

             return "item/editItem";

             return "redirect:queryItem.action";

             return "forward:editItem.action";

參數綁定

    參數綁定能夠是客戶端數據綁定,也能夠是服務端參數綁定;

  服務端參數綁定就是在request<--controller這個過程之間發生的事,request到達controller控制權就在咱們的手上,咱們能夠運行本身的邏輯代碼,而後獲取參數,而後再將其綁定在request域中,等着給DispatchServlet渲染.

  支持的類型:HttpServletRequest,HttpServletResponse,HttpSession,Model/ModelMap

Items item = itemService.findItemById(id);

model.addAttribute("item", item);

modelMap是model接口實現類,實質上都是實例化ModelMap來存儲key/value

頁面經過${item.xxx}來取值.

  客戶端參數綁定就是request-->controller,將客戶填入的數據進行自動獲取至方法上

  支持的類型:基礎類型(Bollean,Integer,String,Float/Double),POJO,自定義類型轉換器,集合類

public String editItem(Model model,Integer id,Boolean status) throws Exception
http://localhost:8080/springmvc_mybatis/item/editItem.action?id=2&status=false

參數id和status將自動獲取

@RequestParam 經常使用來處理基礎類型

public String editItem(@RequestParam(value="item_id",required=true) String id

  @RequestParam(value="item_status",defaultValue="true") Boolean status) {}

 

頁面定義

<input type="text" name="name"/>

<input type="text" name="price"/>

Controller方法定義,獲取items匹配的屬性名name,price

@RequestMapping("/editItemSubmit")

public String editItemSubmit(Items items)throws Exception{

System.out.println(items);

 

自定義類型轉換器

public class CustomDateConverter implements Converter<String, Date> {

    @Override
    public Date convert(String source) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return simpleDateFormat.parse(source);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
CustomDateConverter.java
<mvc:annotation-driven conversion-service="conversionService">
</mvc:annotation-driven>
<!-- conversionService -->
    <bean id="conversionService"
        class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <!-- 轉換器 -->
        <property name="converters">
            <list>
                <bean class="com.harry.controller.converter.CustomDateConverter"/>
            </list>
        </property>
    </bean>
springmvc.xml

  

  集合類

<input type="checkbox" name="item_id" value="001"/>
<input type="checkbox" name="item_id" value="002"/>
<input type="checkbox" name="item_id" value="003"/>

-------------------------------------------------------------------------
public String deleteitem(String[] item_id)throws Exception{
        System.out.println(item_id);
}
StringArray
<c:forEach items="${itemsList }" var="item" varStatus="s">
<tr>
    <td><input type="text" name="itemsList[${s.index }].name" value="${item.name }"/></td>
    <td><input type="text" name="itemsList[${s.index }].price" value="${item.price }"/></td>
    .....
    .....
</tr>
</c:forEach>
-----------------------------------------------------------------------------------------------------
public class QueryVo {
private List<Items> itemList;//商品列表

  //get/set方法..
}

-----------------------------------------------------------------------------------------------------
public String useraddsubmit(Model model,QueryVo queryVo)throws Exception{
System.out.println(queryVo.getItemList());
}
List
<tr>
<td>學生信息:</td>
<td>
姓名:<inputtype="text"name="itemInfo['name']"/>
年齡:<inputtype="text"name="itemInfo['price']"/>
.. .. ..
</td>
</tr>
----------------------------------------------------------------------------------------
public class QueryVo {
private Map<String, Object> itemInfo = new HashMap<String, Object>();
  //get/set方法..
}
-----------------------------------------------------------------------------------------
public String useraddsubmit(Model model,QueryVo queryVo)throws Exception{
System.out.println(queryVo.getStudentinfo());
}
Map

  

自定義類與包裝類

   咱們在工做中通常使用逆向工程生成工具來進行代碼開發,也就是ORM裏面的實體部分已是自動生成的,這部分的代碼不能動,由於很容易形成代碼混亂,可是咱們又常常須要去修改裏面的部分屬性;

  咱們常常還碰到另一種狀況,就是客戶端請求的多是包含幾個類的視圖(如多值查詢),那麼針對這種狀況,咱們可使用一個包裝類來做爲視圖類,而後返回視圖類來進行響應,而咱們上面也是這麼作的.

  鑑於上述緣由,咱們會使用自定義類來對其進行封裝,使用視圖類來進行傳輸,這樣,咱們就能夠在不碰那些代碼的前提下任意進行代碼修改.

  

  

  

  

編碼問題

  相信你在開發時必定碰到過亂碼問題,SpringMVC也提供了一個統一的編碼過濾器,可是隻適用於POST編碼過濾,這是由於開發中上傳表單數據都規定使用POST,因此SpringMVC也就只提供這一種過濾器.

<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
web.xml

  還有GET的亂碼解決方案有兩種,

  1.修改tomcat配置文件添加編碼與工程編碼一致

  <Connector URIEncoding="utf-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

  2.對參數進行從新編碼  

  String userName = new String(request.getParamter("userName").getBytes("ISO8859-1"),"utf-8");

Validation

  數據校驗通常都會在前端進行,可是有時安全性要求高點的環境下,也會在服務端進行數據校驗.

  服務端校驗

  Controller層  檢驗頁面請求的合法性,在服務端控制層conroller校驗,不區分客戶端類型(瀏覽器、手機客戶端、遠程調用)

  Service層   不區分客戶端類型(瀏覽器、手機客戶端、遠程調用)

  DAO層     通常不進行校驗

  

  SpringMVC使用校驗框架Validation.

  校驗思路:
  頁面提交請求的參數,請求到controller方法中,使用validation進行校驗.若是校驗出錯,將錯誤信息展現到頁面.

  1.導入Bean-Validation框架jar包,Hibernate-validator

  2.配置校驗器

<!-- 校驗器 -->
    <bean id="validator"
        class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <!-- hibernate校驗器-->
        <property name="providerClass" value="org.hibernate.validator.HibernateValidator" />
        <!-- 指定校驗使用的資源文件,在文件中配置校驗錯誤信息,若是不指定則默認使用classpath下的ValidationMessages.properties -->
        <property name="validationMessageSource" ref="messageSource" />
    </bean>
<!-- 校驗錯誤信息配置文件 -->
    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <!-- 資源文件名-->
        <property name="basenames">   
            <list>    
            <value>classpath:CustomValidationMessages</value> 
            </list>   
        </property>
        <!-- 資源文件編碼格式 -->
        <property name="fileEncodings" value="utf-8" />
        <!-- 對資源文件內容緩存時間,單位秒 -->
        <property name="cacheSeconds" value="120" />
    </bean>


<!--註解映射器 -->
    <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> -->
    <!--註解適配器 -->
    <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> -->
    
    <!-- 使用 mvc:annotation-driven代替上邊註解映射器和註解適配器配置
    mvc:annotation-driven默認加載不少的參數綁定方法,
    好比json轉換解析器就默認加載了,若是使用mvc:annotation-driven不用配置上邊的RequestMappingHandlerMapping和RequestMappingHandlerAdapter
    實際開發時使用mvc:annotation-driven
     -->
    <mvc:annotation-driven conversion-service="conversionService"
    validator="validator"></mvc:annotation-driven>
springmvc.xml

  3.在POJO中添加校驗規則.

  

  4.配置校驗時產生的message

  

  5.捕獲校驗錯誤信息

    

  在須要校驗的pojo前邊添加@Validated,在須要校驗的pojo後邊添加BindingResult bindingResult接收校驗出錯信息
  注意:@Validated和BindingResult bindingResult是配對出現,而且形參順序是固定的(一前一後).

  6.controller傳遞錯誤信息

  

  7.頁面顯示錯誤信息

  

  8.校驗分組

  在pojo中定義校驗規則,而pojo是被多個 controller所共用,當不一樣的controller方法對同一個pojo進行校驗,可是每一個controller方法須要不一樣的校驗.能夠指定不一樣的分組來進行校驗.

  

  9.在校驗中添加分組

  

  10.在Controller指定分組的校驗規則

  

數據回顯

  數據回顯指提交數據後出現錯誤,將剛纔提交的數據回顯到剛纔的提交頁面,好比填一張表,填完後一項錯誤後從新回到提交前的狀態.

  1.SpringMVC默認對POJO進行回顯.

  pojo數據傳入controller方法後,springmvc自動將pojo數據放到request域,key等於pojo類型(首字母小寫)

  

  

  2.@ModelAttribute

  @ModelAttribute能夠指定POJO對應的key值,頁面獲取能夠直接使用key值進行獲取,還能夠將返回值存入request域

  

  

  3.直接使用model.addAttribute

  

異常處理器

   異常處理思路很簡單,就是DAO,Service,Controller出現異常直接向上拋出,而後在DispatchServlet那一層設置一個異常處理器就處理全部異常.

  

  1.建立自定義Exception  

package com.harry.exception;

@SuppressWarnings("serial")
public class ExceptionCustom extends Exception {
    
    //異常信息
    public String message;
    
    public ExceptionCustom(String message){
        super(message);
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
ExceptionCustom.java

  2.定義全局異常處理器  

  全局異常處理器處理思路:
    解析出異常類型;
    若是該異常類型是系統自定義的異常直接取出異常信息,在錯誤頁面展現;
    若是該異常類型不是系統自定義的異常,構造一個自定義的異常類(信息爲"未知錯誤");

  實現SpringMVC提供的HandlerExceptionResolver接口

package com.harry.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;


public class ExceptionCustomResolver implements HandlerExceptionResolver {

    /**
     * (非 Javadoc)
     * <p>Title: resolveException</p>
     * <p>Description: </p>
     * @param request
     * @param response
     * @param handler
     * @param ex 系統 拋出的異常
     * @return
     * @see org.springframework.web.servlet.HandlerExceptionResolver#resolveException(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
     */
    @Override
    public ModelAndView resolveException(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex) {
        //handler就是處理器適配器要執行Handler對象(只有method)
        
//        解析出異常類型
//        若是該 異常類型是系統 自定義的異常,直接取出異常信息,在錯誤頁面展現
//        String message = null;
//        if(ex instanceof ExceptionCustom){
//            message = ((ExceptionCustom)ex).getMessage();
//        }else{
////            若是該 異常類型不是系統 自定義的異常,構造一個自定義的異常類型(信息爲「未知錯誤」)
//            message="未知錯誤";
//        }
        
        //上邊代碼變爲
        ExceptionCustom ExceptionCustom = null;
        if(ex instanceof ExceptionCustom){
            ExceptionCustom = (ExceptionCustom)ex;
        }else{
            ExceptionCustom = new ExceptionCustom("未知錯誤");
        }
        
        //錯誤信息
        String message = ExceptionCustom.getMessage();
        
        
        ModelAndView modelAndView = new ModelAndView();
        
        //將錯誤信息傳到頁面
        modelAndView.addObject("message", message);
        
        //指向錯誤頁面
        modelAndView.setViewName("error");

        
        return modelAndView;
    }

}
ExceptionCustomResolver.java

  錯誤頁面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
${message }
</body>
</html>
error.jsp

  springmvc.xml配置全局異常處理器

<!-- 全局異常處理器
    只要實現HandlerExceptionResolver接口就是全局異常處理器
     -->
    <bean class="cn.itcast.ssm.exception.CustomExceptionResolver"></bean>
springmvc.xml

上傳圖片

  1.當提交的表單數據混合幾種數據格式,好比字符串和圖片文件,必須在表單設置提交的壓縮類型爲multipart/form-data.

  

  2.要解析這種壓縮格式的數據須要在springmvc配置文件中添加multipart類型解析器.

<!-- 文件上傳 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 設置上傳文件的最大尺寸爲5MB -->
        <property name="maxUploadSize">
            <value>5242880</value>
        </property>
    </bean>
springmvc.xml

  3.加上springmvc圖片組件jar包

  

  4.建立圖片虛擬目錄存儲圖片

  

  或者在tomcat/conf/server.xml下

  

注意:在圖片虛擬目錄 中,必定將圖片目錄分級建立(提升i/o性能),通常咱們採用按日期(年-月-日)進行分級建立.

   5.上傳頁面部分代碼  

<tr>
    <td>商品圖片</td>
    <td>
        <c:if test="${item.pic !=null}">
            <img src="/pic/${item.pic}" width=100 height=100/>
            <br/>
        </c:if>
        <input type="file"  name="pictureFile"/> 
    </td>
</tr>
editItems.jsp

  6.添加controller映射model,保存圖片路徑名稱

  

  

Json數據交互

   Json數據交互的應用場景普遍,基本上每一個網站都會有這樣的數據交互,特色是數據交互量小,輕便,可異步刷新.

  1.json數據交互思路

  

  客戶端請求json串(或者鍵值對) <--------> 服務端json對象

  2.springmvc採用jackson包來轉換json串與json對象,因此要添加jar包

  

  3.配置json轉換器

<!--註解適配器 -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
        <list>
        <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
        </list>
        </property>
    </bean>

<!--若是使用<mvc:annotation-driven /> 則不用定義上邊的內容 -->
springmvc.xml

  4.測試請求json串和key/value,輸出json串

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>json交互測試</title>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
//請求json,輸出是json
function requestJson(){
    
    $.ajax({
        type:'post',
        url:'${pageContext.request.contextPath }/requestJson.action',
        contentType:'application/json;charset=utf-8',
        //數據格式是json串,商品信息
        data:'{"name":"手機","price":999}',
        success:function(data){//返回json結果
            alert(data);
        }
        
    });
    
    
}
//請求key/value,輸出是json
function requerstKeyValue(){
    
    $.ajax({
        type:'post',
        url:'${pageContext.request.contextPath }/requerstKeyValue.action',
        //請求是key/value這裏不須要指定contentType,由於默認就 是key/value類型
        //contentType:'application/json;charset=utf-8',
        //數據格式是json串,商品信息
        data:'name=手機&price=999',
        success:function(data){//返回json結果
            alert(data.name);
        }
        
    });
    
}
</script>
</head>
<body>
<input type="button" onclick="requestJson()" value="請求json,輸出是json"/>
<input type="button" onclick="requerstKeyValue()" value="請求key/value,輸出是json"/>
</body>
</html>
jsonTest.jsp
package com.harry.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.harry.entity.ItemsCustom;


@Controller
public class JsonController {
    
    //請求json串(商品信息),輸出json(商品信息)
    //@RequestBody將請求的商品信息的json串轉成itemsCustom對象
    //@ResponseBody將itemsCustom轉成json輸出
    @RequestMapping("/requestJson")
    public @ResponseBody ItemsCustom requestJson(@RequestBody ItemsCustom itemsCustom){
        
        //@ResponseBody將itemsCustom轉成json輸出
        return itemsCustom;
    }
    
    //請求key/value,輸出json
    @RequestMapping("/responseJson")
    public @ResponseBody ItemsCustom responseJson(ItemsCustom itemsCustom){
        
        //@ResponseBody將itemsCustom轉成json輸出
        return itemsCustom;
    }

}
JsonController.java

  

RESTful支持

  RESTful是一種開發理念,並不是指具體的技術,理念的闡述具體以下:

  1.對url進行規範,寫RESTful格式的url

  非REST的url:http://...../queryItems.action?id=001&type=T01

  REST的url風格:http://..../items/001

特色:url簡潔,將參數經過url傳到服務端

  2.http的方法規範

  不論是刪除,添加,更新,使用url是一致的,若是進行刪除,須要設置http的方法爲delete,同理添加.

  後臺controller方法:判斷http方法,若是是delete執行刪除,若是是post執行添加。

  3.對http的contentType規範

  請求時指定contentType,要json數據,設置成json格式的type.

 

  示例

  1.controller定義映射使用RESTful風格的url,將查詢信息的id傳入controller.

  

  @RequestMapping(value="/ itemsView/{id}"):{×××}佔位符,請求的URL能夠是「/viewItems/1」或「/viewItems/2」,經過在方法中使用@PathVariable獲取{×××}中的×××變量.

   @PathVariable用於將請求URL中的模板變量映射到功能處理方法的參數上. 若是RequestMapping中表示爲"/ itemsView /{id}",id和形參名稱一致,@PathVariable不用指定名稱.

  2.配置前端控制器

  

  "*.action"的解析在RESTful風格下會致使靜態資源沒法訪問,這裏替換成"/";

  3.springmvc.xml中配置靜態資源映射

  

攔截器

攔截器能設置請求前,響應後進行攔截執行特定的代碼,SpringMVC定義攔截器須要實現HandlerInterceptor接口.

接口中提供三個方法分別在handler執行前,後還有ModelAndView返回前執行

package com.harry.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;


public class HandlerInterceptor1 implements HandlerInterceptor {

    
    //進入 Handler方法以前執行
    //用於身份認證、身份受權
    //好比身份認證,若是認證經過表示當前用戶沒有登錄,須要此方法攔截再也不向下執行
    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        
        System.out.println("HandlerInterceptor1...preHandle");
        
        //return false表示攔截,不向下執行
        //return true表示放行
        return true;
    }

    //進入Handler方法以後,返回modelAndView以前執行
    //應用場景從modelAndView出發:將公用的模型數據(好比菜單導航)在這裏傳到視圖,也能夠在這裏統一指定視圖
    @Override
    public void postHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        
        System.out.println("HandlerInterceptor1...postHandle");
        
    }

    //執行Handler完成執行此方法
    //應用場景:統一異常處理,統一日誌處理
    @Override
    public void afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        
        System.out.println("HandlerInterceptor1...afterCompletion");
    }

}
HanlerInterceptor1.java

  針對HandlerMapping的攔截器(不推薦使用)

  完成攔截器編寫後須要在HandlerMapping處進行定義,畢竟攔截器是映射階段進行工做的.

package com.harry.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;


public class HandlerInterceptor1 implements HandlerInterceptor {

    
    //進入 Handler方法以前執行
    //用於身份認證、身份受權
    //好比身份認證,若是認證經過表示當前用戶沒有登錄,須要此方法攔截再也不向下執行
    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        
        System.out.println("HandlerInterceptor1...preHandle");
        
        //return false表示攔截,不向下執行
        //return true表示放行
        return true;
    }

    //進入Handler方法以後,返回modelAndView以前執行
    //應用場景從modelAndView出發:將公用的模型數據(好比菜單導航)在這裏傳到視圖,也能夠在這裏統一指定視圖
    @Override
    public void postHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        
        System.out.println("HandlerInterceptor1...postHandle");
        
    }

    //執行Handler完成執行此方法
    //應用場景:統一異常處理,統一日誌處理
    @Override
    public void afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        
        System.out.println("HandlerInterceptor1...afterCompletion");
    }

}
springmvc.xml

  全局性攔截器

<!--攔截器 -->
<mvc:interceptors>
    <!--多個攔截器,順序執行 -->
    <!-- 登錄認證攔截器 -->
    <mvc:interceptor>
        <mvc:mapping path="/**"/>
        <bean class="com.harry.interceptor.LoginInterceptor"></bean>
    </mvc:interceptor>
    <mvc:interceptor>
        <!-- /**表示全部url包括子url路徑 -->
        <mvc:mapping path="/**"/>
        <bean class="com.harry.interceptor.HandlerInterceptor1"></bean>
    </mvc:interceptor>
    <mvc:interceptor>
        <mvc:mapping path="/**"/>
        <bean class="com.harry.interceptor.HandlerInterceptor2"></bean>
    </mvc:interceptor>
</mvc:interceptors>
    
springmvc.xml

  攔截器的應用 

  好比:統一日誌處理攔截器,須要該 攔截器preHandle必定要放行,且將它放在攔截器連接中第一個位置.

  好比:登錄認證攔截器,放在攔截器連接中第一個位置.權限校驗攔截器,放在登錄認證攔截器以後.(由於登錄經過後才校驗權限)

  攔截器實現登錄認證

1.處理邏輯

  用戶請求頁面

  攔截器進行攔截

    若是頁面無需訪問權限,放行

    頁面須要訪問權限

      session存在校驗數據,經過

      session無校驗數據,轉入登錄頁面

 2.登錄用Controller方法和請求頁面  

package com.harry.controller;

import javax.servlet.http.HttpSession;

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

@Controller
public class LoginController {

    // 登錄
    @RequestMapping("/login")
    public String login(HttpSession session, String username, String password)
            throws Exception {

        // 調用service進行用戶身份驗證
        // ...

        // 在session中保存用戶身份信息
        session.setAttribute("username", username);
        // 重定向到商品列表頁面
        return "redirect:/items/queryItems.action";
    }

    // 退出
    @RequestMapping("/logout")
    public String logout(HttpSession session) throws Exception {

        // 清除session
        session.invalidate();

        // 重定向到商品列表頁面
        return "redirect:/items/queryItems.action";
    }

}
LoginController.java
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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 }/login.action" method="post">
用戶帳號:<input type="text" name="username" /><br/>
用戶密碼 :<input type="password" name="password" /><br/>
<input type="submit" value="登錄"/>
</form>
</body>
</html>
login.jsp

   3.登錄攔截器和設置

package com.harry.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoginInterceptor implements HandlerInterceptor {

    
    //進入 Handler方法以前執行
    //用於身份認證、身份受權
    //好比身份認證,若是認證經過表示當前用戶沒有登錄,須要此方法攔截再也不向下執行
    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        
        //獲取請求的url
        String url = request.getRequestURI();
        //判斷url是不是公開 地址(實際使用時將公開 地址配置配置文件中)
        //這裏公開地址是登錄提交的地址
        if(url.indexOf("login.action")>=0){
            //若是進行登錄提交,放行
            return true;
        }
        
        //判斷session
        HttpSession session  = request.getSession();
        //從session中取出用戶身份信息
        String username = (String) session.getAttribute("username");
        
        if(username != null){
            //身份存在,放行
            return true;
        }
        
        //執行這裏表示用戶身份須要認證,跳轉登錄頁面
        request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
        
        //return false表示攔截,不向下執行
        //return true表示放行
        return false;
    }

    //進入Handler方法以後,返回modelAndView以前執行
    //應用場景從modelAndView出發:將公用的模型數據(好比菜單導航)在這裏傳到視圖,也能夠在這裏統一指定視圖
    @Override
    public void postHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        
        System.out.println("HandlerInterceptor1...postHandle");
        
    }

    //執行Handler完成執行此方法
    //應用場景:統一異常處理,統一日誌處理
    @Override
    public void afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        
        System.out.println("HandlerInterceptor1...afterCompletion");
    }

}
LoginInterceptor.java
        <!--攔截器 -->
<mvc:interceptors>
    <!--多個攔截器,順序執行 -->
    <!-- 登錄認證攔截器 -->
    <mvc:interceptor>
        <mvc:mapping path="/**"/>
        <bean class="com.harry.interceptor.LoginInterceptor"></bean>
    </mvc:interceptor>
</mvc:interceptors>
springmvc.xml
相關文章
相關標籤/搜索