getemployeeaction.java

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

JAVA
118
字号
package strutsds;
import java.io.IOException;
import javax.servlet.ServletException;
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;

public class GetEmployeeAction extends Action {
  protected ActionForm buildEmployeeForm(String username)throws Exception {
    String user = null;
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    EmployeeActionForm form = null;
    DataSource dataSource = (DataSource)servlet.getServletContext().getAttribute(Action.DATA_SOURCE_KEY);
    try {
      conn = dataSource.getConnection();
      stmt = conn.createStatement();
      rs =stmt.executeQuery("select * from employees "
                            + "where username='"
                            + username + "'");
                            if ( rs.next() ) {
                            form = new EmployeeActionForm();
                            form.setUsername(rs.getString("username"));
                            form.setPassword(rs.getString("password"));
                            form.setDepid(rs.getString("depid"));
                            form.setRoleid(rs.getString("roleid"));
                            form.setName(rs.getString("name"));
                            form.setPhone(rs.getString("phone"));
                            form.setEmail(rs.getString("email"));
                            }
                            else {
                            throw new Exception("Employee " + username
                            + " not found!");
                            }
                            }
                            finally {
                            if (rs != null) {
                            rs.close();
                            }
                            if (stmt != null) {
                            stmt.close();
                            }
                            if (conn != null) {
                            conn.close();
                            }
                            }
                            return form;
  }
  public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse httpServletResponse) {
    /*@todo: complete the business logic here, this is just a skeleton. */
    // Default target to success
    EmployeeActionForm ef=null;
                            String target = "success";
                            /*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 = "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);
                            }
                            return (mapping.findForward(target));
                            }
                            }*/
                            if ( isCancelled(request) ) {
                        // Cancel pressed back to employee list
                            return (mapping.findForward(target));
                            }
                            try {
                        // Build the EmployeeForm with the Retrieved values
                            ef=(EmployeeActionForm)buildEmployeeForm(request.getParameter("username"));
                            System.out.println("in getemployeeaction:"+request.getParameter("username")+"mapping:"+mapping.getAttribute()+"scope:"+mapping.getScope());
                            // Add the form to the request or session, bound to the
                            // key named in the <action> attribute name
                            if ("request".equals(mapping.getScope())) {
                              request.setAttribute(mapping.getAttribute(), ef);
                            }
                            else {
                              HttpSession session = request.getSession();
                              session.setAttribute(mapping.getAttribute(),ef);
                              System.out.println(ef.getEmail());
                            }
                            } catch (Exception e) {
                              target = "error";
                              ActionErrors errors = new ActionErrors();
                              errors.add(ActionErrors.GLOBAL_ERROR,
                                         new ActionError("errors.database.error",e.getMessage()));
                              // Report any errors
                            if (!errors.empty()) {
                              saveErrors(request, errors);
                            }
                            }
                            System.out.print("begining forward to jsp !~"+target);
                            // Forward to the appropriate View
                            return (mapping.findForward(target));

  }
}

⌨️ 快捷键说明

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