Help

Built with Seam

You can find the full source code for this website in the Seam package in the directory /examples/wiki. It is licensed under the LGPL.

See Creating Custom EL Functions and Creating Custom UI Compositions as a starting point

Given you already have the elfunctions.taglib.xml from the custom EL functions, no need to add any more xml or configuration.

Open and edit FnLibrary.java

import org.el.func.IsTextHandler;
import com.sun.facelets.tag.AbstractTagLibrary;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class FnLibrary extends AbstractTagLibrary {

	public final static String Namespace = "http://org.el.func/SeamFunc";

	public static final FnLibrary INSTANCE = new FnLibrary();

	public FnLibrary() {
		super(Namespace);
		try {
			this.addTagHandler("doesFieldTypeClassHaveAnnotation", DoesFieldTypeClassHaveAnnotation.class);
			Method[] methods = SeamFunc.class.getMethods();
			for (int i = 0; i < methods.length; i++) {
				if (Modifier.isStatic(methods[i].getModifiers())) {
					this.addFunction(methods[i].getName(), methods[i]);
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}

Create DoesFieldTypeClassHaveAnnotation.class

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

import javax.el.ValueExpression;
import javax.faces.component.UIComponent;

import com.sun.facelets.FaceletContext;
import com.sun.facelets.tag.TagAttribute;
import com.sun.facelets.tag.TagConfig;
import com.sun.facelets.tag.TagHandler;

public class DoesFieldTypeClassHaveAnnotation extends TagHandler {
	
	private final TagAttribute annotation;
	
	private final TagAttribute field;


    public DoesFieldTypeClassHaveAnnotation(final TagConfig config) {
        super(config);
        annotation = this.getRequiredAttribute("annotation");
        field = this.getRequiredAttribute("field");
    }

    
    public void apply(final FaceletContext faceletsContext, final UIComponent aParent) throws IOException {
    	boolean apply = false;
    	
    	ValueExpression valueExpression = this.field.getValueExpression(faceletsContext, Object.class);
    	Field f = (Field)valueExpression.getValue(faceletsContext.getFacesContext().getELContext());
    	
		try {
			Annotation[] asOnField = f.getType().getAnnotations();
			for(Annotation t : asOnField) {
				if (annotation.getValue().equals(t.annotationType().getName())) {
					apply = true;
					break;
				}
				
			}
		} catch (SecurityException ex) {
			System.err.println(ex.getMessage());
		}
		
		if (apply) {
            this.nextHandler.apply(faceletsContext, aParent);
        }
	}
}

In your test.xhtml

<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
		xmlns:s="http://jboss.com/products/seam/taglib"
		xmlns:ui="http://java.sun.com/jsf/facelets"
		xmlns:f="http://java.sun.com/jsf/core"
		xmlns:h="http://java.sun.com/jsf/html"
		xmlns:rich="http://richfaces.org/rich"
		xmlns:a="http://richfaces.org/a4j"
		xmlns:e="http://org.el.func/SeamFunc"
		xmlns:custom="http://enhancements.seam/jsf"
		template="/layout/template.xhtml">
		
    <ui:define name="body">
	
	<custom:print message="World" />

	<br />

	<e:doesFieldTypeClassHaveAnnotation field="#{person.dog}" annotation="javax.persistence.Entity">
		The dog field in the Person Entity is also an Entity
	</e:doesFieldTypeClassHaveAnnotation>
		
    </ui:define>
    
</ui:composition>

This would be the case where there is a @Entity public class Dog and

@Entity
public class Person {
...
private Dog dog;
...
}