Package org.strecks.web.tag

Source Code of org.strecks.web.tag.BindTag

/*
* Copyright 2005-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless requiredSuffix by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package org.strecks.web.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* Tag which allows variable in page context to be to any of the scopes (request, session, or application) using a
* supplied expression. Useful, for example, for binding the loop variable of an iteration to an action bean property,
* page bean property or view helper property
*
* @author Phil Zoio
*/
public class BindTag extends TagSupport
{

  Log log = LogFactory.getLog(BindTag.class);

  private static final long serialVersionUID = -4230698900508229618L;

  /**
   * the source page context attribute name
   */
  private String sourceAttribute;

  /**
   * the target attribute name. This is the attribute to which the property will be bound
   */
  private String targetExpression;

  /**
   * the target scope. Can be "request", "session" or "application"
   */
  private String targetScope;

  // ------------------------------------------------------- Public Methods

  public int doStartTag() throws JspException
  {

    Object attribute = pageContext.getAttribute(sourceAttribute);

    if (attribute != null)
    {

      String targetAttribute = null;
      String targetProperty = null;

      int firstDot = targetExpression.indexOf('.');
      if (firstDot == -1)
      {
        targetAttribute = targetExpression;
      }
      else
      {
        targetAttribute = targetExpression.substring(0, firstDot);
        targetProperty = targetExpression.substring(firstDot + 1);
      }

      Object bean = null;

      if (targetScope == null)
        targetScope = "request";

      if ("page".equals(targetScope))
      {
        bean = pageContext.getAttribute(targetAttribute, PageContext.PAGE_SCOPE);
      }
      else if ("request".equals(targetScope))
      {
        bean = pageContext.getAttribute(targetAttribute, PageContext.REQUEST_SCOPE);
      }
      else if ("session".equals(targetScope))
      {
        bean = pageContext.getAttribute(targetAttribute, PageContext.SESSION_SCOPE);
      }
      else if ("application".equals(targetScope))
      {
        bean = pageContext.getAttribute(targetAttribute, PageContext.APPLICATION_SCOPE);
      }
      else
      {
        throw new IllegalStateException("Invalid scope " + targetScope
            + ": must be 'request', 'session' or 'application'");
      }

      if (bean == null)
      {
        log.debug("No bean found for attribute " + targetAttribute + " in scope " + targetScope);
      }
      else
      {

        // attempt to set property using BeanUtils
        try
        {
          PropertyUtils.setProperty(bean, targetProperty, attribute);
        }
        catch (Exception e)
        {
          throw new JspException("Unable to set property " + targetProperty + " in bean "
              + bean.getClass().getName() + " with value " + attribute);
        }

      }

    }
    return (EVAL_BODY_INCLUDE);

  }

  /**
   * Release any acquired resources.
   */
  public void release()
  {
    super.release();
    sourceAttribute = null;
    targetExpression = null;
    targetScope = null;
  }

  /* ****** getters and setters ******* */

  public void setSourceAttribute(String sourceAttribute)
  {
    this.sourceAttribute = sourceAttribute;
  }

  public void setTargetExpression(String targetExpression)
  {
    this.targetExpression = targetExpression;
  }

  public void setTargetScope(String targetScope)
  {
    this.targetScope = targetScope;
  }

}
TOP

Related Classes of org.strecks.web.tag.BindTag

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.