📄 searchaction.java
字号:
package cn.com.shoppingonline;
import java.util.ArrayList;
import java.util.List;
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;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.DynaActionForm;
public final class SearchAction extends Action{
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
DynaActionForm searchForm = (DynaActionForm) form;
Integer sortId = (Integer)searchForm.get("sortId");
String keyword = (String)searchForm.get("keyword");
Integer pageId = (Integer)searchForm.get("pageId");
int pageCount = 0;
int iPageId = 0;
if (pageId!=null) iPageId = pageId.intValue();
if (iPageId<0) iPageId = 0;
String PageForward="toListMain";
ActionMessages errors = new ActionMessages();
List productList=new DbOperate().getMatchProducts(sortId.intValue(),keyword);
HttpSession session = request.getSession(true);
/*
* 没有搜索到商品处理
*/
if (productList.size()==0){
errors.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("errors.noProduct"));
if (!errors.isEmpty()) {
saveErrors(request, errors);
}
PageForward="toWrong";
}
/*
* 获取总页数
*/
if (productList.size()%Constants.PRODUCT_PAGE_SIZE ==0){
pageCount=productList.size() / Constants.PRODUCT_PAGE_SIZE;
}
else{
pageCount=productList.size() / Constants.PRODUCT_PAGE_SIZE+1;
}
/*
* 分页显示
*/
if ((productList.size()>iPageId * Constants.PRODUCT_PAGE_SIZE )&&(iPageId>=0)){
List dispList=new ArrayList();
for (int i=iPageId*Constants.PRODUCT_PAGE_SIZE;i<(iPageId+1)*Constants.PRODUCT_PAGE_SIZE;i++){
if (i<productList.size()){
dispList.add(productList.get(i));
}
}
session.setAttribute(Constants.SEARCH_PRODUCT_LIST_KEY,dispList);
session.setAttribute(Constants.CUR_PAGEID_KEY,new Integer(iPageId));
session.setAttribute(Constants.PAGE_COUNT_KEY,new Integer(pageCount));
session.setAttribute(Constants.CUR_SORTID_KEY,sortId);
session.setAttribute(Constants.CUR_KEYWORD_KEY,keyword);
PageForward="toListMain";
}
return (mapping.findForward(PageForward));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -