GetEmployeeAction.java
上传用户: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.     EmployeeActionForm ef=null;
  64.                             String target = "success";
  65.                             /*EmployeesActionMapping employeesMapping =
  66.                             (EmployeesActionMapping)mapping;
  67. // Does this action require the user to login
  68.                             if ( employeesMapping.isLoginRequired() ) {
  69.                             HttpSession session = request.getSession();
  70.                             if ( session.getAttribute("USER") == null ) {
  71. // The user is not logged in
  72.                             target = "login";
  73.                             ActionErrors errors = new ActionErrors();
  74.                             errors.add(ActionErrors.GLOBAL_ERROR,
  75.                             new ActionError("errors.login.required"));
  76. // Report any errors we have discovered back to the
  77. // original form
  78.                             if (!errors.empty()) {
  79.                             saveErrors(request, errors);
  80.                             }
  81.                             return (mapping.findForward(target));
  82.                             }
  83.                             }*/
  84.                             if ( isCancelled(request) ) {
  85.                         // Cancel pressed back to employee list
  86.                             return (mapping.findForward(target));
  87.                             }
  88.                             try {
  89.                         // Build the EmployeeForm with the Retrieved values
  90.                             ef=(EmployeeActionForm)buildEmployeeForm(request.getParameter("username"));
  91.                             System.out.println("in getemployeeaction:"+request.getParameter("username")+"mapping:"+mapping.getAttribute()+"scope:"+mapping.getScope());
  92.                             // Add the form to the request or session, bound to the
  93.                             // key named in the <action> attribute name
  94.                             if ("request".equals(mapping.getScope())) {
  95.                               request.setAttribute(mapping.getAttribute(), ef);
  96.                             }
  97.                             else {
  98.                               HttpSession session = request.getSession();
  99.                               session.setAttribute(mapping.getAttribute(),ef);
  100.                               System.out.println(ef.getEmail());
  101.                             }
  102.                             } catch (Exception e) {
  103.                               target = "error";
  104.                               ActionErrors errors = new ActionErrors();
  105.                               errors.add(ActionErrors.GLOBAL_ERROR,
  106.                                          new ActionError("errors.database.error",e.getMessage()));
  107.                               // Report any errors
  108.                             if (!errors.empty()) {
  109.                               saveErrors(request, errors);
  110.                             }
  111.                             }
  112.                             System.out.print("begining forward to jsp !~"+target);
  113.                             // Forward to the appropriate View
  114.                             return (mapping.findForward(target));
  115.   }
  116. }