employeelistaction.java

来自「100多M的J2EE培训内容」· Java 代码 · 共 131 行

JAVA
131
字号
package strutsds;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.ArrayList;


public class EmployeeListAction extends Action {

  protected ArrayList getEmployees() {
    Employee employee = null;
    //1.new Collection to contains the Model object employee
    ArrayList employees = new ArrayList();

    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    ServletContext context = servlet.getServletContext();
    DataSource dataSource = (DataSource)context.getAttribute(Action.DATA_SOURCE_KEY);
    try {
      conn = dataSource.getConnection();
      stmt = conn.createStatement();
      rs =stmt.executeQuery("select * from employees, roles,"
                            +
                            "departments where employees.roleid=roles.roleid "
                            + "and employees.depid=departments.depid");
      while (rs.next()) {
       //2. get Employee() object
        employee = new Employee();
        employee.setUsername(rs.getString("username"));
        employee.setName(rs.getString("name"));
        employee.setRolename(rs.getString("rolename"));
        employee.setPhone(rs.getString("phone"));
        employee.setEmail(rs.getString("email"));
        employee.setRoleid(new Integer(rs.getInt("roleid")));
        employee.setDepid(new Integer(rs.getInt("depid")));
        employee.setDepartment(rs.getString("depname"));
        //3.add it to the collection
        employees.add(employee);
        System.err.println("in EmployeeActionList ---Username : "
                           + employee.getUsername()
                           + " Department : " + rs.getString("depname"));
      }
    }
    catch (SQLException e) {
      System.err.println(e.getMessage());
    }
    finally {
      if (rs != null) {
        try {
          rs.close();
        }
        catch (SQLException sqle) {
          System.err.println(sqle.getMessage());
        }
        rs = null;
      }
      if (stmt != null) {
        try {
          stmt.close();
        }
        catch (SQLException sqle) {
          System.err.println(sqle.getMessage());
        }
        stmt = null;
      }
      if (conn != null) {
        try {
          conn.close();
        }
        catch (SQLException sqle) {
          System.err.println(sqle.getMessage());
        }
        conn = null;
      }
    }
    return employees;
  }

  public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse httpServletResponse) {
// Forward to the appropriate View
    /*Add any custom ActionMappings to the application. Again, this step is optional; it’s only necessary
 when your application defines a custom ActionMapping. In this application, we’ve added a custom
 ActionMapping that will be associated with the Action class; the custom mapping will specify
 whether the user must be logged in to perform the Action that it applies to. The default value is false,
 which allows a user who is not logged in to execute the Action. Our custom ActionMapping is shown
 in Listing 11.6.

     EmployeesActionMapping employeesMapping =
(EmployeesActionMapping)mapping;
// Does this action require the user to login
if ( employeesMapping.isLoginRequired() ) {
      HttpSession session = request.getSession();
      if (session.getAttribute("USER") == null) {
// The user is not logged in
        target = new String("login");
        ActionErrors errors = new ActionErrors();
        errors.add(ActionErrors.GLOBAL_ERROR,
                   new ActionError("errors.login.required"));
// Report any errors we have discovered back to
// the original form
        if (!errors.empty()) {
          saveErrors(request, errors);
        }
      }
    }*/


    //4.bind the Collection to session
    request.setAttribute("employees",getEmployees());
    return (mapping.findForward("success"));
  }
}

⌨️ 快捷键说明

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