GetEmployeeAction.java~5~
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:6k
源码类别:

Java编程

开发平台:

Java

  1. package strutsds;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import javax.servlet.http.HttpSession;
  7. import org.apache.struts.action.Action;
  8. import org.apache.struts.action.ActionForm;
  9. import org.apache.struts.action.ActionForward;
  10. import org.apache.struts.action.ActionMapping;
  11. import org.apache.struts.action.ActionErrors;
  12. import org.apache.struts.action.ActionError;
  13. import javax.sql.DataSource;
  14. import java.sql.Connection;
  15. import java.sql.Statement;
  16. import java.sql.ResultSet;
  17. import java.sql.SQLException;
  18. public class GetEmployeeAction extends Action {
  19.   protected ActionForm buildEmployeeForm(String username)throws Exception {
  20.     String user = null;
  21.     Connection conn = null;
  22.     Statement stmt = null;
  23.     ResultSet rs = null;
  24.     EmployeeActionForm form = null;
  25.     DataSource dataSource = (DataSource)servlet.getServletContext().getAttribute(Action.DATA_SOURCE_KEY);
  26.     try {
  27.       conn = dataSource.getConnection();
  28.       stmt = conn.createStatement();
  29.       rs =stmt.executeQuery("select * from employees "
  30.                             + "where username='"
  31.                             + username + "'");
  32.                             if ( rs.next() ) {
  33.                             form = new EmployeeActionForm();
  34.                             form.setUsername(rs.getString("username"));
  35.                             form.setPassword(rs.getString("password"));
  36.                             form.setDepid(rs.getString("depid"));
  37.                             form.setRoleid(rs.getString("roleid"));
  38.                             form.setName(rs.getString("name"));
  39.                             form.setPhone(rs.getString("phone"));
  40.                             form.setEmail(rs.getString("email"));
  41.                             }
  42.                             else {
  43.                             throw new Exception("Employee " + username
  44.                             + " not found!");
  45.                             }
  46.                             }
  47.                             finally {
  48.                             if (rs != null) {
  49.                             rs.close();
  50.                             }
  51.                             if (stmt != null) {
  52.                             stmt.close();
  53.                             }
  54.                             if (conn != null) {
  55.                             conn.close();
  56.                             }
  57.                             }
  58.                             return form;
  59.   }
  60.   public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse httpServletResponse) {
  61.     /*@todo: complete the business logic here, this is just a skeleton. */
  62.     // Default target to success
  63.                             String target = "success";
  64.                             /*EmployeesActionMapping employeesMapping =
  65.                             (EmployeesActionMapping)mapping;
  66. // Does this action require the user to login
  67.                             if ( employeesMapping.isLoginRequired() ) {
  68.                             HttpSession session = request.getSession();
  69.                             if ( session.getAttribute("USER") == null ) {
  70. // The user is not logged in
  71.                             target = "login";
  72.                             ActionErrors errors = new ActionErrors();
  73.                             errors.add(ActionErrors.GLOBAL_ERROR,
  74.                             new ActionError("errors.login.required"));
  75. // Report any errors we have discovered back to the
  76. // original form
  77.                             if (!errors.empty()) {
  78.                             saveErrors(request, errors);
  79.                             }
  80.                             return (mapping.findForward(target));
  81.                             }
  82.                             }*/
  83.                             if ( isCancelled(request) ) {
  84.                         // Cancel pressed back to employee list
  85.                             return (mapping.findForward(target));
  86.                             }
  87.                             try {
  88.                         // Build the EmployeeForm with the Retrieved values
  89.                             form =buildEmployeeForm(request.getParameter("username"));
  90.                             System.out.println("in getemployeeaction:"+request.getParameter("username")+"mapping:"+mapping.getAttribute()+"scope:"+mapping.getScope());
  91.                             // Add the form to the request or session, bound to the
  92.                             // key named in the <action> attribute name
  93.                             if ("request".equals(mapping.getScope())) {
  94.                               request.setAttribute(mapping.getAttribute(), form);
  95.                             }
  96.                             else {
  97.                               HttpSession session = request.getSession();
  98.                               session.setAttribute(mapping.getAttribute(), form);
  99.                             }
  100.                             } catch (Exception e) {
  101.                               target = "error";
  102.                               ActionErrors errors = new ActionErrors();
  103.                               errors.add(ActionErrors.GLOBAL_ERROR,
  104.                                          new ActionError("errors.database.error",e.getMessage()));
  105.                               // Report any errors
  106.                             if (!errors.empty()) {
  107.                               saveErrors(request, errors);
  108.                             }
  109.                             }
  110.                             // Forward to the appropriate View
  111.                             return (mapping.findForward(target));
  112.   }
  113. }