Book.java~29~
上传用户:toby828
上传日期:2015-06-26
资源大小:8558k
文件大小:7k
源码类别:

Jsp/Servlet

开发平台:

Java

  1. package com.action;
  2. import org.apache.struts.action.ActionMapping;
  3. import org.apache.struts.action.ActionForm;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import org.apache.struts.action.ActionForward;
  7. import com.actionForm.BookForm;
  8. import org.apache.struts.action.Action;
  9. import com.dao.BookDAO;
  10. import java.util.Date;
  11. public class Book extends Action {
  12.     private BookDAO bookDAO = null;
  13.     public Book() {
  14.         this.bookDAO = new BookDAO();
  15.     }
  16.     public ActionForward perform(ActionMapping mapping, ActionForm form,
  17.                                  HttpServletRequest request,
  18.                                  HttpServletResponse response) {
  19.         String action =request.getParameter("action");
  20.         System.out.println("nbook*********************action="+action);
  21.         if(action==null||"".equals(action)){
  22.             request.setAttribute("error","您的操作有误!");
  23.             return mapping.findForward("error");
  24.         }else if("bookAdd".equals(action)){
  25.             return bookAdd(mapping,form,request,response);
  26.         }else if("bookQuery".equals(action)){
  27.             return bookQuery(mapping,form,request,response);
  28.         }else if("bookModifyQuery".equals(action)){
  29.             return bookModifyQuery(mapping,form,request,response);
  30.         }else if("bookModify".equals(action)){
  31.             return bookModify(mapping,form,request,response);
  32.         }else if("bookDel".equals(action)){
  33.             return bookDel(mapping,form,request,response);
  34.         }else if("bookDetail".equals(action)){
  35.             return bookDetail(mapping,form,request,response);
  36.         }else if("bookifQuery".equals(action)){
  37.             return bookifQuery(mapping,form,request,response);
  38.         }
  39.         request.setAttribute("error","操作失败!");
  40.         return mapping.findForward("error");
  41.     }
  42.     /***********************添加图书信息**************************/
  43.        private ActionForward bookAdd(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest servletRequest, HttpServletResponse servletResponse){
  44.            BookForm bookForm = (BookForm) actionForm;
  45.            bookForm.setBarcode(bookForm.getBarcode());
  46.            bookForm.setBookName(bookForm.getBookName());
  47.            bookForm.setTypeId(bookForm.getTypeId());
  48.            bookForm.setAuthor(bookForm.getAuthor());
  49.            bookForm.setTranslator(bookForm.getTranslator());
  50.            bookForm.setIsbn(bookForm.getIsbn());
  51.            bookForm.setPrice(bookForm.getPrice());
  52.            bookForm.setPage(bookForm.getPage());
  53.            bookForm.setBookcaseid(bookForm.getBookcaseid());
  54.            bookForm.setStorage(bookForm.getStorage());
  55.            //获取系统日期
  56.            Date date1=new Date();
  57.            java.sql.Date date=new java.sql.Date(date1.getTime());
  58.            bookForm.setInTime(date.toString());
  59.            bookForm.setOperator(bookForm.getOperator());
  60.            int a=bookDAO.insert(bookForm);
  61.            if(a==0){
  62.                servletRequest.setAttribute("error","图书信息添加失败!");
  63.                return actionMapping.findForward("error");
  64.          }else if(a==2){
  65.              servletRequest.setAttribute("error","该图书信息已经添加!");
  66.              return actionMapping.findForward("error");
  67.          }else{
  68.              return actionMapping.findForward("bookAdd");
  69.         }
  70.        }
  71.        /***********************查询全部图书信息**************************/
  72.        private ActionForward bookQuery(ActionMapping actionMapping,ActionForm actionForm,HttpServletRequest servletRequest,HttpServletResponse servletResponse){
  73.        String str=null;
  74.        servletRequest.setAttribute("book",bookDAO.query(str));
  75.        return actionMapping.findForward("bookQuery");
  76.        }
  77.        /***********************条件查询图书信息**************************/
  78.        private ActionForward bookifQuery(ActionMapping actionMapping,ActionForm actionForm,HttpServletRequest servletRequest,HttpServletResponse servletResponse){
  79.        if(servletRequest.getParameter("f")==null){
  80.            String str=null;
  81.        }else{
  82.            String str = servletRequest.getParameter("f") + " like '%" +
  83.                         servletRequest.getParameter("key") + "%";
  84.        }
  85.        servletRequest.setAttribute("ifbook",bookDAO.query(str));
  86.        System.out.print("条件查询图书信息时的str:"+str);
  87.        return actionMapping.findForward("bookifQuery");
  88.        }
  89.         /***********************查询修改图书信息**************************/
  90.         private ActionForward bookModifyQuery(ActionMapping actionMapping,ActionForm actionForm,HttpServletRequest servletRequest,HttpServletResponse servletResponse){
  91.             BookForm bookForm=(BookForm)actionForm;
  92.             System.out.println("查询修改图书信息:"+servletRequest.getParameter("ID"));
  93.             bookForm.setId(Integer.valueOf(servletRequest.getParameter("ID")));
  94.             servletRequest.setAttribute("bookQueryif",bookDAO.queryM(bookForm));
  95.             return actionMapping.findForward("bookQueryModify");
  96.         }
  97.         /***********************查询图书详细信息**************************/
  98.         private ActionForward bookDetail(ActionMapping actionMapping,ActionForm actionForm,HttpServletRequest servletRequest,HttpServletResponse servletResponse){
  99.             BookForm bookForm=(BookForm)actionForm;
  100.             bookForm.setId(Integer.valueOf(servletRequest.getParameter("ID")));
  101.             servletRequest.setAttribute("bookDetail",bookDAO.queryM(bookForm));
  102.             return actionMapping.findForward("bookDeatil");
  103.         }
  104.         /***********************修改图书信息**************************/
  105.         private ActionForward bookModify(ActionMapping actionMapping,ActionForm actionForm,HttpServletRequest servletRequest,HttpServletResponse servletResponse){
  106.             BookForm bookForm=(BookForm)actionForm;
  107.             bookForm.setBarcode(bookForm.getBarcode());
  108.             bookForm.setBookName(bookForm.getBookName());
  109.             bookForm.setTypeId(bookForm.getTypeId());
  110.             bookForm.setAuthor(bookForm.getAuthor());
  111.             bookForm.setTranslator(bookForm.getTranslator());
  112.             bookForm.setIsbn(bookForm.getIsbn());
  113.             bookForm.setPrice(bookForm.getPrice());
  114.             bookForm.setPage(bookForm.getPage());
  115.             bookForm.setBookcaseid(bookForm.getBookcaseid());
  116.             bookForm.setStorage(bookForm.getStorage());
  117.             bookForm.setInTime(bookForm.getInTime());
  118.             bookForm.setOperator(bookForm.getOperator());
  119.             int ret=bookDAO.update(bookForm);
  120.             if(ret==0){
  121.                 servletRequest.setAttribute("error","修改图书信息失败!");
  122.                 return actionMapping.findForward("error");
  123.             }else{
  124.                 return actionMapping.findForward("bookModify");
  125.             }
  126.         }
  127.         /***********************删除图书信息**************************/
  128.         private ActionForward bookDel(ActionMapping actionMapping,ActionForm actionForm,HttpServletRequest servletRequest,HttpServletResponse servletResponse){
  129.             BookForm bookForm=(BookForm)actionForm;
  130.             bookForm.setId(Integer.valueOf(servletRequest.getParameter("ID")));
  131.             int ret=bookDAO.delete(bookForm);
  132.             if(ret==0){
  133.                 servletRequest.setAttribute("error","删除图书信息失败!");
  134.                 return actionMapping.findForward("error");
  135.             }else{
  136.                 return actionMapping.findForward("bookDel");
  137.             }
  138.         }
  139. }