All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ajax4jsf.taglib.ajax.KeepAliveTag Maven / Gradle / Ivy

Go to download

Ajax4jsf is an open source extension to the JavaServer Faces standard that adds AJAX capability to JSF applications without requiring the writing of any JavaScript.

The newest version!
/**
 * Licensed under the Common Development and Distribution License,
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.sun.com/cddl/
 *   
 * Unless required 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.ajax4jsf.taglib.ajax;

import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
import javax.faces.webapp.UIComponentTag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

import org.ajax4jsf.framework.renderer.AjaxPhaseListener;
import org.ajax4jsf.framework.util.message.Messages;

/**
 * @author shura
 *
 */
public class KeepAliveTag extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4322021112358067548L;
	
	private String beanName = null;
	
	private String ajaxOnly = null;

	/**
	 * @return the ajaxOnly
	 */
	public String getAjaxOnly() {
		return ajaxOnly;
	}

	/**
	 * @param ajaxOnly the ajaxOnly to set
	 */
	public void setAjaxOnly(String ajaxOnly) {
		this.ajaxOnly = ajaxOnly;
	}

	/**
	 * @return the name
	 */
	public String getBeanName() {
		return beanName;
	}

	/**
	 * @param name the name to set
	 */
	public void setBeanName(String name) {
		this.beanName = name;
	}

	/* (non-Javadoc)
	 * @see javax.servlet.jsp.tagext.TagSupport#doStartTag()
	 */
	public int doStartTag() throws JspException {
        if (beanName == null) {
            throw new JspException(Messages.getMessage(Messages.NULL_TYPE_ATTRIBUTE_ERROR));
        }
        if(UIComponentTag.isValueReference(beanName)){
            throw new JspException(Messages.getMessage(Messages.NAME_MUST_BE_LITERAL));
        }
		boolean isAjaxOnly = false;
		FacesContext facesContext = FacesContext.getCurrentInstance();
		if (null != ajaxOnly) {
            if (UIComponentTag.isValueReference(ajaxOnly))
            {
                ValueBinding vb = facesContext.getApplication().createValueBinding(ajaxOnly);
                isAjaxOnly = Boolean.TRUE.equals(vb.getValue(facesContext));
            }
            else
            {
                //TODO: More sophisticated way to convert boolean value (yes/no, 1/0, on/off, etc.)
                isAjaxOnly = Boolean.parseBoolean(ajaxOnly);
            }

		}
        // Get bean instance from EL expression.
        String beanExpression = "#{"+beanName+"}";
        ValueBinding valueBinding = facesContext.getApplication().createValueBinding(beanExpression);
        Object bean = valueBinding.getValue(facesContext);
        // Put bean instance to ViewRoot. 
        String beanAttributeName = isAjaxOnly?AjaxPhaseListener.AJAX_BEAN_PREFIX:AjaxPhaseListener.VIEW_BEAN_PREFIX+beanName;
        facesContext.getViewRoot().getAttributes().put(beanAttributeName, bean);
		return Tag.SKIP_BODY;
	}

	/* (non-Javadoc)
	 * @see javax.servlet.jsp.tagext.TagSupport#release()
	 */
	public void release() {
		beanName = null;
		ajaxOnly = null;
		super.release();
	}
	

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy