• 基于SSM框架的简易的分页功能——包含maven项目的搭建


      新人第一次发帖,有什么不对的地方请多多指教~~

        分页这个功能经常会被使用到,我之前学习的时候找了很多资源,可都看不懂(笨死算了),最后还是在朋友帮助下做出了这个分页。我现在把我所能想到的知识

      做了一个整理发上来,供大家参考参考,希望能帮助到一些和我一样的人吧@.@ 中间肯定有很多没有表述明白,请多多见谅,博主刚刚结束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="查看操作记录" style=" 100px; height: 30px;"/> </form> </body> </html>

       分页功能显示页面:

    <%@ 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 style="100px; height: 30px; margin: 10px auto;line-height: 30px;">
            <strong><font style="size:36px;">操作记录</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

  • 相关阅读:
    Vue 入门. 如何在HTML代码里面快速使用Vue
    CSS3 神器总结
    《JavaScript高级程序设计(第3版)》阅读总结记录第二章之在HTML中使用JavaScript
    《JavaScript高级程序设计(第3版)》阅读总结记录第一章之JavaScript简介
    简单轮播的实现
    怎么作好一个领导者?什么是团队?怎么当好一个组员?
    谈BFC和haslayout
    JavaScript 数组
    ubuntu配置android开发环境和编译源码遇到的一些问题
    C语言文件操作函数大全
  • 原文地址:https://www.cnblogs.com/AfricanChief/p/6098853.html
Copyright © 2020-2023  润新知