程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> 基於SSM框架的簡易的分頁功能——包含maven項目的搭建,ssmmaven

基於SSM框架的簡易的分頁功能——包含maven項目的搭建,ssmmaven

編輯:JAVA綜合教程

基於SSM框架的簡易的分頁功能——包含maven項目的搭建,ssmmaven


  新人第一次發帖,有什麼不對的地方請多多指教~~

    分頁這個功能經常會被使用到,我之前學習的時候找了很多資源,可都看不懂(笨死算了),最後還是在朋友幫助下做出了這個分頁。我現在把我所能想到的知識

  做了一個整理發上來,供大家參考參考,希望能幫助到一些和我一樣的人吧@.@ 中間肯定有很多沒有表述明白,請多多見諒,博主剛剛結束java 四個月,小白一只,

  還望海涵~~~

  前面八步為項目准備工作,如果maven項目搭建完成的可以跳過~ 

  最後面有附件,是完整的一個工程,包含數據庫的呦

 

 

首先需要一個IDEA!

  我使用的是ideaIU-2016.2 下載地址:http://www.jetbrains.com/idea/download/#section=windows

  IDEA常用配置:http://blog.csdn.net/yelove1990/article/details/51541327

然後創建一個maven項目

  一、Create New Project 創建項目

  

  二、選擇maven項目並按指示創建

  

 

  三、GroupId是項目組織唯一的標識符,實際對應JAVA的包的結構,是main目錄裡java的目錄結構。

    ArtifactId是項目唯一的標識符,實際對應項目的名稱,就是項目根目錄的名稱。

    一般GroupId就是填xxx.xx.xxx類似這樣的。

    

  四、這裡需要添加一條參數,Name是劃線左邊的名稱,value是劃線右邊的值,相應的填寫進去就行,然後點OK-Next

   

  五、創建工程,工程名為前面的ArtifactId

  

   六、然後IDEA就會自動創建項目

    創建完成會有BUILD SUCCESS的字樣提示,表示創建成功。

   七、目錄結構like this

   

 

  八、添加maven依賴太多了,不一一解釋了,教程網址:http://www.yiibai.com/maven/

    直接復制粘貼到自己的maven中即可。

    maven依賴的下載地址:http://mvnrepository.com/

<properties>
    <!-- 核心框架的版本設置 -->
    <spring.version>4.2.5.RELEASE</spring.version>
    <maven.build.timestamp.format>yyyyMMdd</maven.build.timestamp.format>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <downloadSources>true</downloadSources>
  </properties>

  <dependencies>
    <!--common start -->
    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>18.0</version>
    </dependency>

    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.5</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.5</version>
    </dependency>

    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.3.2</version>
    </dependency>

    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.4</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>

    <dependency>
      <groupId>commons-collections</groupId>
      <artifactId>commons-collections</artifactId>
      <version>3.2.1</version>
    </dependency>

    <dependency>
      <groupId>org.quartz-scheduler</groupId>
      <artifactId>quartz</artifactId>
      <version>1.8.5</version>
    </dependency>

    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.8</version>
    </dependency>

    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-validator</artifactId>
      <version>4.2.0.Final</version>
    </dependency>
    <!--common end -->

    <!--spring start -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <!--spring end -->

    <!--springmvc 控制層 start -->
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
    </dependency>

    <dependency>
      <groupId>taglibs</groupId>
      <artifactId>standard</artifactId>
      <version>1.1.2</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.7.4</version>
    </dependency>
    <!--springmvc end -->

    <!--mybatis 數據訪問層 start -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.38</version>
      <scope>runtime</scope>
    </dependency>

    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.0.20</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.0</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.0</version>
    </dependency>

    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.8.0</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>1.6.4.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>com.dyuproject.protostuff</groupId>
      <artifactId>protostuff-core</artifactId>
      <version>1.0.8</version>
    </dependency>

    <dependency>
      <groupId>com.dyuproject.protostuff</groupId>
      <artifactId>protostuff-runtime</artifactId>
      <version>1.0.8</version>
    </dependency>
    <!--mybatis 數據訪問層 end -->

    <!--Commons DBCP 1.4 start-->
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.4</version>
    </dependency>
    <!--Commons DBCP 1.4 end-->


    <dependency>
      <groupId>net.sf.jxls</groupId>
      <artifactId>jxls-core</artifactId>
      <version>1.0.6</version>
    </dependency>
</dependencies>
maven

 

  九、配置tomcat,教程地址如下:

     來自於:chenshun123的博客

       網址:http://blog.csdn.net/chenshun123/article/details/51285890

 

 

 —————————————————————————————————分割線————————————————————————————————————————

 

接下來是正文部分!

  首先需要一個分頁工具類(Page),和實體類(Record),包裝類(seeRecord)。中間內容基本不需要改動,您可以根據需要改動每頁顯示的記錄條數。

  1 package po;
  2 
  3 import java.io.Serializable;
  4 
  5 public class Page implements Serializable{
  6 
  7     private static final long serialVersionUID = -3198048449643774660L;
  8 
  9     private int pageNow = 1; // 當前頁數
 10 
 11     private int pageSize = 5; // 每頁顯示記錄的條數
 12 
 13     private int totalCount; // 總的記錄條數
 14 
 15     private int totalPageCount; // 總的頁數
 16 
 17     @SuppressWarnings("unused")
 18     private int startPos; // 開始位置,從0開始
 19 
 20     @SuppressWarnings("unused")
 21     private boolean hasFirst;// 是否有首頁
 22 
 23     @SuppressWarnings("unused")
 24     private boolean hasPre;// 是否有前一頁
 25 
 26     @SuppressWarnings("unused")
 27     private boolean hasNext;// 是否有下一頁
 28 
 29     @SuppressWarnings("unused")
 30     private boolean hasLast;// 是否有最後一頁
 31 
 32     /**
 33      * 通過構造函數 傳入  總記錄數  和  當前頁
 34      * @param totalCount
 35      * @param pageNow
 36      */
 37     public Page(int totalCount, int pageNow) {
 38         this.totalCount = totalCount;
 39         this.pageNow = pageNow;
 40     }
 41 
 42     /**
 43      * 取得總頁數,總頁數=總記錄數/總頁數
 44      * @return
 45      */
 46     public int getTotalPageCount() {
 47         totalPageCount = getTotalCount() / getPageSize();
 48         return (totalCount % pageSize == 0) ? totalPageCount
 49                 : totalPageCount + 1;
 50     }
 51 
 52     public void setTotalPageCount(int totalPageCount) {
 53         this.totalPageCount = totalPageCount;
 54     }
 55 
 56     public int getPageNow() {
 57         return pageNow;
 58     }
 59 
 60     public void setPageNow(int pageNow) {
 61         this.pageNow = pageNow;
 62     }
 63 
 64     public int getPageSize() {
 65         return pageSize;
 66     }
 67 
 68     public void setPageSize(int pageSize) {
 69         this.pageSize = pageSize;
 70     }
 71 
 72     public int getTotalCount() {
 73         return totalCount;
 74     }
 75 
 76     public void setTotalCount(int totalCount) {
 77         this.totalCount = totalCount;
 78     }
 79     /**
 80      * 取得選擇記錄的初始位置
 81      * @return
 82      */
 83     public int getStartPos() {
 84         return (pageNow - 1) * pageSize;
 85     }
 86 
 87     public void setStartPos(int startPos) {
 88         this.startPos = startPos;
 89     }
 90 
 91     /**
 92      * 是否是第一頁
 93      * @return
 94      */
 95     public boolean isHasFirst() {
 96         return (pageNow == 1) ? false : true;
 97     }
 98 
 99     public void setHasFirst(boolean hasFirst) {
100         this.hasFirst = hasFirst;
101     }
102     /**
103      * 是否有首頁
104      * @return
105      */
106     public boolean isHasPre() {
107         // 如果有首頁就有前一頁,因為有首頁就不是第一頁
108         return isHasFirst() ? true : false;
109     }
110 
111     public void setHasPre(boolean hasPre) {
112         this.hasPre = hasPre;
113     }
114     /**
115      * 是否有下一頁
116      * @return
117      */
118     public boolean isHasNext() {
119         // 如果有尾頁就有下一頁,因為有尾頁表明不是最後一頁
120         return isHasLast() ? true : false;
121     }
122 
123     public void setHasNext(boolean hasNext) {
124         this.hasNext = hasNext;
125     }
126     /**
127      * 是否有尾頁
128      * @return
129      */
130     public boolean isHasLast() {
131         // 如果不是最後一頁就有尾頁
132         return (pageNow == getTotalCount()) ? false : true;
133     }
134 
135     public void setHasLast(boolean hasLast) {
136         this.hasLast = hasLast;
137     }
138 
139 }
page工具類
package po;

public class SeeRecord {

    /*當前登錄的用戶名*/
    private String loginName;

    /*分頁對象*/
    private Page page;

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }
}
seeRecord
package po;

public class Record {
    private int nub;
    private String username;
    private String loginName;
    private String days;
    private String business;
    private String note;

    public int getNub() {
        return nub;
    }

    public void setNub(int nub) {
        this.nub = nub;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getDays() {
        return days;
    }

    public void setDays(String days) {
        this.days = days;
    }

    public String getBusiness() {
        return business;
    }

    public void setBusiness(String business) {
        this.business = business;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }
}
Record

 

  然後編寫Mybatis的Recordmapper.xml配置文件,編寫sql語句

<?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="ssm.dao.RecordDao" ><!--對應相應的DAO接口-->
    <!--查詢操作記錄-->
    <select id="queryAllRecord" parameterType="ssm.po.SeeRecord" resultType="ssm.po.Record">
        SELECT * FROM record WHERE loginName = #{loginName} limit #{page.startPos},#{page.pageSize}<!--startPos開始頁 , pageSize每頁顯示的記錄條數-->
    </select>

    <!--查詢總記錄條數-->
    <select id="getCount" resultType="int" parameterType="String">
        select count(*) from record where loginName = #{value}
    </select>
</mapper>

  接下來編寫Dao接口(RecordDao)

package Dao;

import org.springframework.stereotype.Repository;
import po.Record;
import po.SeeRecord;
import java.util.List;

@Repository
public interface RecordDao {
    /*取得操作記錄*/
    List<Record> queryAllRecord(SeeRecord seeRecord);

    /*取得記錄條數信息,通過當前用戶的loginName查詢*/
    int getCount(String loginName);
}

  接口編寫完成後需要慌裡面傳值,此時就需要編寫Controller。

    先獲取到當前頁和用戶名,然後調用DAO接口查詢數據庫,獲得總記錄條數,把總記錄條數和當前頁傳入到page的構造方法中,page工具類會根據傳入的參數,算出共幾頁。然後查出當前用戶的所有操作記錄,存到recordList中並返回給頁面,頁面就可以顯示了。

package controller;

import Dao.RecordDao;
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 po.Page;
import po.Record;
import po.SeeRecord;


import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class RecordController {

    @Autowired
    private RecordDao recordDao;

    /*查看操作記錄*/
    @RequestMapping("/queryAll")
    public String record(String loginName , Model model, SeeRecord seeRecord, String pageNow){
        String user_Name;
        Page page = null;//new 對象在下面
        user_Name = loginName;//獲取當前登錄的用戶的loginName
        int totalCount = recordDao.getCount(user_Name);//獲取當前用戶總記錄條數
        if(pageNow != null && pageNow != "") {//防止出現空指針異常
            int pageNow1 = Integer.parseInt(pageNow);
            page = new Page(totalCount, pageNow1);//這樣寫的好處,判斷完成外面可以繼續調用
        }
        seeRecord.setLoginName(user_Name);
        seeRecord.setPage(page);
        List<Record> recordList = recordDao.queryAllRecord(seeRecord);//取得當前用戶的所有操作記錄
        model.addAttribute("page",page);
        model.addAttribute("recordList",recordList);
        return "recordPage";
    }
}

  

編寫jsp頁面接收並顯示。

由於測試需要,我加了一個跳轉頁面。向Controller中傳入了兩個值,pageNow和loginName,表示當前登錄的用戶和當前的頁數。

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用於跳轉到分頁頁面</title>
</head>
<body>
    <form action="/queryAll?pageNow=1&loginName='ceshiyong01'" method="post">
<input type="submit" value="查看操作記錄">分頁功能顯示頁面:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
    <title>操作記錄</title>
    <style type="text/css">
        body{
            margin: 0px;
            padding: 0px;
        }
        table{
            width: 100%;
            height: 400px;
            border: #000 1px solid;
        }
        tr td{
            height: 40px;
            text-align: center;
            border: #000 1px solid;
        }

    </style>
</head>
<body>
    <div >
        <strong><font >操作記錄</font></strong>
    </div>
    <table>
        <tr>
            <td width="70px">流水號</td>
            <td width="90px">姓名</td>
            <td width="120px">用戶名</td>
            <td width="200px">操作日期</td>
            <td width="250px">操作記錄</td>
            <td width="80px">操作方式</td>
        </tr>
        <!--循環遍歷所有得到的記錄並輸出-->
        <c:forEach items="${recordList}" var="record">
        <tr>
            <td width="70px">${record.nub}</td>
            <td width="90px">${record.username}</td>
            <td width="120px">${record.loginName}</td>
            <td width="200px">${record.days}</td>
            <td width="250px">${record.business}</td>
            <td width="80px">${record.note}</td>
        </tr>
        </c:forEach>
        <tr>
            <td colspan="6">
            <!-- 分頁功能 start -->
            <div align="center">
                <font size="2">共 ${page.totalPageCount} 頁</font> <font size="2">第
                ${page.pageNow} 頁</font> <a href="${pageContext.request.contextPath}/queryAll?pageNow=1">首頁</a>
                <c:choose>
                    <c:when test="${page.pageNow - 1 > 0}">
                        <a href="${pageContext.request.contextPath}/queryAll?pageNow=${page.pageNow - 1}">上一頁</a>
                    </c:when>
                    <c:when test="${page.pageNow - 1 <= 0}">
                        <a href="${pageContext.request.contextPath}/queryAll?pageNow=1">上一頁</a>
                    </c:when>
                </c:choose>
                <c:choose>
                    <c:when test="${page.totalPageCount==0}">
                        <a href="${pageContext.request.contextPath}/queryAll?pageNow=${page.pageNow}">下一頁</a>
                    </c:when>
                    <c:when test="${page.pageNow + 1 < page.totalPageCount}">
                        <a href="${pageContext.request.contextPath}/queryAll?pageNow=${page.pageNow + 1}">下一頁</a>
                    </c:when>
                    <c:when test="${page.pageNow + 1 >= page.totalPageCount}">
                        <a href="${pageContext.request.contextPath}/queryAll?pageNow=${page.totalPageCount}">下一頁</a>
                    </c:when>
                </c:choose>
                <c:choose>
                    <c:when test="${page.totalPageCount==0}">
                        <a href="${pageContext.request.contextPath}/queryAll?pageNow=${page.pageNow}">尾頁</a>
                    </c:when>
                    <c:otherwise>
                        <a href="${pageContext.request.contextPath}/queryAll?pageNow=${page.totalPageCount}">尾頁</a>
                    </c:otherwise>
                </c:choose>
            </div>
            <!-- 分頁功能 End -->
            </td>
        </tr>
    </table>
</body>
</html>

 

—————————————————————————————分割線2號———————————————————————————————————————————

最後配置XML文件:

  db.properties:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/pagedemo?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456

  

  log4j.properties:

log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

  

  spring-mvc.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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-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/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
       <!--自動掃描 @ssm.com.imarce.pageDemo.controller-->
        <context:component-scan base-package="com.imarce.pageDemo.controller"/>


        <!-- 啟動對@AspectJ注解的支持 -->
        <aop:aspectj-autoproxy proxy-target-class="true" />

        <!--定義跳轉的文件的前後綴,視圖模式設置-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
            <property name="prefix" value=""/>
            <property name="suffix" value=".jsp"/>
        </bean>

    <mvc:annotation-driven>
        <!-- 將StringHttpMessageConverter的默認編碼設為UTF-8 -->
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8"/>
            </bean>
            <!-- 將Jackson2HttpMessageConverter的默認格式化輸出為false -->
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list><value>application/json;charset=UTF-8</value></list>
                </property>
                <property name="prettyPrint" value="false"/>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <!--靜態資源默認servlet配置
        1、加入對靜態資源的處理:js,css,gif,png
        2、允許使用"/"做整體映射  <url-pattern>/</url-pattern>
    -->
    <mvc:default-servlet-handler/>

</beans>

  

  spring-mybatis.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:context="http://www.springframework.org/schema/context" 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.1.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                        http://www.springframework.org/schema/tx  
                        http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--自動掃描包-->
    <context:component-scan base-package="com.imarce.pageDemo"/>

    <!-- 加載properties文件 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:db.properties"/>
    </bean>

    <!-- 配置數據源 -->
    <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}"/>
    </bean>

    <!-- mybatis和spring完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 自動掃描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath:mapping/*.xml"/>
    </bean>

    <!-- DAO接口所在包名,Spring會自動查找其下的類 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.imarce.pageDemo.Dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>
</beans>

 

  web.xml: 這裡有個問題,創建完maven項目自動生成的web.xml頭部版本可能是2.3左右的,如果版本低於2.4,會導致jsp頁面的EL表達式失效。只要把頭部改到2.4以上即可。

  詳見:http://blog.csdn.net/hanshileiai/article/details/46619777

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

  <!-- spring mvc servlet-->
  <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:spring-*.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
  </servlet>

  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.properties</param-value>
  </context-param>

  <!-- 編碼過濾器 -->
  <filter>
    <filter-name>encodingFilter</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>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!-- 防止spring內存溢出監聽器,比如quartz -->
  <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    <!--使用request可以使用-->
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>


  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <!-- 此處也可以配置成 *.do 形式 -->
    <url-pattern>/</url-pattern>
  </servlet-mapping>


  <welcome-file-list>
    <welcome-file>transferPage.jsp</welcome-file>
  </welcome-file-list>

  <display-name>Archetype Created Web Application</display-name>
</web-app>

  

效果圖如下:

 

工程文件下載:http://pan.baidu.com/s/1pLNcykb

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved