java 代码
- delegator = request.getAttribute("delegator");
- //获取request参数
- lookupFlag = request.getParameter("lookupFlag");
- shipmentTypeId = request.getParameter("shipmentTypeId");
- originFacilityId = request.getParameter("originFacilityId");
- destinationFacilityId = request.getParameter("destinationFacilityId");
- statusId = request.getParameter("statusId");
- // 设置分页参数
- viewIndex = 1;
- try {
- viewIndex = Integer.valueOf((String) request.getParameter("VIEW_INDEX")).intValue();
- } catch (Exception e) {
- viewIndex = 1;
- }
- context.put("viewIndex", viewIndex);
- viewSize = 20;
- try {
- viewSize = Integer.valueOf((String) request.getParameter("VIEW_SIZE")).intValue();
- } catch (Exception e) {
- viewSize = 20;
- }
- context.put("viewSize", viewSize);
-
- //设置参数链表(paramListBuffer),接受参数
- findShipmentExprs = new LinkedList();
- paramListBuffer = new StringBuffer();
- if (UtilValidate.isNotEmpty(shipmentTypeId)) {
- paramListBuffer.append("&shipmentTypeId=");
- paramListBuffer.append(shipmentTypeId);
- findShipmentExprs.add(new EntityExpr("shipmentTypeId", EntityOperator.EQUALS, shipmentTypeId));
- currentShipmentType = delegator.findByPrimaryKeyCache("ShipmentType", UtilMisc.toMap("shipmentTypeId", shipmentTypeId));
- context.put("currentShipmentType", currentShipmentType);
- }
- if (UtilValidate.isNotEmpty(originFacilityId)) {
- paramListBuffer.append("&originFacilityId=");
- paramListBuffer.append(originFacilityId);
- findShipmentExprs.add(new EntityExpr("originFacilityId", EntityOperator.EQUALS, originFacilityId));
- currentOriginFacility = delegator.findByPrimaryKeyCache("Facility", UtilMisc.toMap("facilityId", originFacilityId));
- context.put("currentOriginFacility", currentOriginFacility);
- }
- if (UtilValidate.isNotEmpty(destinationFacilityId)) {
- paramListBuffer.append("&destinationFacilityId=");
- paramListBuffer.append(destinationFacilityId);
- findShipmentExprs.add(new EntityExpr("destinationFacilityId", EntityOperator.EQUALS, destinationFacilityId));
- currentDestinationFacility = delegator.findByPrimaryKeyCache("Facility", UtilMisc.toMap("facilityId", destinationFacilityId));
- context.put("currentDestinationFacility", currentDestinationFacility);
- }
- if (UtilValidate.isNotEmpty(statusId)) {
- paramListBuffer.append("&statusId=");
- paramListBuffer.append(statusId);
- findShipmentExprs.add(new EntityExpr("statusId", EntityOperator.EQUALS, statusId));
- currentStatus = delegator.findByPrimaryKeyCache("StatusItem", UtilMisc.toMap("statusId", statusId));
- context.put("currentStatus", currentStatus);
- }
- if ("Y".equals(lookupFlag)) { //如果有查看权限,则通过。
- context.put("paramList", paramListBuffer.toString());
- if (findShipmentExprs.size() > 0) {
- EntityFindOptions findOpts = new EntityFindOptions(true, EntityFindOptions.TYPE_SCROLL_INSENSITIVE, EntityFindOptions.CONCUR_READ_ONLY, true);
- mainCond = new EntityConditionList(findShipmentExprs, EntityOperator.AND);
- List orderBy = UtilMisc.toList("-estimatedShipDate");
- boolean beganTransaction = false;
- try {
- beganTransaction = TransactionUtil.begin();
- // using list iterator
- EntityListIterator orli = delegator.findListIteratorByCondition("Shipment", mainCond, null, null, orderBy, findOpts);
- // get the indexes for the partial list
- lowIndex = (((viewIndex - 1) * viewSize) + 1);
- highIndex = viewIndex * viewSize;
- // 如果 highIndex 大于总记录数,则highIndex更改。
- // 否则不变。
- orli.last();
- shipmentListSize = orli.currentIndex();
- if (highIndex > shipmentListSize) {
- highIndex = shipmentListSize;
- }
- // get the partial list for this page
- orli.beforeFirst();
- if (shipmentListSize > 0) {
- shipmentList = orli.getPartialList(lowIndex, viewSize);
- } else {
- shipmentList = new ArrayList();
- }
- // 在所记录的信息传递给shipmentList后,orli关闭。
- orli.close();
- } catch (GenericEntityException e) {
- String errMsg = "Failure in operation, rolling back transaction";
- Debug.logError(e, errMsg, module);
- try {
- // only rollback the transaction if we started one...
- TransactionUtil.rollback(beganTransaction, errMsg, e);
- } catch (GenericEntityException e2) {
- Debug.logError(e2, "Could not rollback transaction: " + e2.toString(), module);
- }
- // after rolling back, rethrow the exception
- throw e;
- } finally {
- // only commit the transaction if we started one... this will throw an exception if it fails
- TransactionUtil.commit(beganTransaction);
- }
- } else { //没有权限
- shipmentList = new ArrayList();
- shipmentListSize = 0;
- highIndex = 0;
- lowIndex = 0;
- }
- //设置返回信息。
- context.put("shipmentList", shipmentList);
- context.put("listSize", shipmentListSize);
- context.put("highIndex", highIndex);
- context.put("lowIndex", lowIndex);
- }
本文展示了一个使用Java进行分页查询的例子,包括从HTTP请求中获取参数、设置查询条件并进行分页处理的过程。该示例适用于需要对大量数据进行高效管理的应用场景。
2542

被折叠的 条评论
为什么被折叠?



