tagutil.java

来自「It is a java server faces tab component.」· Java 代码 · 共 99 行

JAVA
99
字号
package com.cim.jsf.util;

import javax.faces.application.Application;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.el.MethodBinding;
import javax.faces.el.ValueBinding;
import javax.faces.webapp.UIComponentTag;

public class TagUtil {
	public static void setObject( UIComponent component, String attributeName,
								  String attributeValue ) {
		if ( attributeValue == null ) {
			return;
		}
		if ( UIComponentTag.isValueReference( attributeValue ) ) {
			setValueBinding( component, attributeName, attributeValue );
		} else {
			component.getAttributes().put( attributeName, attributeValue );
		}
	}

	public static void setString( UIComponent component, String attributeName,
								  String attributeValue ) {
		if ( attributeValue == null ) {
			return;
		}
		if ( UIComponentTag.isValueReference( attributeValue ) ) {
			setValueBinding( component, attributeName, attributeValue );
		} else {
			component.getAttributes().put( attributeName, attributeValue );
		}
	}

	public static void setInteger( UIComponent component, String attributeName,
								   String attributeValue ) {
		if ( attributeValue == null ) {
			return;
		}
		if ( UIComponentTag.isValueReference( attributeValue ) ) {
			setValueBinding( component, attributeName, attributeValue );
		} else {
			component.getAttributes().put( attributeName,
										   new Integer( attributeValue ) );
		}
	}

	public static void setBoolean( UIComponent component, String attributeName,
								   String attributeValue ) {
		if ( attributeValue == null ) {
			return;
		}
		if ( UIComponentTag.isValueReference( attributeValue ) ) {
			setValueBinding( component, attributeName, attributeValue );
		} else {
			component.getAttributes().put( attributeName,
										   new Boolean( attributeValue ) );
		}
	}

	public static void setValueBinding( UIComponent component,
										String attributeName,
										String attributeValue ) {
		FacesContext context = FacesContext.getCurrentInstance();
		Application app = context.getApplication();
		ValueBinding vb = app.createValueBinding( attributeValue );
		component.setValueBinding( attributeName, vb );
	}

	public static void setMethodBinding( UIComponent component,
										 String attributeName,
										 String attributeValue,
										 Class[] paramTypes ) {
		if ( attributeValue == null ) {
			return;
		}
		if ( UIComponentTag.isValueReference( attributeValue ) ) {
			FacesContext context = FacesContext.getCurrentInstance();
			Application app = context.getApplication();
			MethodBinding mb = app.createMethodBinding( attributeValue,
					paramTypes );
			component.getAttributes().put( attributeName, mb );
		}
	}

	public static Object evaluateVBExpression( String expression ) {
		if ( expression == null ) {
			return expression;
		}
		if ( !UIComponentTag.isValueReference( expression ) ) {
			return expression;
		}
		FacesContext context = FacesContext.getCurrentInstance();
		Object result = context.getApplication().createValueBinding( expression ).
				getValue( context );
		return result;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?