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;
}
}