十四、增加EmployeeService实现用户添加

1.添加EmployeeAction,用来处理mainFrame界面中,用户提出的增删查改请求

  1.1com.myz.web.actions包下新建EmployeeAction

package com.myz.web.actions;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.myz.domain.Employee;
import com.myz.service.interfaces.DepartmentServiceInterface;
import com.myz.service.interfaces.EmployeeServiceInterface;
import com.myz.web.forms.EmployeeForm;

public class EmployeeAction extends DispatchAction {//跳往添加雇员界面
    public ActionForward goAddEmp(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // TODO Auto-generated method stub
        return mapping.findForward("goAddEmp");
    }
    
}

 

  1.2struts-config.xml中配置EmployeeAction

<action path="/employee" parameter="flag" name="employeeForm">
        <!-- 跳往雇员添加界面 -->
            <forward name="goAddEmp" path="/WEB-INF/addEmpUI.jsp"></forward>
        </action>

 

  1.3applicationContext.xml中配置EmployeeAction

<!-- 配置EmployeeAction -->
    <bean name="/employee" class="com.myz.web.actions.EmployeeAction" scope="prototype"></bean>

  1.4WEB-INF目录下新建addEmpUI.jsp

<body>
    <h1>添加雇员</h1>
    <form action="${pageContext.request.contextPath}/employee.do?flag=addEmp"" method="post">
    <table>
        <tr><td>id:</td><td><input type="text" name="id"></td></tr>
        <tr><td>名字:</td><td><input type="text" name="name"></td></tr>
        <tr><td>邮件:</td><td><input type="text" name="email"></td></tr>
        <tr><td>密码:</td><td><input type="password" name="password"></td></tr>
        <tr><td>grade:</td><td><input type="text" name="grade"></td></tr>
        <tr><td>工资:</td><td><input type="text" name="salary"></td></tr>
        
        <tr><td>部门:</td><td>
            <select name="departmentid">
                <option value="1">财务部</option>
                <option value="2">人事部</option>
            </select>
        </td></tr>
        
        <tr><td><input type="submit" value="登录"></td><td><input type="reset" value="重置"></td></tr>
    </table>
        
    </form>
  </body>

  1.5完善EmployeeForm,以便addEmpUI.jsp的提交信息能够填充进去

package com.myz.web.forms;

import org.apache.struts.action.ActionForm;

public class EmployeeForm extends ActionForm {
    //属性名称与addEmpUI.jsp中的控件名称一样
    private Integer id;
    private String password;
    private String email;
    private Integer grade;
    private String name;
    private float salary;
    private Integer departmentid;
    
    public String getEmail() {
        return email;
    }
    public Integer getDepartmentid() {
        return departmentid;
    }
    public void setDepartmentid(Integer departmentid) {
        this.departmentid = departmentid;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public Integer getGrade() {
        return grade;
    }
    public void setGrade(Integer grade) {
        this.grade = grade;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public float getSalary() {
        return salary;
    }
    public void setSalary(float salary) {
        this.salary = salary;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

 

  1.6在struts-config.xml中将Employeeform与EmployeeAction相关联,并增加addEmp函数的forward

<action path="/employee" parameter="flag" name="employeeForm">
            <forward name="goAddEmp" path="/WEB-INF/addEmpUI.jsp"></forward>
        </action>

  1.7EmployeeAction中新建addEmp函数

package com.myz.web.actions;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.myz.domain.Employee;
import com.myz.service.interfaces.DepartmentServiceInterface;
import com.myz.service.interfaces.EmployeeServiceInterface;
import com.myz.web.forms.EmployeeForm;

public class EmployeeAction extends DispatchAction {
    @Resource
    private DepartmentServiceInterface departmentService;
    
    @Resource
    private EmployeeServiceInterface employeeService;
    
    public EmployeeServiceInterface getEmployeeService() {
        return employeeService;
    }

    public void setEmployeeService(EmployeeServiceInterface employeeService) {
        this.employeeService = employeeService;
    }

    public DepartmentServiceInterface getDepartmentService() {
        return departmentService;
    }

    public void setDepartmentService(DepartmentServiceInterface departmentService) {
        this.departmentService = departmentService;
    }

    //跳往添加雇员界面
    public ActionForward goAddEmp(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // TODO Auto-generated method stub
        return mapping.findForward("goAddEmp");
    }
    
    //添加雇员
    public ActionForward addEmp(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // TODO Auto-generated method stub
        EmployeeForm employeeForm=(EmployeeForm) form;
        
        Employee e=new Employee();
        e.setName(employeeForm.getName());
        e.setEmail(employeeForm.getEmail());
        e.setGrade(employeeForm.getGrade());
        e.setId(employeeForm.getId());
        e.setHiredate(new Date());
        e.setPassword(employeeForm.getPassword());
        e.setSalary(employeeForm.getSalary());
        System.out.println(employeeForm.getDepartmentid());
        System.out.println(departmentService.getDepartmentById(employeeForm.getDepartmentid()));
        e.setDepartment(departmentService.getDepartmentById(employeeForm.getDepartmentid()));
        
        try {
            employeeService.addEmployee(e);
        } catch (Exception e2) {
            // TODO: handle exception
            return mapping.findForward("err");
        }
        return mapping.findForward("ok");
    }
}

  1.8增加ok.jsp和err.jsp

<body>
    <h2>操作成功!</h2>
    <a href="#">继续添加</a><br/>
    <a href="#">返回主界面</a>
  </body>
<body>
    <h2>操作失败</h2>
    <a href="#">返回主界面</a>
  </body>

   1.9配置全局跳转的forward

<global-forwards>
        <!-- 跳往雇员添加成功界面 -->
            <forward name="ok" path="/WEB-INF/ok.jsp"></forward>
        <!-- 跳往雇员添加失败界面 -->
            <forward name="err" path="/WEB-INF/err.jsp"></forward>
    </global-forwards>

 

2.测试

  在登录成功后,添加一个员工,在id不重复,其它属性类型填写正确的情况下,将会添加成功!

posted @ 2018-02-10 17:29  菜鸡蔡文姬  阅读(175)  评论(0编辑  收藏  举报