当前位置: 代码迷 >> Web前端 >> struts-jquery-grid-tags 学习札记
  详细解决方案

struts-jquery-grid-tags 学习札记

热度:390   发布时间:2012-11-07 09:56:10.0
struts-jquery-grid-tags 学习笔记

java 代码:

?

?

package com.topdt.message.web.action;

?

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import java.util.Map;

?

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.apache.struts2.convention.annotation.Action;

import org.apache.struts2.convention.annotation.Namespace;

import org.apache.struts2.convention.annotation.ParentPackage;

import org.apache.struts2.convention.annotation.Result;

import org.apache.struts2.interceptor.SessionAware;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Scope;

import org.springframework.stereotype.Controller;

?

import com.opensymphony.xwork2.ActionSupport;

import com.topdt.message.entity.Business;

import com.topdt.message.service.BusinessManager;

?

@Controller

@Scope("prototype")

@ParentPackage(value = "gloab-package")

@Namespace(value = "/message")

@Action(value = "business", results = {

@Result(name="query",type="json"),

@Result(name="show",location="/message/business/business_list.jsp")

})

public class BusinessAction extends ActionSupport implements SessionAware {

?

private static final long serialVersionUID = 1L;

?

private static final Log log = LogFactory.getLog(BusinessAction.class);

private List<Business> gridModel;

private Integer rows;

private Integer page;

private String sord;

private String sidx;

private String searchField;

private String searchString;

private Integer totalrows;

private String searchOper;

private Integer total;

private Integer records;

private boolean loadonce;

private List<Business> myCustomers;

@Autowired

private BusinessManager businessManager;

public BusinessAction() {

this.rows = Integer.valueOf(0);

?

this.page = Integer.valueOf(0);

?

this.total = Integer.valueOf(0);

?

this.records = Integer.valueOf(0);

?

this.loadonce = false;

}

public String show() {

return "show";

}

?

public String query() {

this.myCustomers = businessManager.query();

if ((this.sord != null) && (this.sord.equalsIgnoreCase("asc"))) {

Collections.sort(this.myCustomers);

}

if ((this.sord != null) && (this.sord.equalsIgnoreCase("desc"))) {

Collections.sort(this.myCustomers);

Collections.reverse(this.myCustomers);

}

?

this.records = businessManager.getCustomersCount(this.myCustomers);

?

if (this.totalrows != null) {

this.records = this.totalrows;

}

?

int to = this.rows.intValue() * this.page.intValue();

?

int from = to - this.rows.intValue();

?

if (to > this.records.intValue())

to = this.records.intValue();

?

if (this.loadonce) {

if ((this.totalrows != null) && (this.totalrows.intValue() > 0)) {

setGridModel(this.myCustomers.subList(0, this.totalrows

.intValue()));

} else {

setGridModel(this.myCustomers);

}

?

} else if ((this.searchString != null) && (this.searchOper != null)) {

Long id = new Long(this.searchString);

if (this.searchOper.equalsIgnoreCase("eq")) {

log.debug("search id equals " + id);

List cList = new ArrayList();

Business business = businessManager.findById(this.myCustomers, id);

if (business != null)

cList.add(business);

?

setGridModel(cList);

} else if (this.searchOper.equalsIgnoreCase("ne")) {

log.debug("search id not " + id);

setGridModel(businessManager.findNotById(this.myCustomers, id, from, to));

} else if (this.searchOper.equalsIgnoreCase("lt")) {

log.debug("search id lesser then " + id);

setGridModel(businessManager.findLesserAsId(this.myCustomers, id, from, to));

} else if (this.searchOper.equalsIgnoreCase("gt")) {

log.debug("search id greater then " + id);

setGridModel(businessManager.findGreaterAsId(this.myCustomers, id, from, to));

}

} else {

setGridModel(businessManager.getCustomers(this.myCustomers, from, to));

}

?

this.total = Integer.valueOf((int) Math.ceil(this.records.intValue()

/ this.rows.intValue()));

return "query";

}

?

public String getJSON() {

return query();

}

?

public Integer getRows() {

return this.rows;

}

?

public void setRows(Integer rows) {

this.rows = rows;

}

?

public Integer getPage() {

return this.page;

}

?

public void setPage(Integer page) {

this.page = page;

}

?

public Integer getTotal() {

return this.total;

}

?

public void setTotal(Integer total) {

this.total = total;

}

?

public Integer getRecords() {

return this.records;

}

?

public void setRecords(Integer records) {

this.records = records;

?

if ((this.records.intValue() > 0) && (this.rows.intValue() > 0)) {

this.total = Integer.valueOf((int) Math.ceil(this.records

.intValue()

/ this.rows.intValue()));

} else {

this.total = Integer.valueOf(0);

}

}

?

public List<Business> getGridModel() {

return this.gridModel;

}

?

public void setGridModel(List<Business> gridModel) {

this.gridModel = gridModel;

}

?

public String getSord() {

return this.sord;

}

?

public void setSord(String sord) {

this.sord = sord;

}

?

public String getSidx() {

return this.sidx;

}

?

public void setSidx(String sidx) {

this.sidx = sidx;

}

?

public void setSearchField(String searchField) {

this.searchField = searchField;

}

?

public void setSearchString(String searchString) {

this.searchString = searchString;

}

?

public void setSearchOper(String searchOper) {

this.searchOper = searchOper;

}

?

public void setLoadonce(boolean loadonce) {

this.loadonce = loadonce;

}

?

public void setTotalrows(Integer totalrows) {

this.totalrows = totalrows;

}

public void setSession(Map<String, Object> session) {

}

}

?

?

?

package com.topdt.message.service;

?

import java.util.ArrayList;

import java.util.Hashtable;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

?

?

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

?

import com.topdt.message.dao.BusinessDao;

import com.topdt.message.entity.Business;

import com.topdt.message.entity.Product;

?

?

@Service

@Transactional(readOnly = false)

public class BusinessManager {

private static Map<Long, Product> MAP_ID = new Hashtable<Long, Product>();

private static Map<String, Product> MAP_CODE = new Hashtable<String, Product>();

@Autowired

private BusinessDao businessDao;

@Transactional(readOnly = true)

public List<Business> query() {

return ?businessDao.queryAll();

}

public static List<Business> getCustomers(List<Business> list,

int from, int to) {

return list.subList(from, to);

}

?

public static Business findById(List<Business> list, Long id) {

for (Iterator i$ = list.iterator(); i$.hasNext();) {

Business business = (Business) i$.next();

if (business.getBusinessId()== id)

return business;

}

return null;

}

?

public static List<Business> findNotById(List<Business> list, Long id,

int from, int to) {

List sResult = new ArrayList();

?

for (Iterator i$ = list.iterator(); i$.hasNext();) {

Business Business = (Business) i$.next();

?

if (Business.getBusinessId() != id)

sResult.add(Business);

}

?

return sResult.subList(from, to);

}

?

public static List<Business> findGreaterAsId(List<Business> list,

Long id, int from, int to) {

List sResult = new ArrayList();

?

for (Iterator i$ = list.iterator(); i$.hasNext();) {

Business Business = (Business) i$.next();

?

if (Business.getBusinessId() > id)

sResult.add(Business);

}

?

return sResult.subList(from, to);

}

?

public static List<Business> findLesserAsId(List<Business> list,

Long id, int from, int to) {

List sResult = new ArrayList();

?

for (Iterator i$ = list.iterator(); i$.hasNext();) {

Business Business = (Business) i$.next();

if (Business.getBusinessId() < id)

sResult.add(Business);

}

?

return sResult.subList(from, to);

}

?

public static Integer getCustomersCount(List<Business> list) {

return Integer.valueOf(list.size());

}

?

public BusinessDao getBusinessDao() {

return businessDao;

}

?

public void setBusinessDao(BusinessDao businessDao) {

this.businessDao = businessDao;

}

}



html代码:


<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<%@ taglib prefix="sjg" uri="/struts-jquery-grid-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>业务列表</title>
<!-- no cache headers -->
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="no-cache" />
<meta http-equiv="Expires" content="-1" />
<meta http-equiv="Cache-Control" content="no-cache" />
<!-- end no cache headers -->
<sj:head locale="zh-CN" />
</head>
<body style="margin:10px;padding:0px;">
? ? <s:url id="editurl" action="business!query"/>
? ? <s:url id="busGrid" action="business!query"/>
? ? <sjg:grid
? ? id="busTable"
? ? caption="业务列表"
? ? dataType="json"
? ? href="%{busGrid}"
? ? pager="true"
? ? navigator="true"
? ? navigatorSearchOptions="{sopt:['eq','ne','lt','gt']}"
? ? navigatorAddOptions="{height:280,reloadAfterSubmit:true}"
? ? navigatorEditOptions="{height:280,reloadAfterSubmit:false}"
? ? navigatorEdit="false"
? ? navigatorView="false"
? ? navigatorDelete="true"
? ? navigatorDeleteOptions="{height:280,reloadAfterSubmit:true}"
? ? navigatorExtraButtons="{
? ? seperator: {?
? ? title : 'seperator' ?
? ? },?
? ? hide : {?
? ? title : 'Show/Hide',?
? ? icon: 'ui-icon-wrench',?
? ? topic: 'showcolumns'
? ? },
? ? alert : {?
? ? title : 'Alert',?
? ? onclick: function(){ alert('Grid Button clicked!') }
? ? }
? ? }"
? ? gridModel="gridModel"
? ? rowList="10,15,20"
? ? rowNum="15"
? ? editurl="%{editurl}"
? ? editinline="true"
? ? onSelectRowTopics="rowselect"
? ? onEditInlineSuccessTopics="oneditsuccess"
? ? viewrecords="true"
? ? >
? ? <sjg:gridColumn width="40"name="businessId" index="businessId" align="right" title="业务ID" formatter="integer" sortable="false"/>
? ? <sjg:gridColumn width="100"name="businessName" index="businessName" ?title="业务名称" sortable="false"/>
? ? <sjg:gridColumn width="100"name="shortName" index="shortName" title="业务简称" sortable="false"/>
? ? <sjg:gridColumn width="100"name="businessCatalog" index="businessCatalog" title="业务分类" sortable="false"/>
? ? <sjg:gridColumn width="100"name="businessType" index="businessType" title="业务类型" sortable="false"/>
? ? <sjg:gridColumn width="100"name="businessCode" index="businessCode" title="业务代码" sortable="false"/>
? ? <sjg:gridColumn width="100"name="modelCode" index="modelCode" title="模型编码" sortable="false"/>
? ? <sjg:gridColumn width="100"name="ownerId" index="ownerId" title="业务属主" sortable="false"/>
? ? <sjg:gridColumn width="100"name="resourceId" index="resourceId" title="服务类型" sortable="false"/>
? ? <sjg:gridColumn width="100"name="serviceType" index="serviceType" title="服务资源" sortable="false"/>
? ? </sjg:grid>
</body>
</html>



  相关解决方案