Result实现类

时间:2014-04-29 18:55:17   收藏:0   阅读:415
mamicode.com,码迷
package org.apache.struts2.dispatcher;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Result;
import com.opensymphony.xwork2.util.TextParseUtil;
import com.opensymphony.xwork2.util.TextParseUtil.ParsedValueEvaluator;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.apache.struts2.StrutsStatics;

public abstract class StrutsResultSupport
  implements Result, StrutsStatics
{
  private static final Logger LOG = LoggerFactory.getLogger(StrutsResultSupport.class);
  public static final String DEFAULT_PARAM = "location";
  private boolean parse;
  private boolean encode;
  private String location;
  private String lastFinalLocation;

  public StrutsResultSupport()
  {
    this(null, true, false);
  }

  public StrutsResultSupport(String location) {
    this(location, true, false);
  }

  public StrutsResultSupport(String location, boolean parse, boolean encode) {
    this.location = location;
    this.parse = parse;
    this.encode = encode;
  }

  public void setLocation(String location)
  {
    this.location = location;
  }

  public String getLocation()
  {
    return this.location;
  }

  public String getLastFinalLocation()
  {
    return this.lastFinalLocation;
  }

  public void setParse(boolean parse)
  {
    this.parse = parse;
  }

  public void setEncode(boolean encode)
  {
    this.encode = encode;
  }

  public void execute(ActionInvocation invocation)
    throws Exception
  {
    this.lastFinalLocation = conditionalParse(this.location, invocation);
    doExecute(this.lastFinalLocation, invocation);
  }

  protected String conditionalParse(String param, ActionInvocation invocation)
  {
    if ((this.parse) && (param != null) && (invocation != null)) {
      return TextParseUtil.translateVariables(param, invocation.getStack(), new TextParseUtil.ParsedValueEvaluator()
      {
        public Object evaluate(String parsedValue) {
          if ((StrutsResultSupport.this.encode) && 
            (parsedValue != null))
          {
            try
            {
              return URLEncoder.encode(parsedValue, "UTF-8");
            }
            catch (UnsupportedEncodingException e) {
              if (StrutsResultSupport.LOG.isWarnEnabled()) {
                StrutsResultSupport.LOG.warn("error while trying to encode [" + parsedValue + "]", e, new String[0]);
              }
            }
          }

          return parsedValue;
        }
      });
    }
    return param;
  }

  protected abstract void doExecute(String paramString, ActionInvocation paramActionInvocation)
    throws Exception;
}
mamicode.com,码迷

**********************************************************

 

Result实现类,码迷,mamicode.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!