Book.java~27~
上传用户: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.        System.out.print("条件查询图书信息时的str:"+str);
  76.        return actionMapping.findForward("bookQuery");
  77.        }
  78.        /***********************条件查询图书信息**************************/
  79.        private ActionForward bookifQuery(ActionMapping actionMapping,ActionForm actionForm,HttpServletRequest servletRequest,HttpServletResponse servletResponse){
  80.        String str=servletRequest.getParameter("f")+"like '%"+servletRequest.getParameter("key")+"%";
  81.        servletRequest.setAttribute("ifbook",bookDAO.query(str));
  82.        return actionMapping.findForward("bookifQuery");
  83.        }
  84.         /***********************查询修改图书信息**************************/
  85.         private ActionForward bookModifyQuery(ActionMapping actionMapping,ActionForm actionForm,HttpServletRequest servletRequest,HttpServletResponse servletResponse){
  86.             BookForm bookForm=(BookForm)actionForm;
  87.             System.out.println("查询修改图书信息:"+servletRequest.getParameter("ID"));
  88.             bookForm.setId(Integer.valueOf(servletRequest.getParameter("ID")));
  89.             servletRequest.setAttribute("bookQueryif",bookDAO.queryM(bookForm));
  90.             return actionMapping.findForward("bookQueryModify");
  91.         }
  92.         /***********************查询图书详细信息**************************/
  93.         private ActionForward bookDetail(ActionMapping actionMapping,ActionForm actionForm,HttpServletRequest servletRequest,HttpServletResponse servletResponse){
  94.             BookForm bookForm=(BookForm)actionForm;
  95.             bookForm.setId(Integer.valueOf(servletRequest.getParameter("ID")));
  96.             servletRequest.setAttribute("bookDetail",bookDAO.queryM(bookForm));
  97.             return actionMapping.findForward("bookDeatil");
  98.         }
  99.         /***********************修改图书信息**************************/
  100.         private ActionForward bookModify(ActionMapping actionMapping,ActionForm actionForm,HttpServletRequest servletRequest,HttpServletResponse servletResponse){
  101.             BookForm bookForm=(BookForm)actionForm;
  102.             bookForm.setBarcode(bookForm.getBarcode());
  103.             bookForm.setBookName(bookForm.getBookName());
  104.             bookForm.setTypeId(bookForm.getTypeId());
  105.             bookForm.setAuthor(bookForm.getAuthor());
  106.             bookForm.setTranslator(bookForm.getTranslator());
  107.             bookForm.setIsbn(bookForm.getIsbn());
  108.             bookForm.setPrice(bookForm.getPrice());
  109.             bookForm.setPage(bookForm.getPage());
  110.             bookForm.setBookcaseid(bookForm.getBookcaseid());
  111.             bookForm.setStorage(bookForm.getStorage());
  112.             bookForm.setInTime(bookForm.getInTime());
  113.             bookForm.setOperator(bookForm.getOperator());
  114.             int ret=bookDAO.update(bookForm);
  115.             if(ret==0){
  116.                 servletRequest.setAttribute("error","修改图书信息失败!");
  117.                 return actionMapping.findForward("error");
  118.             }else{
  119.                 return actionMapping.findForward("bookModify");
  120.             }
  121.         }
  122.         /***********************删除图书信息**************************/
  123.         private ActionForward bookDel(ActionMapping actionMapping,ActionForm actionForm,HttpServletRequest servletRequest,HttpServletResponse servletResponse){
  124.             BookForm bookForm=(BookForm)actionForm;
  125.             bookForm.setId(Integer.valueOf(servletRequest.getParameter("ID")));
  126.             int ret=bookDAO.delete(bookForm);
  127.             if(ret==0){
  128.                 servletRequest.setAttribute("error","删除图书信息失败!");
  129.                 return actionMapping.findForward("error");
  130.             }else{
  131.                 return actionMapping.findForward("bookDel");
  132.             }
  133.         }
  134. }