BookSearchAction.java
上传用户:jishiqi_cj
上传日期:2022-08-08
资源大小:24765k
文件大小:3k
源码类别:

Java编程

开发平台:

Java

  1. package BookStore;
  2. import javax.sql.DataSource;
  3. import java.sql.Connection;
  4. import javax.servlet.ServletContext;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.servlet.http.HttpSession;
  8. import org.apache.struts.action.Action;
  9. import org.apache.struts.action.ActionForm;
  10. import org.apache.struts.action.ActionForward;
  11. import org.apache.struts.action.ActionMapping;
  12. import org.apache.struts.action.ActionMessage;
  13. import org.apache.struts.action.ActionMessages;
  14. import java.util.Vector;
  15. import org.apache.struts.action.DynaActionForm;
  16. public final class BookSearchAction extends Action{  
  17. public ActionForward execute(
  18. ActionMapping mapping,
  19. ActionForm form,
  20. HttpServletRequest request,  
  21. HttpServletResponse response) throws Exception {
  22.     DynaActionForm bookSearchForm = (DynaActionForm) form;         
  23. Integer mode = null;
  24. Integer bookCatId = null;
  25. String key = null;
  26. String field = null;
  27. if (bookSearchForm!=null){
  28. mode = (Integer)bookSearchForm.get("mode");     //搜索模式
  29. bookCatId = (Integer)bookSearchForm.get("bookCatId");
  30. key = (String)bookSearchForm.get("key");
  31. field = (String)bookSearchForm.get("field");
  32. }
  33.     ServletContext context = servlet.getServletContext();
  34. DataSource dataSource = (DataSource)context.getAttribute(Constants.DATASOURCE_KEY);
  35.         DB db = new DB(dataSource);
  36.         
  37. SearchStatus schSta = new SearchStatus();
  38. int iMode=1; 
  39. int iCatId=0; 
  40. if (mode==null){
  41. iCatId = BookCat.GetFirstBookCat(db);
  42. schSta.setCatId(iCatId);
  43. }
  44. else if (mode.intValue()==1){     //按类别查询
  45. iCatId= bookCatId.intValue();
  46. schSta.setCatId(iCatId);
  47. }
  48. else if (mode.intValue()==2){ //按关键字查询
  49. iMode = mode.intValue();
  50. schSta.setKey(key);
  51. schSta.setField(field);
  52. }
  53. schSta.setMode(iMode);
  54.         int bookCount = Book.GetBookCount(db,iMode,iCatId,field,key);
  55. int iPageCount;
  56. if (bookCount%Constants.BOOK_PAGE_SIZE==0) {
  57. iPageCount = bookCount/Constants.BOOK_PAGE_SIZE;
  58. }
  59. else{
  60. iPageCount = bookCount/Constants.BOOK_PAGE_SIZE+1;
  61. }
  62. schSta.setBookCount(bookCount);
  63. schSta.setPageCount(iPageCount);
  64.         Vector bookList = new Vector();
  65.         HttpSession session = request.getSession();
  66.         
  67. session.setAttribute(Constants.BOOK_CAT_LIST_KEY,BookCat.Search(db));
  68. bookList = Book.SearchBook(db,iMode,iCatId,field,key);
  69. session.setAttribute(Constants.BOOK_LIST_KEY,bookList);
  70. session.setAttribute(Constants.BOOK_SEARCH_STATUS_KEY,schSta);
  71. db.close();
  72.     return (mapping.findForward("toBookMain"));
  73.   }
  74. }