DWZ (JUI) 应用实例(一):通过自定义标签和BaseAction 简化 table 分页排序

本文主要是介绍DWZ (JUI) 应用实例(一):通过自定义标签和BaseAction 简化 table 分页排序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

前段时间忙于做项目,在使用DWZ过程中,发现有些代码的重复度挺大,所以工作空闲之余,写一下分页排序的Demo,并且使用自定义标签简化重复的代码。(发现越来越多的人关注该框架了,因为是国产,可以更快的上手)


源码下载

http://download.csdn.net/detail/tcl_6666/6680323


分页思路

服务器返回当前页的数据,总条数,再由js来生成分页标签。分页是配合服务器端来处理的, 不是纯js做的分页。

    因为如果数据量很大,比如有好几百页,存js分页就是悲剧了,纯js分页是必须一次载入所有数据,性能很慢。

 

通过自定义标签,封装原有的pagerForm

对于DWZ的使用者,众所周知下面是分页的主要参数,这些参数都在放在pagerForm中的

targetType:navTab或dialog,用来标记是navTab上的分页还是dialog上的分页

totalCount: 总条数        

numPerPage: 每页显示多少条

pageNumShown: 页标数字多少个

currentPage: 当前是第几页

 

通过自定义标签,封装原有的pagerForm

paginationForm.tag

<%@tag language="java" pageEncoding="UTF-8"body-content="scriptless" trimDirectiveWhitespaces="true"%><!--自定义标签 属性 -->
<%@attribute name="action" type="java.lang.String"required="true"%>
<%@attribute name="onsubmit" type="java.lang.String"%><%@taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core"%><formid="pagerForm" method="post" action="${action }"
<c:iftest="${onsubmit!=null}"> οnsubmit="${onsubmit}"</c:if>>
<inputtype="hidden" name="pageNum"value="${pageNum}"/>
<inputtype="hidden" name="numPerPage"value="${numPerPage}"/>
<inputtype="hidden" name="orderField"value="${orderField}"/>
<inputtype="hidden" name="orderDirection"value="${orderDirection}"/>
<jsp:doBody/>
</form>

 

自定义标签,封装原有的<divclass="pages">和<div class="pagination"

pagination.tag

<%@tag language="java" pageEncoding="UTF-8"trimDirectiveWhitespaces="true"%>
<%@attribute name="page" type="com.dwz.util.Page"required="true"%>
<%@attribute name="pageNumShown" type="java.lang.Integer"%>
<%@attribute name="begin" type="java.lang.Integer"%>
<%@attribute name="end" type="java.lang.Integer"%>
<%@attribute name="step" type="java.lang.Integer"%>
<%@attribute name="target" type="java.lang.String"%>
<%@attribute name="rel" type="java.lang.String"%>
<%@attribute name="onchange" type="java.lang.String"%><%@taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core"%><c:iftest="${pageNumShown == null}" >
<c:setvar="pageNumShown" value="10"/>
</c:if>
<c:iftest="${begin == null}" >
<c:setvar="begin" value="10"/>
</c:if>
<c:iftest="${end == null}" >
<c:setvar="end" value="50"/>
</c:if>
<c:iftest="${step == null}" >
<c:setvar="step" value="5"/>
</c:if>
<c:iftest="${target == null}" >
<c:setvar="target" value="navTab"/>
</c:if>
<c:iftest="${onchange == null}" >
<c:setvar="onchange"value="navTabPageBreak({numPerPage:this.value})"/>
</c:if><divclass="panelBar">
<divclass="pages">
<span>每页显示</span>
<selectname="numPerPage" οnchange="${onchange}">
<c:forEachbegin="${begin }" end="${end }" step="${step }"varStatus="s">
<c:choose>
<c:whentest="${numPerPage==s.index }">
<optionvalue="${s.index}"selected="selected">${s.index}</option>
</c:when>
<c:whentest="${numPerPage!=s.index }">
<optionvalue="${s.index}">${s.index}</option>
</c:when>
</c:choose>
</c:forEach>                                                
</select>
<span>总条数:${totalCount}</span>
</div>
<div<c:if test="${rel!=null }">rel="${rel}" </c:if>class="pagination" targetType="${target}"totalCount="${totalCount}" numPerPage="${numPerPage}"pageNumShown="${pageNumShown}"currentPage="${pageNum}"></div>
</div>


 

注意:

在自定义jsp标签时,tag文件默认需要放到WEB-INF目录下

 

引入自定义标签和jstl

include.inc.jsp

<%@page 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"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions"prefix="fn"%><%@taglib prefix="custom" tagdir="/WEB-INF/tags" %><c:setvar="contextPath"value="${pageContext.request.contextPath}"/>

userList中可以看到简化后的效果

<%@pagecontentType="text/html; charset=utf-8"%><%@include file="/page/common/include.inc.jsp"%><!-- 自定义标签封装pagerForm,简化分页 -->
<custom:paginationForm action="${contextPath}/user!userList.action">
<input type="hidden" name="keywords"value="${keywords }"/>
</custom:paginationForm><formmethod="post" action="${contextPath }/user!userList.action"οnsubmit="return navTabSearch(this)">
<divclass="pageHeader">
<divclass="searchBar"><label>登录名称:</label><inputtype="text" name="keywords" value="${keywords}"><buttontype="submit">检索</button>
</div>
</div>
</form><divclass="pageContent">
<divclass="panelBar">
<ulclass="toolBar">
<li><aclass="add" href="user!dosave.action"target="dialog"mask="true"><span>添加</span></a></li><!-- 用于数组传送数据 --><li><atitle="确实要删除这些记录吗?" target="selectedTodo"rel="ids" href="user!del.action"class="delete"><span>删除用户</span></a></li><li><aclass="edit" href="user!dosave.action?userId={sid_user}"target="dialog" mask="true"warn="请选择一个用户"><span>修改</span></a></li>
</ul>
</div><divlayoutH="116" id="w_list_print">
<tableclass="table" width="98%" targetType="navTab"asc="asc" desc="desc">
<thead>
<tr><th><inputtype="checkbox" group="ids"class="checkboxCtrl"></th>
<th orderField="id" <c:if test="${orderField=='id'}">class="${orderDirection}"</c:if> title="排序">编号</th>
<th>用户姓名</th>
<th>用户密码</th>
<th>是否是管理员</th>
<thorderField="staff_id" <s:iftest="orderField=='staff_id'">class="${orderDirection}"</s:if>>staff-id</th><th>操作</th>
</tr>
</thead>
<tbody>
<c:forEachvar="user" items="${userList}">
<trtarget="sid_user" rel="${user.id}">
<td><inputname="ids" value="${user.id}"type="checkbox"></td>
<td>${user.id}</td>
<td>${user.loginName}</td>
<td>${user.passWord}</td>
<td>${user.adminInd}</td>
<td>${user.staffId}</td>
<td><a title="删除"target="ajaxTodo" href="user!del.action?ids=${user.id}"class="btnDel">删除</a>
<atitle="编辑" target="dialog" mask="true"href="user!dosave.action?userId=${user.id}"class="btnEdit">编辑</a>
</td>
</tr>
</c:forEach></tbody>
</table>
</div><!-- 自定义标签封装pagerForm,简化分页 -->
<custom:pagination page="${page }"/>


 

重点是

自定义custom:paginationForm和custom:pagination 标签中的参数名称

<inputtype="hidden" name="pageNum"value="${pageNum}"/>
<inputtype="hidden" name="numPerPage"value="${numPerPage}"/>

要和BaseAction的属性对上

 

<c:if test='${param.orderField == "name" }'> class="${param.orderDirection}"  </c:if>    

用来切换 排序图标和排序类型  

 

BaseAction封装分页参数

public class BaseAction extends ActionSupport{protectedInteger pageNum=1; // 当前是第几页protectedInteger totalCount=0; // 总条数protectedInteger numPerPage=20; // 每页显示多少条protectedString orderField; // 排序字段protectedString orderDirection; // 排序方向。。。。。getter方法和setter方法省略

 

UserAction通过继承BaseAction,得到客户端传来的数据

publicclass UserAction extends BaseAction{
SysuserDaouserDao = new SysuserDaoImpl();privateString keywords = "";protected Integer userId;protectedSysusers sysusers;privateString[] ids;privateList<Sysusers> userList = null;publicString userList() {
totalCount= userDao.getCout(keywords);
userList= userDao.userList(keywords,pageNum,numPerPage,orderField,orderDirection);return"list";
}


 

效果图


 

加入自定义分页实体类

如果要加入自定义的分页实体,通过注入(对象.属性,比如

<input type="hidden" name="page.pageNum" value="${page.pageNum}"/>)方式应该可以完成,但在试验过程中,发现分页js还会用到分页属性,如果把name改为page.pageNum,js会根据这个name进行分页,也就是说这个input有两处会用到。

而更改之后,分页就不起作用了。导致不能加入自己的分页实体,这是在struts2中。不过应该通过修改dwzjs代码可以解决这个问题。

 

如果是springmvc,不用对象.属性方式,直接使用原生的属性名称,也可以注入到controller类中的方法中的实体参数中。这也是struts2springmvc的一大区别。

 

在开发过程中,DWZ是我负责的一部分,我需要搭起一个模型来,别人用时只需要按照我定义的规范做就是了。这是DWZ的分页规范,下面我会介绍DWZ的异常处理信息规范。

 

这篇关于DWZ (JUI) 应用实例(一):通过自定义标签和BaseAction 简化 table 分页排序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/828336

相关文章

Python datetime 模块概述及应用场景

《Pythondatetime模块概述及应用场景》Python的datetime模块是标准库中用于处理日期和时间的核心模块,本文给大家介绍Pythondatetime模块概述及应用场景,感兴趣的朋... 目录一、python datetime 模块概述二、datetime 模块核心类解析三、日期时间格式化与

SpringBoot中四种AOP实战应用场景及代码实现

《SpringBoot中四种AOP实战应用场景及代码实现》面向切面编程(AOP)是Spring框架的核心功能之一,它通过预编译和运行期动态代理实现程序功能的统一维护,在SpringBoot应用中,AO... 目录引言场景一:日志记录与性能监控业务需求实现方案使用示例扩展:MDC实现请求跟踪场景二:权限控制与

Python开发文字版随机事件游戏的项目实例

《Python开发文字版随机事件游戏的项目实例》随机事件游戏是一种通过生成不可预测的事件来增强游戏体验的类型,在这篇博文中,我们将使用Python开发一款文字版随机事件游戏,通过这个项目,读者不仅能够... 目录项目概述2.1 游戏概念2.2 游戏特色2.3 目标玩家群体技术选择与环境准备3.1 开发环境3

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

Pandas透视表(Pivot Table)的具体使用

《Pandas透视表(PivotTable)的具体使用》透视表用于在数据分析和处理过程中进行数据重塑和汇总,本文就来介绍一下Pandas透视表(PivotTable)的具体使用,感兴趣的可以了解一下... 目录前言什么是透视表?使用步骤1. 引入必要的库2. 读取数据3. 创建透视表4. 查看透视表总结前言

C语言中位操作的实际应用举例

《C语言中位操作的实际应用举例》:本文主要介绍C语言中位操作的实际应用,总结了位操作的使用场景,并指出了需要注意的问题,如可读性、平台依赖性和溢出风险,文中通过代码介绍的非常详细,需要的朋友可以参... 目录1. 嵌入式系统与硬件寄存器操作2. 网络协议解析3. 图像处理与颜色编码4. 高效处理布尔标志集合

Java中的Lambda表达式及其应用小结

《Java中的Lambda表达式及其应用小结》Java中的Lambda表达式是一项极具创新性的特性,它使得Java代码更加简洁和高效,尤其是在集合操作和并行处理方面,:本文主要介绍Java中的La... 目录前言1. 什么是Lambda表达式?2. Lambda表达式的基本语法例子1:最简单的Lambda表

Python结合PyWebView库打造跨平台桌面应用

《Python结合PyWebView库打造跨平台桌面应用》随着Web技术的发展,将HTML/CSS/JavaScript与Python结合构建桌面应用成为可能,本文将系统讲解如何使用PyWebView... 目录一、技术原理与优势分析1.1 架构原理1.2 核心优势二、开发环境搭建2.1 安装依赖2.2 验

Java字符串操作技巧之语法、示例与应用场景分析

《Java字符串操作技巧之语法、示例与应用场景分析》在Java算法题和日常开发中,字符串处理是必备的核心技能,本文全面梳理Java中字符串的常用操作语法,结合代码示例、应用场景和避坑指南,可快速掌握字... 目录引言1. 基础操作1.1 创建字符串1.2 获取长度1.3 访问字符2. 字符串处理2.1 子字