文章出處

1需求

以案例作為驅動。

SpringMVC和MyBatis使用一個案例(商品訂單管理)。

功能需求:商品列表查詢

2環境準備

數據庫環境:mysql5.6

java環境:

jdk1.7

MyEclipse2014

SpringMVC版本:spring3.2

需要spring3.2所有jar(一定要包括spring-webmvc-3.2.0.RELEASE.jar)

新建一個web項目,將所需jar包拷貝至lib下。

3配置前端控制器

在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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringMVC</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- springmvc前端控制器  -->
  <servlet>
      <servlet-name>springmvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <!-- contextConfigLocation配置springmvc加載的配置文件(該配置文件中配置了處理器映射器、適配器等等) 
           如果不配置contextConfigLocation,默認加載的是/WEB-INF/servlet名稱-servlet.xml(即springmvc-servlet.xml)
      -->
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:springmvc.xml</param-value>
      </init-param>
      <!-- 表示servlet隨服務啟動 -->
      <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
      <servlet-name>springmvc</servlet-name>
      <!--Servlet攔截方式
          方式一:*.action,訪問以.action結尾由DispatcherServlet進行解析
          方式二:/,所有訪問的地址都由DispatcherServlet進行解析,對于靜態文件的解析需要配置不讓DispatcherServlet進行解析。
          使用此方式可以實現RESTful風格的url
          方式三:/*,這樣配置不對,使用這種配置,最終要轉發到一個jsp頁面時,
          仍然會由DispatcherServlet解析jsp地址,不能根據jsp頁面找到handler,會報錯
        -->
      <url-pattern>*.action</url-pattern>
  </servlet-mapping>
  
</web-app>

DispathcerServlet作為springmvc的中央調度器存在,DispatcherServlet創建時會默認從DispatcherServlet.properties文件加載springmvc所用的各個組件,如果在springmvc.xml中配置了組件則以springmvc.xml中配置的為準,DispatcherServlet的存在降低了springmvc各各組件之間的耦合度。

4配置處理器適配器

在classpath下的springmvc.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 ">

    <!-- 處理器適配器
           所有處理器適配器都實現HandlerAdapter接口
     -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

</beans>

通過查看源碼:

此適配器能執行實現Controller接口的Handler。

5開發Handler

需要實現Controller接口,才能由org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter適配器執行。

public class ItemsController1 implements Controller{

    @Override
    public ModelAndView handleRequest(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        //調用service查找數據庫,查詢商品列表,這里使用靜態數據模擬
        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("/WEB-INF/jsp/items/itemsList.jsp");
        return modelAndView;
    }

}

6視圖編寫

 創建/WEB-INF/jsp/order/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 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 }/item/editItem.action?id=${item.id}">修改</a></td>

</tr>
</c:forEach>

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

</html>

7配置Handler

在spring容器中(springmvc.xml)加載編寫的Handler。

  <!-- 配置Handler -->
    <bean name="/queryItems.action" class="joanna.yan.ssm.controller.ItemsController1"/>

8配置處理器映射器

在classpath下的springmvc.xml中配置處理器映射器。

  <!-- 配置處理器映射器 
         將bean的name作為url進行查找,需要在配置Handler時指定beanname(就是url)
    -->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

9配置視圖解析器

需要在springmvc.xml中配置解析jsp的視圖解析器。

 <!-- 配置視圖解析器 
         解析jsp視圖,默認使用jstl標簽,所有classpath下得有jstl的包
    -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>

 

10項目部署調試

將項目部署至Tomcat,訪問http://localhost:8080/SpringMVC/queryItems.action

效果圖:

從目前上面的代碼來看,springmvc中一個類就一個方法,而struts中一個類可以寫多個方法,很不方便。早期確實是這么干的,但是后來SpringMVC提倡用注解開發。

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()