我认为您需要的是 EL Resolver。您需要从 javax 实现 ELResolver,只要您有任何 EL 字符串,只需使用您自己的 EL Resolver 解析它。
下面是示例代码。它不会按原样工作,所以尝试删除不必要的东西,如函数映射器、变量映射器等,然后运行它。
Maven 依赖
<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