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

📄 employeeaction.java

📁 ssi学习资料
💻 JAVA
字号:
package css.web.demo.action;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.*;
import org.apache.struts.actions.DispatchAction;

import css.web.demo.form.EmployeeForm;
import css.web.demo.model.Employee;
import css.web.demo.service.DepartmentService;
import css.web.demo.service.EmployeeService;
import css.web.demo.util.Constants;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class EmployeeAction extends DispatchAction {
    private Log logger = LogFactory.getLog(this.getClass());
    private EmployeeService empService;
    private DepartmentService deptService;

    public EmployeeAction(EmployeeService empService, DepartmentService deptService) {
        super();
        this.empService = empService;
        this.deptService = deptService;
    }

    public ActionForward getEmployees(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        logger.debug("getEmployees");
        populateEmployees(request);
        return mapping.findForward(Constants.SUCCESS);
    }

    public ActionForward setUpForInsertOrUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        logger.debug("setUpForInsertOrUpdate");
        EmployeeForm employeeForm = (EmployeeForm)form;
        if (isUpdate(request, employeeForm)) {
            Integer id = Integer.valueOf(employeeForm.getEmployeeId());
            Employee employee = empService.getEmployee(id);
            BeanUtils.copyProperties(employeeForm, employee);
        }
        prep(request);
        return mapping.findForward(Constants.SUCCESS);
    }

    public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        logger.debug("delete");
        EmployeeForm employeeForm = (EmployeeForm)form;
        Integer id = Integer.valueOf(employeeForm.getEmployeeId());
        empService.deleteEmployee(id);
        populateEmployees(request);
        return mapping.findForward(Constants.SUCCESS);
    }

    public ActionForward insertOrUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        logger.debug("insertOrUpdate");
        EmployeeForm employeeForm = (EmployeeForm)form;
        if (validationSuccessful(request, employeeForm)) {
            Employee employee = new Employee();
            BeanUtils.copyProperties(employee, employeeForm);
            if (isUpdate(request, employeeForm)) {
                logger.debug("update");
                empService.updateEmployee(employee);
            } else {
                logger.debug("insert");
                empService.insertEmployee(employee);
            }
            populateEmployees(request);
            return mapping.findForward(Constants.SUCCESS);
        } else {
            prep(request);
            return mapping.findForward(Constants.FAILURE);
        }
    }

    private void populateEmployees(HttpServletRequest request) {
        List employees = empService.getAllEmployees();
        request.setAttribute(Constants.EMPLOYEES, employees);
    }

    private void prep(HttpServletRequest request) {
        request.setAttribute(Constants.DEPARTMENTS, deptService.getAllDepartments());
    }

    private boolean isUpdate(HttpServletRequest request, EmployeeForm empForm) {
        boolean updateFlag = true;
        //if ID is null or 0 we know we are doing an insert. You could check other
        //things to decide, like a dispatch param
        //It's annoying that BeanUtils will convert nulls to 0 so have to do 0 check also,
        //or you could register a converter, which is the preferred way to handle it, but goes
        //beyond this demo
        String id = empForm.getEmployeeId();
        if (id == null || id.trim().length() == 0 || Integer.parseInt(id) == 0) {
            updateFlag = false;
        }
        return updateFlag;
    }

    private boolean validationSuccessful(HttpServletRequest request, EmployeeForm form) {
        //if you really like using the validation framework stuff, you can just
        //call  ActionErrors errors = form.validate( mapping, request ); in this method
        //and check for errors being empty, if not save them and you're done.
        //I end up finding the validation framework a bit annoying to work with, so I do it
        //old-Skool way. Inevitably in a more complex app you end up having to perform
        //more complex validation than the validation framework provides, so I just assume
        //keep it all here in one place, versus having some handled by xml configuration and
        //some hardcoded.
        boolean isOk = true;
        ActionMessages errors = new ActionMessages();
        if (form.getAge() == null || form.getAge().trim().length() == 0) {
            errors.add("age", new ActionMessage("errors.required", "Age"));
        } else {
            try {
                Integer.parseInt(form.getAge());
            } catch (NumberFormatException e) {
                errors.add("age", new ActionMessage("errors.number", "Age"));
            }
        }
        if (form.getFirstName() == null || form.getFirstName().trim().length() == 0) {
            errors.add("firstName", new ActionMessage("errors.required", "First Name"));
        }
        if (form.getLastName() == null || form.getLastName().trim().length() == 0) {
            errors.add("lastName", new ActionMessage("errors.required", "Last Name"));
        }
        if (!errors.isEmpty()) {
            //saveErrors(request, errors);
            isOk = false;
        }
        return isOk;
    }
    /*
    private void saveSuccessMessage( HttpServletRequest request, String messageResource ) {
        ActionMessages messages = new ActionMessages();
        messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(messageResource));
        saveMessages(request, messages);
    }
    */
}

⌨️ 快捷键说明

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