📄 searchcustomeraction.java
字号:
package app18b.action;
import app18b.dao.CustomerDAO;
import app18b.dao.DAOFactory;
import app18b.form.CustomerSearchCriteriaForm;
import app18b.util.Config;
import app18b.to.CustomerSearchCriteriaTO;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class SearchCustomerAction extends Action {
private static int maxRecordsPerPage;
private static int maxPagesPerRetrieve;
private static int maxRecordsPerRetrieve;
private static String CUSTOMER_LIST = "customerList";
static {
maxRecordsPerPage = Integer.parseInt(Config.getInstance().getStringValue("maxRecordsPerPage"));
maxPagesPerRetrieve = Integer.parseInt(Config.getInstance().getStringValue("maxPagesPerRetrieve"));
maxRecordsPerRetrieve = maxRecordsPerPage * maxPagesPerRetrieve;
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
CustomerSearchCriteriaForm criteria = (CustomerSearchCriteriaForm) form;
int currentPage = 1; // the page number requested
try {
currentPage = Integer.parseInt(request.getParameter("page"));
}
catch (NumberFormatException e) {
}
criteria.setCurrentPage(currentPage);
criteria.setNextPage(currentPage + 1);
criteria.setPreviousPage(currentPage - 1);
HttpSession session = request.getSession();
if (request.getMethod().equalsIgnoreCase("POST") ||
session.getAttribute(CUSTOMER_LIST)==null) {
ArrayList customerList = retrieveDataFromDB(criteria, 0, maxRecordsPerRetrieve);
session.setAttribute(CUSTOMER_LIST, customerList);
criteria.setCachedStartPage(1);
}
else {
// retrieve the cached result
if (currentPage<criteria.getCachedStartPage() ||
currentPage >= criteria.getCachedStartPage() + maxPagesPerRetrieve) {
ArrayList customerList = retrieveDataFromDB(criteria, (currentPage - 1) * maxRecordsPerPage, maxRecordsPerRetrieve);
session.setAttribute(CUSTOMER_LIST, customerList);
criteria.setCachedStartPage(currentPage);
}
}
criteria.setStartRecord((currentPage - criteria.getCachedStartPage()) * maxRecordsPerPage);
criteria.setEndRecord(criteria.getStartRecord() + maxRecordsPerPage - 1);
return mapping.getInputForward();
}
private ArrayList retrieveDataFromDB (CustomerSearchCriteriaForm criteria, int offset, int maxRecordsPerRetrieve) {
CustomerSearchCriteriaTO customerSearch = new CustomerSearchCriteriaTO();
ArrayList customerList = new ArrayList();
customerSearch.setName(criteria.getName());
customerSearch.setAddress(criteria.getAddress());
customerSearch.setPhone(criteria.getPhone());
CustomerDAO customerDAO = DAOFactory.getInstance().getCustomerDAO();
try {
customerList = customerDAO.searchCustomers(customerSearch, offset, maxRecordsPerRetrieve +1);
int size = customerList.size();
if (size < maxRecordsPerRetrieve + 1)
criteria.setHighestPageNumber(criteria.getCurrentPage() + (int) Math.ceil(size/maxRecordsPerPage));
else
criteria.setHighestPageNumber(-1);
}
catch (Exception ex) {
}
return customerList;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -