Package org.beangle.struts2.action

Source Code of org.beangle.struts2.action.DispatchAction

/* Copyright c 2005-2012.
* Licensed under GNU  LESSER General Public License, Version 3.
* http://www.gnu.org/licenses
*/
package org.beangle.struts2.action;

import java.util.Collection;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.beangle.struts2.convention.Flash;
import org.beangle.struts2.convention.route.Action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class DispatchAction extends ActionSupport {

  public String execute() throws Exception {
    return forward(new Action(this, "index"));
  }

  protected String forward() {
    return SUCCESS;
  }

  protected String forward(String view) {
    return view;
  }

  protected String forward(String view, String message) {
    addActionMessage(getText(message));
    return view;
  }

  /**
   * @param action
   * @return
   */
  protected String forward(Action action) {
    ActionContext.getContext().getContextMap().put("dispatch_action", action);
    return "chain:dispatch_action";
  }

  protected String forward(Action action, String message) {
    addActionMessage(getText(message));
    return forward(action);
  }

  /**
   * @param method
   * @param message
   * @param params
   * @return
   */
  protected String redirect(String method, String message, String params) {
    return redirect(new Action((String) null, method, params), message);
  }

  /**
   * @param method
   * @param message
   * @return
   */
  protected String redirect(String method, String message) {
    return redirect(new Action(method), message);
  }

  protected String redirect(Action action, String message) {
    if (StringUtils.isNotEmpty(message)) {
      addFlashMessage(message);
    }
    ActionContext.getContext().getContextMap().put("dispatch_action", action);
    return "redirectAction:dispatch_action";
  }

  protected void addMessage(String msgKey) {
    addActionMessage(getText(msgKey));
  }

  protected void addError(String msgKey) {
    addActionError(getText(msgKey));
  }

  protected void addFlashMessage(String msgKey) {
    getFlash().addMessage(getText(msgKey));
  }

  protected Flash getFlash() {
    Flash flash = (Flash) ActionContext.getContext().getSession().get("flash");
    if (null == flash) {
      flash = new Flash();
      ActionContext.getContext().getSession().put("flash", flash);
    }
    return flash;
  }

  /**
   * 将flash中的消息转移到actionmessage<br>
   * 不要将flash和message混合使用。
   */
  public Collection<String> getActionMessages() {
    Flash flash = getFlash();
    @SuppressWarnings("unchecked")
    List<String> messages = (List<String>) flash.get(Flash.MESSAGES);
    if (null != messages) {
      for (String msg : messages) {
        addActionMessage(msg);
      }
      messages.clear();
    }
    return super.getActionMessages();
  }

  /**
   * 将flash中的错误转移到actionerror<br>
   * 不要将flash和error混合使用。
   */
  public Collection<String> getActionErrors() {
    Flash flash = getFlash();
    @SuppressWarnings("unchecked")
    List<String> errors = (List<String>) flash.get(Flash.ERRORS);
    if (null != errors) {
      for (String msg : errors) {
        addActionError(msg);
      }
      errors.clear();
    }
    return super.getActionErrors();
  }

}
TOP

Related Classes of org.beangle.struts2.action.DispatchAction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.