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