0

此类将用于实例化 BaseAction 工厂并将操作传递给控制器

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

public class Controller extends HttpServlet {
    private static final long serialVersionUID = 1L;

  private static final Logger log = Logger.getLogger(Controller.class);

  public void init() {

 }

  public void doGet(HttpServletRequest _req, HttpServletResponse _res)
    throws ServletException, IOException
  {

/* 转发到 doPost 方法 */

    doPost(_req, _res);
  }

  public void doPost(HttpServletRequest _req, HttpServletResponse _res)
    throws ServletException, IOException
  {

    String view = null;
    boolean redirect = false ;

    try {

/* 用助手包装请求对象 */

      ReqUtility reqUtil = new ReqUtility(_req);

/* 根据请求参数创建 BaseAction 对象 */ BaseAction action = reqUtil.getAction(); log.debug("控制器 - getAction " + _req.getQueryString() + " " + _req.getPathInfo());

/* 执行业务逻辑 */

      boolean retval = action.execute(_req, _res) ;

/如果这是消费者操作,我们已经重定向到 eDirect,所以只需返回/

      String className = action.getClass().getName();
      boolean consumerWizardAction = (className.contains("ApplWizardAction"));
      log.debug("Controller. className = ["+className+"], consumerWizardAction ["+consumerWizardAction+"]");
      if (!retval && consumerWizardAction) {
          log.debug("returning after redirect");
          redirect = true;
          return ;
      }

/* 获取适当的操作视图 */

      view = action.getView();

      if(null == view) throw new ServletException("No view specified in action: "+action.getClass().getName());


    if (!redirect) {
          /* Forward the request to the given view */
          RequestDispatcher dispatcher = _req.getRequestDispatcher(view);
          if( _req.getParameter("includeView") != null) {
            _res.setContentType("text/html");
            dispatcher.include(_req, _res);
          } else {
            dispatcher.forward(_req, _res);
          }

    }



    }catch (Exception e) {
      /* Forward the error to the error.jsp page */
      log.debug("Controller - exception " + e.toString());

      _req.setAttribute("javax.servlet.jsp.jspException", e);
      view = "/error.jsp";
      e.printStackTrace();
    }

  }

  public void destroy()
  {
  }

}
4

0 回答 0