Getter Setter的JSF EL字段引用如何工作?

时间:2012-11-09 05:44:25

标签: java oop

我想为我自己的项目复制这个,但不是为xhtml或其他xml结构化文件复制。我想在java文件中复制这种功能。有可能吗?

#{Object.field}

示例类

class Object
{
   public String getField()
   {
      return "field";
   }

   public void setField()
   {
   }
}

它以某种方式将getter和setter的后缀转换为实际的字段引用。但我希望开发人员只能放置实际字段。因此,当它们没有放置实际字段的名称(这是getter和setter函数的后缀)时,它会在编译时显示错误。

我真的不想使用EL表达式。 “#{} Object.field”。我只想复制系统,以便开发人员不必使用循环,getter和setter来创建表。如果可能,我希望他们能够像这样编码

createTable(new Field[]{Object.field1, Object.field4, Object.field6}, new Object[]{object1, object2, object3});
//new Field[]{Object.field1, Object.field4} <-- selected fields
//new Object[]{object1, object2, object3} <-- objects that is going to be shown in the table

我发现使用java反射可以做到这一点。但要获得一个特定的“领域”会有很多麻烦。如果我能像“Object.fieldName”一样轻松获得“Field”,那就太棒了。

2 个答案:

答案 0 :(得分:0)

我认为你需要的是EL Resolver。你需要从javax&amp;中实现ELResolver。只要你有任何EL字符串,只需使用你自己的EL解析器解决它。

以下是示例代码。它不会工作,所以尝试删除不必要的东西,如函数映射器,变量映射器等&amp;然后运行它。

Maven dependancies

    <dependency>
        <groupId>javax.el</groupId>
        <artifactId>el-api</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>jasper-el</artifactId>
        <version>6.0.18</version>
    </dependency>

自定义EL解析器

package ravi.tutorial.el;

import java.beans.FeatureDescriptor;
import java.util.Iterator;
import java.util.Map;

import javax.el.ELContext;
import javax.el.ELResolver;
import javax.el.MapELResolver;


public class DemoELResolver extends ELResolver {

    private ELResolver delegate = new MapELResolver();
    private Map<Object, Object> userMap;

    public DemoELResolver(Map<Object, Object> userMap) {
        this.userMap = userMap;
    }

    @Override
    public Object getValue(ELContext context, Object base, Object property) {
        if (base == null) {
            base = userMap;
        }
        return delegate.getValue(context, base, property);
    }

    @Override
    public Class<?> getCommonPropertyType(ELContext context, Object base) {
        if (base == null) {
            base = userMap;
        }
        return delegate.getCommonPropertyType(context, base);
    }

    @Override
    public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context,
            Object base) {
        if (base == null) {
            base = userMap;
        }
        return delegate.getFeatureDescriptors(context, base);
    }

    @Override
    public Class<?> getType(ELContext context, Object base, Object property) {
        if (base == null) {
            base = userMap;
        }
        return delegate.getType(context, base, property);
    }

    @Override
    public boolean isReadOnly(ELContext context, Object base, Object property) {
        if (base == null) {
            base = userMap;
        }
        return delegate.isReadOnly(context, base, property);
    }

    @Override
    public void setValue(ELContext context, Object base, Object property,
            Object value) {
        if (base == null) {
            base = userMap;
        }
        delegate.setValue(context, base, property, value);
    }

}

使用ELResolver

    ExpressionFactory expressionFactory;

    if (args == null || args.length == 0) {
        System.out
                .println("Enter command line argument  1=Apache Jasper   2=Sourceforge JUEL.");
        return;
    }

    if (args[0].equals("1")) {
        expressionFactory = new org.apache.el.ExpressionFactoryImpl();
        System.out.println("Choosing org.apache.el.ExpressionFactoryImpl");
    } else if (args[0].equals("2")) {
        expressionFactory = new de.odysseus.el.ExpressionFactoryImpl();
        System.out.println("Choosing de.odysseus.el.ExpressionFactoryImpl");
    } else {
        System.out.println("Wrong argument");
        return;
    }

    // create a map with some variables in it
    Map<Object, Object> userMap = new HashMap<Object, Object>();
    userMap.put("x", new Integer(123));
    userMap.put("y", new Integer(456));

    // get the method for ${myprefix:hello(string)}
    Method sayHello = DemoEL.class.getMethod("sayHello",
            new Class[] { String.class });

    // create the context
    ELResolver demoELResolver = new DemoELResolver(userMap);
    final VariableMapper variableMapper = new DemoVariableMapper();
    final DemoFunctionMapper functionMapper = new DemoFunctionMapper();
    functionMapper.addFunction("myprefix", "hello", sayHello);
    final CompositeELResolver compositeELResolver = new CompositeELResolver();
    compositeELResolver.add(demoELResolver);
    compositeELResolver.add(new ArrayELResolver());
    compositeELResolver.add(new ListELResolver());
    compositeELResolver.add(new BeanELResolver());
    compositeELResolver.add(new MapELResolver());
    ELContext context = new ELContext() {
        @Override
        public ELResolver getELResolver() {
            return compositeELResolver;
        }

        @Override
        public FunctionMapper getFunctionMapper() {
            return functionMapper;
        }

        @Override
        public VariableMapper getVariableMapper() {
            return variableMapper;
        }
    };

    // create and resolve a value expression
    String sumExpr = "${x+y}";
    ValueExpression ve = expressionFactory.createValueExpression(context,
            sumExpr, Object.class);
    Object result = ve.getValue(context);
    System.out.println("Result=" + result);

    // call a function
    String fnExpr = "#{myprefix:hello('Dave')}";
    ValueExpression fn = expressionFactory.createValueExpression(context,
            fnExpr, Object.class);
    fn.getValue(context);

参考:http://weblogs.java.net/blog/felipeal/archive/2008/10/adding_el_suppo.html

答案 1 :(得分:0)

这些表达式是字符串,只能在运行时进行计算。字段名称以get / set为前缀以形成方法名称,然后使用Reflection API在对象上调用该方法名称。

如果有可能在编译时执行类似的东西那么他们肯定会这样做,我想。

Object.field1这是无效的,除非您在该类中实际拥有类似的公共字段。它必须是字符串"Object.field1"。而且你无法在编译时真正检查该字符串的内容。

因此,在编译时不可能强制执行此操作。你能做的最好的就是抛出一个运行时异常。

我认为您不需要构建像EL-API那样复杂的系统。只需学习Reflection API