⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 storehouseaction.java

📁 hibernate应用测试,初学hibernate 的会员可以看看.
💻 JAVA
字号:
package com.action;

import org.apache.struts.action.*;
import java.io.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.sql.SQLException;

import com.bean.ClientTable;
import com.bean.LogTable;
import com.dao.*;
import com.tool.pagetool.*;
import com.tool.QueryStrCreator;
import com.tool.LogTableWriter;
import com.bean.Storehouse;

public class StorehouseAction extends Action{
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("gb2312");
        response.setContentType("text/html;charset=gb2312");

        Storehouse storehouse = (Storehouse) form;
        String flag = request.getParameter("flag");
        ActionErrors actionErrors = new ActionErrors();
        String target = "";

        //添加仓库
        if (flag.equals("forA")) {
            try {
                String whereSQL="where sh_name='"+storehouse.getShName()+"' and sh_addr='"+storehouse.getShAddr()+"' and sh_kind='"+storehouse.getShKind()+"'";
                boolean tmp = StorehouseDAO.selectIsHasStorehouse(whereSQL);
                if(tmp){
                    ActionError actionError = new ActionError(
                        "errors.generally.failed","仓库名重复");
                actionErrors.add("addClientSuccese", actionError);
                saveErrors(request, actionErrors);
                target = "addStorehouse";
                return mapping.findForward(target);
                }
                StorehouseDAO.startTransation();
                int lastId = StorehouseDAO.getLastId();
                storehouse.setShId(lastId + 1);
                storehouse.setDelFlag(0);
                StorehouseDAO.insertStorehouse(storehouse);
                StorehouseDAO.commit();
                ActionError actionError = new ActionError(
                        "errors.addSuccese");
                actionErrors.add("addClientSuccese", actionError);
                saveErrors(request, actionErrors);
                target = "addStorehouse";
                //写入日志
                String operatorType = "增";
                String sql = "insert into STOREHOUSE "+
                             "(sh_id,sh_name,sh_addr,conn_peop,tel,"+
                             "fax,ku_rong,sh_kind,sh_rate,count_cost_kind,"+
                             "count_cost_unit,del_flag) values "+
                             "("+storehouse.getShId()+","+storehouse.getShName()+","+storehouse.getShAddr()+","+
                             storehouse.getConnPeop()+","+storehouse.getTel()+","+storehouse.getFax()+","+
                             storehouse.getKuRong()+","+storehouse.getShKind()+","+storehouse.getShRate()+","
                             +storehouse.getCountCostKind()+","+storehouse.getCountCostUnit()+","+storehouse.getDelFlag()+")";

                LogTableWriter.write(request, operatorType, sql);
            } catch (SQLException e) {
                e.printStackTrace();
                ActionError actionError = new ActionError("errors.addFailed");
                actionErrors.add("addClientFeiled", actionError);
                saveErrors(request, actionErrors);
                target = "addStorehouse";
            } finally {
                StorehouseDAO.rollback();
            }
        }

        //列出仓库修改或删除列表
       if (!flag.equals("forA")) {

           if (flag.equals("forM")) {
               target = "listForMStorehouse"; //重定向到用户修改例表
               initPage(request); //初始化分页数据并放于request中
           } else if (flag.equals("forD")) {
               target = "deleteStorehouse"; //重定向到用户删除列表
               initPage(request); //初始化分页数据并放于request中
           } else if (flag.equals("forDiscount")) {
               target = "";
               initPage(request); //初始化分页数据并放于request中
           }
       }

       //跳转修改仓库
        if (flag.equals("modify")) {
            if ((storehouse.getShName() == null || storehouse.getShName().equals(""))&&
                storehouse.getShId() != 0) { //跳转到信息修改页面
                try {
                    Storehouse storehouse2 = StorehouseDAO.selectStorehouseByshId(
                            storehouse.getShId());
                    request.setAttribute("forMStorehouse", storehouse2);

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    CompUserDAO.rollback();
                }
                target = "modifStorehouse";
            }

            if (storehouse.getShName() != null &
                storehouse.getShId() != 0 &&!storehouse.getShName().equals("")) { //执行修改
                String whereSQL="where sh_name='"+storehouse.getShName()+"' and sh_addr='"+storehouse.getShAddr()+"' and sh_kind='"+storehouse.getShKind()+"' and sh_id<>"+storehouse.getShId();
                boolean tmp = StorehouseDAO.selectIsHasStorehouse(whereSQL);
                if(tmp){
                    ActionError actionError = new ActionError(
                        "errors.generally.failed","仓库名重复");
                actionErrors.add("addClientSuccese", actionError);
                saveErrors(request, actionErrors);
                request.setAttribute("forMStorehouse", storehouse);
                target = "modifStorehouse";
                return mapping.findForward(target);
                }
                try {
                    StorehouseDAO.updateStorehouse(storehouse);
                    request.setAttribute("forMStorehouse", storehouse);
                    ActionError actionError = new ActionError(
                            "errors.modifyUserInfoSuccese");
                    actionErrors.add("modifyCUserSuccese", actionError);
                    saveErrors(request, actionErrors);
                    //写入日志
                    String operatorType = "改";
                    String sql = "update STOREHOUSE set "+
                                 "SH_ID="+storehouse.getShId()+",SH_NAME="+storehouse.getShName()+","+
                                 "SH_ADDR="+storehouse.getShAddr()+",CONN_PEOP="+storehouse.getConnPeop()+","+
                                 "TEL"+storehouse.getTel()+",FAX="+storehouse.getFax()+",KU_RONG="+storehouse.getKuRong()+","+
                                 "SH_KIND="+storehouse.getShKind()+",SH_RATE="+storehouse.getShRate()+","+
                                 "COUNT_COST_KIND="+storehouse.getCountCostKind()+","+
                                 "COUNT_COST_UNIT="+storehouse.getCountCostUnit()+","+
                                 "DEL_FLAG="+storehouse.getDelFlag()+" "+
                                 "where SH_ID = "+storehouse.getShId();
                    LogTableWriter.write(request, operatorType, sql);

                } catch (Exception e) {
                    e.printStackTrace();
                    ActionError actionError = new ActionError(
                            "errors.modifyUserInfoFailed");
                    actionErrors.add("modifyCUserFeiled", actionError);
                    saveErrors(request, actionErrors);
                } finally {
                    CompUserDAO.rollback();
                }
                target = "modifStorehouse";
            }
        }

        //删除仓库,只作标记不物理删除
       if (flag.equals("delete")) {
           String shIdStr = request.getParameter("shId");
           int shId = -1;
           target = "deleteStorehouse";
//           System.out.println("-------------||"+shIdStr);
           if (shIdStr != null) {
               try {
                   StorehouseDAO.startTransation();
                   shId = Integer.parseInt(shIdStr);
                   Storehouse storehouse2 = StorehouseDAO.selectStorehouseByshId(
                           shId);
                   boolean tmp = false;
                   tmp=StorehouseDAO.selectStorehouseIsHasRefer(storehouse2);
                   if (tmp) {
                       ActionError actionError = new ActionError(
                               "errors.generally.failed","仓库已被引用");
                       actionErrors.add("deleteCUserSuccese", actionError);
                       saveErrors(request, actionErrors);
                       StorehouseDAO.commit();
                       initPage(request); //初始化分页数据并放于request中
                       return mapping.findForward(target);
                   }
                   storehouse2.setDelFlag(1);
                   StorehouseDAO.updateStorehouseSI(storehouse2);
                   ActionError actionError = new ActionError(
                           "errors.deleteSuccese");
                   actionErrors.add("deleteCUserSuccese", actionError);
                   saveErrors(request, actionErrors);
                   StorehouseDAO.commit();
                   initPage(request); //初始化分页数据并放于request中
                   //写入日志
                   String operatorType = "删";
                   String sql = "update STOREHOUSE set DEL_FLAG="+storehouse2.getDelFlag()+" where SH_ID = "+storehouse2.getShId();

                   LogTableWriter.write(request, operatorType, sql);

               } catch (Exception e) {
                   e.printStackTrace();
                   ActionError actionError = new ActionError(
                           "errors.deleteFeiled");
                   actionErrors.add("deleteCUserFeiled", actionError);
                   saveErrors(request, actionErrors);
               } finally {
                   StorehouseDAO.rollback();
               }
           }
       }


        return mapping.findForward(target);
    }

    //初始化并获得分页
   public Page initPage(HttpServletRequest request) {
       String currentPageStr = request.getParameter("currentPage");
       String queryStr = QueryStrCreator.create("storehouse", request);
       int currentPage = 1; //当前页码
       int size = 20; //每页的记录数

       try { //防止从地址栏直接输入非法页码
           if (currentPageStr != null) currentPage = new Integer(
                   currentPageStr).intValue();
       } catch (Exception e) {
           e.printStackTrace();
       }

       //处理分页
       //会话中是否已有分页生成工具
       PageTool pageTool = null;
       HttpSession session = request.getSession();
       pageTool = (PageTool) session.getAttribute("storehousePageTool"); //有,则直接拿来用
       if (pageTool == null) { //没有,则新建,并放进会话中,以备下次用
           pageTool = PageToolBuilder.builder(new StorehouseDAO());
           session.setAttribute("storehousePageTool", pageTool);
       }
       //由分页工具生成分页
       Page page = null;
       try {
           String flag = request.getParameter("flag");
           if (flag.equals("query")) //查询时的分页
               page = pageTool.createPage(currentPage, size, queryStr);
           else //其它非查询的分页
               page = pageTool.createPage(currentPage, size);
       } catch (SQLException ex) {
           ex.printStackTrace();
       }
       //将分页放进请求中,分发到下一个页面显示
       request.setAttribute("allStorehousePage", page);
       return page;
   }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -