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

📄 clienttableaction.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;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class ClientTableAction extends Action {
    public ClientTableAction() {
        try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

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

        ClientTable clientTable = (ClientTable) form;
        String flag = request.getParameter("flag");
        ActionErrors actionErrors = new ActionErrors();
        String target = "";
//        InputStream is = request.getInputStream();
        //添加用户
        if (flag.equals("forA")) {
            try {
                //判断客户名是否已存在
                String whereStr="where client_name = '"+clientTable.getClientName()+"' and seat_num='"+clientTable.getSeatNum()+"'";
                System.out.println("%"+clientTable.getSeatNum()+"%");
                boolean tmp = ClientTableDAO.isHasClient(whereStr);
                if (tmp) {
                    ActionError actionError = new ActionError(
                            "errors.generally.failed","客户名重复");
                    actionErrors.add("addClientSuccese", actionError);
                    saveErrors(request, actionErrors);
                    target = "addClient";
                    return mapping.findForward(target);
                }
                ClientTableDAO.startTransation();
                int lastId = ClientTableDAO.getLastId();
                clientTable.setClientId(lastId + 1);
                clientTable.setDelFlag(0);
                ClientTableDAO.insertClientTable(clientTable);
                ClientTableDAO.commit();
                ActionError actionError = new ActionError(
                        "errors.addSuccese");
                actionErrors.add("addClientSuccese", actionError);
                saveErrors(request, actionErrors);
                target = "addClient";
                //写入日志
                String operatorType = "增";
                String sql = "insert into CLIENT_TABLE " +
                             "(client_id,client_name,seat_num,corpor_repre," +
                             "conn_people,conn_tel,fax,handset,conn_addr,discount,del_flag)" +
                             " values (" + clientTable.getClientId() + "," +
                             clientTable.getClientName()
                             + "," + clientTable.getSeatNum() + "," +
                             clientTable.getCorporRepre()
                             + "," + clientTable.getConnPeople() + "," +
                             clientTable.getConnTel()
                             + "," + clientTable.getFax() + "," +
                             clientTable.getHandset() + "," +
                             clientTable.getConnAddr()
                             + "," + clientTable.getDiscount() + "," +
                             clientTable.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 = "addClient";
            } finally {
                ClientTableDAO.rollback();
            }
        }

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

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

        //删除用户,只作标记不物理删除
        if (flag.equals("delete")) {
            String clientIdStr = request.getParameter("clientId");
            int clientId = -1;
            target = "deleteClient";
            if (clientIdStr != null) {
                try {
                    ClientTableDAO.startTransation();
                    clientId = Integer.parseInt(clientIdStr);
                    ClientTable clientTable2 = ClientTableDAO.
                                               selectClientTableByclientId(
                            clientId);
                    boolean tmp = false;
                    tmp = ClientTableDAO.isHasReferClient(clientTable2);
                    if (tmp) {
                        ActionError actionError = new ActionError(
                                "errors.generally.failed", "客户已被引用");
                        actionErrors.add("deleteCUserSuccese", actionError);
                        saveErrors(request, actionErrors);
                        ClientTableDAO.commit();
                        initPage(request); //初始化分页数据并放于request中
                        return mapping.findForward(target);
                    }
                    clientTable2.setDelFlag(1);
                    ClientTableDAO.updateClientTableForD(clientTable2);
                    ActionError actionError = new ActionError(
                            "errors.deleteSuccese");
                    actionErrors.add("deleteCUserSuccese", actionError);
                    saveErrors(request, actionErrors);
                    ClientTableDAO.commit();
                    initPage(request); //初始化分页数据并放于request中
                    //写入日志
                    String operatorType = "删";
                    String sql = "update CLIENT_TABLE set DEL_FLAG=" +
                                 clientTable2.getDelFlag() +
                                 " where CLIENT_ID = " +
                                 clientTable2.getClientId();

                    LogTableWriter.write(request, operatorType, sql);

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

        //修改用户
        if (flag.equals("modify")) {
            if ((clientTable.getClientName() == null||clientTable.getClientName().equals("")) &&
                clientTable.getClientId() != 0) { //跳转到信息修改页面
                try {
                    ClientTable clientTmp = ClientTableDAO.
                                            selectClientTableByclientId(
                            clientTable.getClientId());
                    request.setAttribute("forMClient", clientTmp);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    CompUserDAO.rollback();
                }
                target = "modifyClient";
            }
            if (clientTable.getClientName() != null &
                clientTable.getClientId() != 0&&!clientTable.getClientName().equals("")) { //执行修改
                try {
                    String whereStr="where client_name = '"+clientTable.getClientName()+"' and seat_num='"+clientTable.getSeatNum()+"' and client_id<>"+clientTable.getClientId();
               boolean tmp = ClientTableDAO.isHasClient(whereStr);
               if (tmp) {
                   ActionError actionError = new ActionError(
                           "errors.generally.failed","客户名重复");
                   actionErrors.add("addClientSuccese", actionError);
                   saveErrors(request, actionErrors);
                   request.setAttribute("forMClient", clientTable);
                   target = "modifyClient";
                   return mapping.findForward(target);
               }

                    ClientTableDAO.updateClientTable(clientTable);
                    request.setAttribute("forMClient", clientTable);
                    ActionError actionError = new ActionError(
                            "errors.modifyUserInfoSuccese");
                    actionErrors.add("modifyCUserSuccese", actionError);
                    saveErrors(request, actionErrors);
                    //写入日志
                    String operatorType = "改";
                    String sql = "update CLIENT_TABLE " +
                                 "set CLIENT_ID=" + clientTable.getClientId() +
                                 "," +
                                 "CLIENT_NAME=" + clientTable.getClientName() +
                                 "," +
                                 "SEAT_NUM=" + clientTable.getSeatNum() + "," +
                                 "CORPOR_REPRE=" + clientTable.getCorporRepre() +
                                 "," +
                                 "CONN_PEOPLE=" + clientTable.getConnPeople() +
                                 "," +
                                 "CONN_TEL=" + clientTable.getConnTel() +
                                 ",FAX=" + clientTable.getFax() + "," +
                                 "HANDSET=" + clientTable.getHandset() +
                                 ",CONN_ADDR=" + clientTable.getConnAddr() +
                                 "," +
                                 "DISCOUNT=" + clientTable.getDiscount() +
                                 ",DEL_FLAG=" + clientTable.getDelFlag() + " " +
                                 "where CLIENT_ID = " + clientTable.getClientId();

                    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 = "modifyClient";
            }
        }

        //设置客户的折扣
        if (flag.equals("modifyDiscount")) {
            try {
                ClientTableDAO.startTransation();
                ClientTableDAO.updateClientTableForDiscount(clientTable);
                ClientTableDAO.commit();
                ActionError actionError = new ActionError(
                        "errors.modifyUserInfoSucces");
                actionErrors.add("modifyCUserSuccese", actionError);
                saveErrors(request, actionErrors);

                //写入日志
                String operatorType = "改";
                String sql = "update CLIENT_TABLE set DISCOUNT=" +
                             clientTable.getDiscount() + " where CLIENT_ID = " +
                             clientTable.getClientId();

                LogTableWriter.write(request, operatorType, sql);

            } catch (SQLException e) {
                e.printStackTrace();
                ActionError actionError = new ActionError(
                        "errors.modifyUserInfoFailed");
                actionErrors.add("modifyCUserFeiled", actionError);
                saveErrors(request, actionErrors);

            } finally {
                ClientTableDAO.rollback();
            }
            target = "modifyDiscount";
        }
        //查询用户
        if (flag.equals("query")) {
            initPage(request); //初始化分页数据并放于request中
            target = "queryClient";
        }

        //为导入提供的查询
        if (flag.equals("queryForImport")) {
            initPage(request); //初始化分页数据并放于request中
            target = "importClientForAddAccount";
        }


        return mapping.findForward(target);
    }

    //初始化并获得分页
    private Page initPage(HttpServletRequest request) {
        String currentPageStr = request.getParameter("currentPage");
        String queryStr = QueryStrCreator.create("clientTable", request);
//        System.out.println("queryStr="+queryStr);
//        System.out.println(queryStr);
        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("clientPageTool"); //有,则直接拿来用
        if (pageTool == null) { //没有,则新建,并放进会话中,以备下次用
            pageTool = PageToolBuilder.builder(new ClientTableDAO());
            session.setAttribute("clientPageTool", pageTool);
        }
        //由分页工具生成分页
        Page page = null;
        try {
            String flag = request.getParameter("flag");
            if (flag.equals("query")||flag.equals("queryForImport")) //查询时的分页
                page = pageTool.createPage(currentPage, size, queryStr);
            else //其它非查询的分页
                page = pageTool.createPage(currentPage, size);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        //将分页放进请求中,分发到下一个页面显示
        request.setAttribute("allClientPage", page);
        return page;
    }

    private void jbInit() throws Exception {
    }
}

⌨️ 快捷键说明

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