• maven+springmvc+easyui+fastjson+pagehelper


    1、maven配置

      1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      3     <modelVersion>4.0.0</modelVersion>
      4     <groupId>com.jdw</groupId>
      5     <artifactId>ssm</artifactId>
      6     <packaging>war</packaging>
      7     <version>0.0.1-SNAPSHOT</version>
      8     <name>ssm Maven Webapp</name>
      9     <properties>
     10         <!-- spring版本号 -->
     11         <spring.version>4.2.5.RELEASE</spring.version>
     12         <!-- mybatis版本号 -->
     13         <mybatis.version>3.2.6</mybatis.version>
     14         <!-- log4j日志文件管理包版本 -->
     15         <slf4j.version>1.7.7</slf4j.version>
     16         <log4j.version>1.2.17</log4j.version>
     17         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     18     </properties>
     19 
     20     <dependencies>
     21         <dependency>
     22             <groupId>junit</groupId>
     23             <artifactId>junit</artifactId>
     24             <version>4.11</version>
     25             <scope>test</scope>
     26         </dependency>
     27         <!-- spring核心包 -->
     28         <dependency>
     29             <groupId>org.springframework</groupId>
     30             <artifactId>spring-core</artifactId>
     31             <version>${spring.version}</version>
     32         </dependency>
     33 
     34         <dependency>
     35             <groupId>org.springframework</groupId>
     36             <artifactId>spring-web</artifactId>
     37             <version>${spring.version}</version>
     38         </dependency>
     39         <dependency>
     40             <groupId>org.springframework</groupId>
     41             <artifactId>spring-oxm</artifactId>
     42             <version>${spring.version}</version>
     43         </dependency>
     44         <dependency>
     45             <groupId>org.springframework</groupId>
     46             <artifactId>spring-tx</artifactId>
     47             <version>${spring.version}</version>
     48         </dependency>
     49 
     50         <dependency>
     51             <groupId>org.springframework</groupId>
     52             <artifactId>spring-jdbc</artifactId>
     53             <version>${spring.version}</version>
     54         </dependency>
     55 
     56         <dependency>
     57             <groupId>org.springframework</groupId>
     58             <artifactId>spring-webmvc</artifactId>
     59             <version>${spring.version}</version>
     60         </dependency>
     61         <dependency>
     62             <groupId>org.springframework</groupId>
     63             <artifactId>spring-aop</artifactId>
     64             <version>${spring.version}</version>
     65         </dependency>
     66 
     67         <dependency>
     68             <groupId>org.springframework</groupId>
     69             <artifactId>spring-context-support</artifactId>
     70             <version>${spring.version}</version>
     71         </dependency>
     72 
     73         <dependency>
     74             <groupId>org.springframework</groupId>
     75             <artifactId>spring-test</artifactId>
     76             <version>${spring.version}</version>
     77         </dependency>
     78         <dependency>
     79             <groupId>aspectj</groupId>
     80             <artifactId>aspectjweaver</artifactId>
     81             <version>1.5.4</version>
     82         </dependency>
     83         <!-- mybatis核心包 -->
     84         <dependency>
     85             <groupId>org.mybatis</groupId>
     86             <artifactId>mybatis</artifactId>
     87             <version>${mybatis.version}</version>
     88         </dependency>
     89         <!-- mybatis/spring包 -->
     90         <dependency>
     91             <groupId>org.mybatis</groupId>
     92             <artifactId>mybatis-spring</artifactId>
     93             <version>1.2.2</version>
     94         </dependency>
     95         <!-- 导入java ee jar 包 -->
     96         <dependency>
     97             <groupId>javax</groupId>
     98             <artifactId>javaee-api</artifactId>
     99             <version>7.0</version>
    100         </dependency>
    101         <!-- pagehelper分页 -->
    102         <dependency>
    103             <groupId>com.github.pagehelper</groupId>
    104             <artifactId>pagehelper</artifactId>
    105             <version>4.1.3</version>
    106         </dependency>
    107         <!-- 导入Mysql数据库链接jar包 -->
    108         <dependency>
    109             <groupId>mysql</groupId>
    110             <artifactId>mysql-connector-java</artifactId>
    111             <version>5.1.30</version>
    112         </dependency>
    113         <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
    114         <dependency>
    115             <groupId>commons-dbcp</groupId>
    116             <artifactId>commons-dbcp</artifactId>
    117             <version>1.2.2</version>
    118         </dependency>
    119         <!-- JSTL标签类 -->
    120         <dependency>
    121             <groupId>jstl</groupId>
    122             <artifactId>jstl</artifactId>
    123             <version>1.2</version>
    124         </dependency>
    125         <!-- 日志文件管理包 -->
    126         <!-- log start -->
    127         <dependency>
    128             <groupId>log4j</groupId>
    129             <artifactId>log4j</artifactId>
    130             <version>${log4j.version}</version>
    131         </dependency>
    132 
    133 
    134         <!-- 格式化对象,方便输出日志 -->
    135         <dependency>
    136             <groupId>com.alibaba</groupId>
    137             <artifactId>fastjson</artifactId>
    138             <version>1.2.5</version>
    139         </dependency>
    140 
    141         <dependency>
    142             <groupId>org.slf4j</groupId>
    143             <artifactId>slf4j-api</artifactId>
    144             <version>${slf4j.version}</version>
    145         </dependency>
    146 
    147         <dependency>
    148             <groupId>org.slf4j</groupId>
    149             <artifactId>slf4j-log4j12</artifactId>
    150             <version>${slf4j.version}</version>
    151         </dependency>
    152         <!-- log end -->
    153 
    154         <!-- 上传组件包 -->
    155         <dependency>
    156             <groupId>commons-fileupload</groupId>
    157             <artifactId>commons-fileupload</artifactId>
    158             <version>1.3.1</version>
    159         </dependency>
    160         <dependency>
    161             <groupId>commons-io</groupId>
    162             <artifactId>commons-io</artifactId>
    163             <version>2.4</version>
    164         </dependency>
    165         <dependency>
    166             <groupId>commons-codec</groupId>
    167             <artifactId>commons-codec</artifactId>
    168             <version>1.9</version>
    169         </dependency>
    170     </dependencies>
    171     <build>
    172         <finalName>ssm</finalName>
    173         <plugins>
    174             <plugin>
    175                 <groupId>org.apache.maven.plugins</groupId>
    176                 <artifactId>maven-compiler-plugin</artifactId>
    177                 <version>3.1</version>
    178                 <configuration>
    179                     <source>1.7</source>
    180                     <target>1.7</target>
    181                     <encoding>UTF8</encoding>
    182                 </configuration>
    183             </plugin>
    184         </plugins>
    185     </build>
    186 </project>
    View Code

    2、spring-mybatis.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
     4     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
     5     xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
     6         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     7         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
     8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
     9     <!-- 自动扫描 -->
    10     <context:component-scan base-package="com.jdw" />
    11     <!-- 引入配置文件 -->
    12     <bean id="propertyConfigurer"
    13         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    14         <property name="location" value="classpath:jdbc.properties" />
    15     </bean>
    16 
    17     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    18         destroy-method="close">
    19         <property name="driverClassName" value="${driver}" />
    20         <property name="url" value="${url}" />
    21         <property name="username" value="${username}" />
    22         <property name="password" value="${password}" />
    23         <!-- 初始化连接大小 -->
    24         <property name="initialSize" value="${initialSize}"></property>
    25         <!-- 连接池最大数量 -->
    26         <property name="maxActive" value="${maxActive}"></property>
    27         <!-- 连接池最大空闲 -->
    28         <property name="maxIdle" value="${maxIdle}"></property>
    29         <!-- 连接池最小空闲 -->
    30         <property name="minIdle" value="${minIdle}"></property>
    31         <!-- 获取连接最大等待时间 -->
    32         <property name="maxWait" value="${maxWait}"></property>
    33     </bean>
    34 
    35     <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    36     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    37         <property name="dataSource" ref="dataSource" />
    38         <!-- 
    39         <property name="configLocation" value="classpath:SqlMapConfig.xml" /> -->
    40         <!-- 自动扫描mapping.xml文件 -->
    41         <property name="mapperLocations" value="classpath:com/jdw/mapping/*.xml"></property>
    42  
    43         <property name="plugins">
    44             <array>
    45                 <bean class="com.github.pagehelper.PageHelper">
    46                     <property name="properties">
    47                         <value>
    48                             dialect=mysql
    49                         </value>
    50                     </property>
    51                 </bean>
    52             </array>
    53         </property>
    54     </bean>
    55 
    56     <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    57     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    58         <property name="basePackage" value="com.jdw.dao" />
    59         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    60     </bean>
    61 
    62     <!-- ================================事务相关控制================================================= -->
    63     <bean name="transactionManager"
    64         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    65         <property name="dataSource" ref="dataSource"></property>
    66     </bean>
    67 
    68     <tx:advice id="userTxAdvice" transaction-manager="transactionManager">
    69         <tx:attributes>
    70             <tx:method name="delete*" propagation="REQUIRED" read-only="false"
    71                 rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException" />
    72             <tx:method name="insert*" propagation="REQUIRED" read-only="false"
    73                 rollback-for="java.lang.RuntimeException" />
    74             <tx:method name="update*" propagation="REQUIRED" read-only="false"
    75                 rollback-for="java.lang.Exception" />
    76 
    77             <tx:method name="find*" propagation="SUPPORTS" />
    78             <tx:method name="get*" propagation="SUPPORTS" />
    79             <tx:method name="select*" propagation="SUPPORTS" />
    80         </tx:attributes>
    81     </tx:advice>
    82 
    83     <aop:config>
    84         <aop:pointcut id="pc"
    85             expression="execution(public * com.jdw.service.*.*(..))" /> <!--把事务控制在Service层 -->
    86         <aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" />
    87     </aop:config>
    88 
    89 </beans>
    View Code

    3、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"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
        <!-- <mvc:view-controller path="/" view-name="index" /> -->
    
        <mvc:default-servlet-handler />
    
        <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
        <context:component-scan base-package="com.jdw.controller" />
    
    
        <!-- 启用默认配置 -->
        <mvc:annotation-driven>
            <mvc:message-converters register-defaults="true">
                <!-- 配置Fastjson支持 -->
                <bean
                    class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                    <property name="supportedMediaTypes"  >
                        <array>
                            <value>text/html;charset=UTF-8</value>
                            <value>application/json</value>
                        </array>
                    </property>
                    <property name="features">
                        <array>
                            <value>WriteMapNullValue</value>
                            <value>WriteNullStringAsEmpty</value>
                        </array>
                    </property>
                </bean>
            </mvc:message-converters>
        </mvc:annotation-driven>
    
        <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
            <property name="prefix" value="/WEB-INF/" />
            <property name="suffix" value=".jsp" />
        </bean>
    
        <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
        <bean id="multipartResolver"
            class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <!-- 默认编码 -->
            <property name="defaultEncoding" value="utf-8" />
            <!-- 文件大小最大值 -->
            <property name="maxUploadSize" value="10485760000" />
            <!-- 内存中的最大值 -->
            <property name="maxInMemorySize" value="40960" />
        </bean>
    </beans>
    View Code

    4、service代码

    @Override
        public Map<String,Object> query(String type, Integer pageNo, Integer pageSize) {
            pageNo = pageNo == null?1:pageNo;
            pageSize = pageSize == null?10:pageSize;
            PageHelper.startPage(pageNo, pageSize);
            List<Category> list = this.dao.selectCategoryByType(type);
            for(Category c:list)
            {
                System.out.println(c);
            }
            //用PageInfo对结果进行包装
            PageInfo<Category> page = new PageInfo<Category>(list);
           
            //测试PageInfo全部属性
            /*
            System.out.println(page.getPageNum());
            System.out.println(page.getPageSize());
            System.out.println(page.getStartRow());
            System.out.println(page.getEndRow());
            System.out.println(page.getTotal());
            System.out.println(page.getPages());
            System.out.println(page.getFirstPage());
            System.out.println(page.getLastPage());
            System.out.println(page.isHasPreviousPage());
            System.out.println(page.isHasNextPage());*/
            Map<String, Object> map=new HashMap<String, Object>();
            map.put("total", page.getTotal());
            map.put("rows", page.getList());
            return map;
        }
    View Code

    5、controller代码

        @ResponseBody
        @RequestMapping(value = "/list")
        public Map<String, Object> getCategories(HttpServletRequest request,HttpServletResponse response) {
            int page=Integer.parseInt(request.getParameter("page"));
            int rows=Integer.parseInt(request.getParameter("rows"));
            String type=request.getParameter("type");
            return categoryService.query(type, page, rows);
        }

    6、easyui调用

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <%@ include file="/public/head.jspf"%>
    <style type="text/css">
    body {
        margin: 1px;
    }
    
    .searchbox {
        margin: -3;
    }
    </style>
    <script type="text/javascript">
        $(function() {
            $('#dg')
                    .datagrid(
                            {
                                url : '${shop}/category/list',
                                //250,
                                idField : 'id',
                                pageSize : 5,
                                pageList : [ 5, 10, 15, 20, 25 ],
                                queryParams : {
                                    type : ''
                                },
                                striped : true,
                                nowrap : true,
                                fitColumns : true,
                                singleSelect : false,
                                pagination : true,
                                toolbar : [
                                        {
                                            iconCls : 'icon-add',
                                            text : '添加类别',
                                            handler : function() {
                                                parent
                                                        .$("#win")
                                                        .window(
                                                                {
                                                                    title : '添加类别',
                                                                    width : 400,
                                                                    height : 300,
                                                                    content : '<iframe src="send_category_add.action" frameborder="0" width="100%" height="100%" />'
                                                                });
                                            }
                                        },
                                        '-',
                                        {
                                            iconCls : 'icon-edit',
                                            text : '更新类别',
                                            handler : function() {
                                                var rows = $("#dg").datagrid(
                                                        "getSelections");
                                                if (rows.length != 1) {
                                                    $.messager.show({
                                                        title : '错误提示',
                                                        msg : '一次只能更新一条记录。',
                                                        timeout : 5000,
                                                        showType : 'slide'
                                                    });
                                                } else {
                                                    parent
                                                            .$("#win")
                                                            .window(
                                                                    {
                                                                        title : '更新类别',
                                                                        width : 400,
                                                                        height : 240,
                                                                        content : '<iframe src="send_category_update.action" frameborder="0" width="100%" height="100%" />'
                                                                    });
                                                }
                                            }
                                        },
                                        '-',
                                        {
                                            iconCls : 'icon-remove',
                                            text : '删除类别',
                                            handler : function() {
                                                var rows = $("#dg").datagrid(
                                                        "getSelections");
                                                if (rows.length == 0) {
                                                    $.messager.show({
                                                        title : '错误提示',
                                                        msg : '至少选中一条记录。',
                                                        timeout : 5000,
                                                        showType : 'slide'
                                                    });
    
                                                } else {
                                                    $.messager
                                                            .confirm(
                                                                    '删除确认对话框',
                                                                    '是否删除选中记录?',
                                                                    function(r) {
                                                                        if (r) {
                                                                            var ids = "";
                                                                            for (var i = 0; i < rows.length; i++) {
                                                                                ids += rows[i].id
                                                                                        + ",";
                                                                            }
                                                                            ids = ids
                                                                                    .substring(
                                                                                            0,
                                                                                            ids
                                                                                                    .lastIndexOf(','));
                                                                            $
                                                                                    .post(
                                                                                            "category_deleteByIds.action",
                                                                                            {
                                                                                                ids : ids
                                                                                            },
                                                                                            function(
                                                                                                    result) {
                                                                                                if (result == "true") {
                                                                                                    //取消选中的所有行
                                                                                                    $(
                                                                                                            "#dg")
                                                                                                            .datagrid(
                                                                                                                    'uncheckAll');
                                                                                                    $(
                                                                                                            "#dg")
                                                                                                            .datagrid(
                                                                                                                    'reload');
                                                                                                } else {
                                                                                                    $.messager
                                                                                                            .show({
                                                                                                                title : '删除异常',
                                                                                                                msg : '删除失败,请检查操作。',
                                                                                                                timeout : 5000,
                                                                                                                showType : 'slide'
                                                                                                            });
                                                                                                }
                                                                                            },
                                                                                            'text');
                                                                        }
                                                                    });
    
                                                }
                                            }
                                        },
                                        '-',
                                        {
    
                                            text : "<input id='ss' name='search' />"
    
                                        } ],
                                rowStyler : function(index, row) {
                                    if (index % 2 == 0) {
                                        return 'background-color:#6293BB;color:#fff;';
                                    }
                                },
                                frozenColumns : [ [ {
                                    field : 'xyz',
                                    checkbox : true
                                }, {
                                    field : 'cId',
                                    title : '编号',
                                    width : 100
                                } ] ],
                                columns : [ [
    
                                        {
                                            field : 'cName',
                                            title : '类别',
                                            width : 100,
                                            formatter : function(value, row, index) {
                                                return "<span title="+value+">"
                                                        + value + "</span>";
                                            }
                                        },
                                        {
                                            field : 'hot',
                                            title : '热点',
                                            width : 100,
                                            align : 'center',
                                            formatter : function(value, row, index) {
                                                if (value)
                                                    return "<input type='checkbox' checked disabled='true' />";
                                                else
                                                    return "<input type='checkbox' disabled='true' />";
                                            }
                                        /*
                                        styler : function(value, row, index) {
                                            if (value < 20) {
                                                return 'background-color:#ffee00;color:red;';
                                            }
                                        }*/
                                        }
    
                                ] ]
                            });
    
            $('#ss').searchbox({
                searcher : function(value, name) {
                    $('#dg').datagrid('load', {
                        type : value
                    });
                },
                prompt : 'sssssss'
            });
    
        });
    </script>
    </head>
    <body>
        <table id="dg"></table>
    
    </body>
    </html>
    View Code

     7、总结说明

      (1)注意bean的getter、setter写法,不能这么写getcName,要这么写getCName,否则fastjson会找不到cName属性

      (2)暂时不要用fastjson 1.2.10,有bug.会抛出 java.lang.AbstractMethodError异常

      (3)<mvc:default-servlet-handler />处理静态资源,不要用mvc:resources,不好用,容易出问题;

      (4)静态资源最好用如下方式:

    head.jspf:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <c:set value="${pageContext.request.contextPath }" var="shop" />
    <title>my website</title>
    <link rel="stylesheet"
        href="${shop}/jquery-easyui-1.3.5/themes/default/easyui.css" type="text/css" />
    <link rel="stylesheet" href="${shop}/jquery-easyui-1.3.5/themes/icon.css"
        type="text/css" />
    <script type="text/javascript"
        src="${shop}/jquery-easyui-1.3.5/jquery.min.js"></script>
    <script type="text/javascript"
        src="${shop}/jquery-easyui-1.3.5/jquery.easyui.min.js"></script>
    <script type="text/javascript"
        src="${shop}/jquery-easyui-1.3.5/locale/easyui-lang-zh_CN.js"></script>
    View Code

    其他页面:

    <head>
    <%@ include file="/public/head.jspf"%>
    </head>

      

  • 相关阅读:
    Dom4j使用Xpath语法读取xml节点
    XML新手入门 创建构造良好的XML(2)
    XML新手入门 创建构造良好的XML(1)
    详解Java解析XML的四种方法
    Java中加载配置文件的集中方式,以及利用ClassLoader加载文件 .
    javaSE读取Properties文件的六种方法
    select
    socket
    socket异步通信-如何设置成非阻塞模式、非阻塞模式下判断connect成功(失败)、判断recv/recvfrom成功(失败)、判断send/sendto
    grep 多模式匹配
  • 原文地址:https://www.cnblogs.com/sdnu/p/5452577.html
Copyright © 2020-2023  润新知