此类是基于iBatis数据库端访问组件的抽象Action类
只需继承该Action实现3个方法即
==========================================
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.ecside.table.limit.Limit;
import org.ecside.table.limit.Sort;
import org.ecside.util.RequestUtils;
import com.fp.zyyw.util.SpringHelper;
import com.ibatis.sqlmap.client.SqlMapClient;
public abstract class AbstractQueryForPagedListAction extends Action {
int DEFAULT_PAGESIZE = 20;
protected String sqlMapStringForList = null;
protected String sqlMapStringForTotalSize = null;
private static final Logger logger = Logger
.getLogger(AbstractQueryForPagedListAction.class);
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
String target = "success";
try {
setSqlMapStringForList();
setSqlMapStringForTotalSize();
Limit limit = RequestUtils.getLimit(req);
Sort sort = limit.getSort();
Map map = new HashMap();
setQueryCondition(map, form, req);
int totalSize = queryForPagedListTotalSize(map);
logger.debug("totalSize = " + totalSize);
limit.setRowAttributes(totalSize, DEFAULT_PAGESIZE);
String to = String.valueOf(limit.getRowEnd());
String from = String.valueOf(limit.getRowStart());
logger.debug("to = " + to);
logger.debug("from = " + from);
System.out.println("from = " + from + " to = " + to);
map.put("to", to);
map.put("from", from);
setQueryOrder(map, sort);
req.setAttribute("recordList", queryForPagedList(map));
} catch (Exception e) {
e.printStackTrace();
logger.error(e);
String errMsg = "查询异常";
target = "error";
req.setAttribute("errMsgType", "USER");
req.setAttribute("errMsg", errMsg);
}
return mapping.findForward(target);
}
public abstract void setSqlMapStringForList();
public abstract void setSqlMapStringForTotalSize();
public abstract void setQueryCondition(Map param, ActionForm form,
HttpServletRequest req);
public List queryForPagedList(Map param) throws SQLException {
SqlMapClient sqlMapClient = (SqlMapClient) SpringHelper.getBean("sqlMapClient");
List list = sqlMapClient.queryForList(this.sqlMapStringForList, param);
sqlMapClient.getCurrentConnection().close();
return list;
}
public int queryForPagedListTotalSize(Map param) throws SQLException {
SqlMapClient sqlMapClient = (SqlMapClient) SpringHelper
.getBean("sqlMapClient");
BigDecimal totalSize = (BigDecimal) sqlMapClient.queryForObject(
this.sqlMapStringForTotalSize, param);
sqlMapClient.getCurrentConnection();
return totalSize.intValue();
}
public void setQueryOrder(Map param, Sort sort) {
if (sort != null && sort.getSortValueMap() != null
&& !sort.getSortValueMap().isEmpty()) {
String orderByClause = sort.getProperty() + " "
+ sort.getSortOrder();
param.put("orderByClause", orderByClause);
}
}
}
只需继承该Action实现3个方法即
==========================================
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.ecside.table.limit.Limit;
import org.ecside.table.limit.Sort;
import org.ecside.util.RequestUtils;
import com.fp.zyyw.util.SpringHelper;
import com.ibatis.sqlmap.client.SqlMapClient;
public abstract class AbstractQueryForPagedListAction extends Action {
int DEFAULT_PAGESIZE = 20;
protected String sqlMapStringForList = null;
protected String sqlMapStringForTotalSize = null;
private static final Logger logger = Logger
.getLogger(AbstractQueryForPagedListAction.class);
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
String target = "success";
try {
setSqlMapStringForList();
setSqlMapStringForTotalSize();
Limit limit = RequestUtils.getLimit(req);
Sort sort = limit.getSort();
Map map = new HashMap();
setQueryCondition(map, form, req);
int totalSize = queryForPagedListTotalSize(map);
logger.debug("totalSize = " + totalSize);
limit.setRowAttributes(totalSize, DEFAULT_PAGESIZE);
String to = String.valueOf(limit.getRowEnd());
String from = String.valueOf(limit.getRowStart());
logger.debug("to = " + to);
logger.debug("from = " + from);
System.out.println("from = " + from + " to = " + to);
map.put("to", to);
map.put("from", from);
setQueryOrder(map, sort);
req.setAttribute("recordList", queryForPagedList(map));
} catch (Exception e) {
e.printStackTrace();
logger.error(e);
String errMsg = "查询异常";
target = "error";
req.setAttribute("errMsgType", "USER");
req.setAttribute("errMsg", errMsg);
}
return mapping.findForward(target);
}
public abstract void setSqlMapStringForList();
public abstract void setSqlMapStringForTotalSize();
public abstract void setQueryCondition(Map param, ActionForm form,
HttpServletRequest req);
public List queryForPagedList(Map param) throws SQLException {
SqlMapClient sqlMapClient = (SqlMapClient) SpringHelper.getBean("sqlMapClient");
List list = sqlMapClient.queryForList(this.sqlMapStringForList, param);
sqlMapClient.getCurrentConnection().close();
return list;
}
public int queryForPagedListTotalSize(Map param) throws SQLException {
SqlMapClient sqlMapClient = (SqlMapClient) SpringHelper
.getBean("sqlMapClient");
BigDecimal totalSize = (BigDecimal) sqlMapClient.queryForObject(
this.sqlMapStringForTotalSize, param);
sqlMapClient.getCurrentConnection();
return totalSize.intValue();
}
public void setQueryOrder(Map param, Sort sort) {
if (sort != null && sort.getSortValueMap() != null
&& !sort.getSortValueMap().isEmpty()) {
String orderByClause = sort.getProperty() + " "
+ sort.getSortOrder();
param.put("orderByClause", orderByClause);
}
}
}