我有一个托管bean(会话范围),如下所示:
class Home {// as homeBean
public void doSomething(ActionEvent ae, int a, int b){
System.out.println("result="+(a+b));
}
}
我想称之为
<a4j:commandLink actionListener="#{homeBean:doSomething(1,2)}"/>
我所知道的是:不可能使用a和b参数。
ok:这应该是一个“静态”的可能性,使用taglib来调用它:
public class CoolTaglib implements TagLibrary{
...
public static void doSomething(int a, int b) {
getHomeBeanFromSession().doSomething(a,b);
}
}
动态调用它怎么样?使用bcel还是URLClassLoader?
答案 0 :(得分:1)
此EL表达式语法仅适用于静态方法,必须在标记库中定义,并在视图中定义名称空间:
#{namespacePrefix:fn(arg)}
此EL表达式在对象实例上调用parameterized method:
#{someInstance.method(arg)}
第二种形式在Expression Language 2.2或更高版本(Java EE 6)中可用。在此之前,某些第三方JSF库支持类似的表达式。
只要在JSF上下文中执行,就可以从静态方法中查找托管bean:
FacesContext context = FacesContext.getCurrentInstance();
SomeBean someBean = context.getApplication()
.evaluateExpressionGet(context,
"#{someBean}", SomeBean.class);
然而,这不是理想的方法。这段代码是针对JSF 2编写的;以前的版本使用不同的动态查找调用。
如果您需要静态方法中的bean,请使用以下形式的表达式:
#{namespacePrefix:fn(someBean, 1, 2)}
答案 1 :(得分:0)
哦,很酷,我找到了一种工作方式:
public class ... implements TagLibrary {
@Override
public Method createFunction(String taglib, String functionName) {
if (!map.containsKey(functionName)) {
String classname = "de.Test" + functionName;
ClassGen _cg = new ClassGen(classname,
"java.lang.Object", "Test.java", ACC_PUBLIC | ACC_SUPER,
new String[] {});
ConstantPoolGen _cp = _cg.getConstantPool();
InstructionFactory _factory = new InstructionFactory(_cg, _cp);
Method meth = find(functionName, getNavigation());
Class<?>[] parameterTypes = meth.getParameterTypes();
int countParams = parameterTypes.length;
Type[] types = new Type[countParams];
String[] names = new String[countParams];
for (int i = 0; i < countParams; i++) {
types[i] = new ObjectType(parameterTypes[i].getName());
names[i] = "arg" + i;
}
InstructionList il = new InstructionList();
MethodGen staticMethod = new MethodGen(ACC_PUBLIC | ACC_STATIC,
Type.OBJECT, types, names, functionName, getClass()
.getName(), il, _cp);
InstructionHandle ih_1 = il.append(new PUSH(_cp, functionName));
il.append(new PUSH(_cp, countParams));
il.append(_factory.createNewArray(Type.OBJECT, (short) 1));
il.append(InstructionConstants.DUP);
for (int i = 0; i < countParams; i++) {
il.append(new PUSH(_cp, i));
il.append(_factory.createLoad(Type.OBJECT, i));
il.append(InstructionConstants.AASTORE);
if (i != countParams - 1)
il.append(InstructionConstants.DUP);
}
il.append(_factory.createInvoke(getClass().getName(),
"call", Type.OBJECT, new Type[] { Type.STRING,
new ArrayType(Type.OBJECT, 1) },
Constants.INVOKESTATIC));
InstructionHandle ih_25 = il.append(_factory
.createReturn(Type.OBJECT));
staticMethod.setMaxStack();
staticMethod.setMaxLocals();
_cg.addMethod(staticMethod.getMethod());
il.dispose();
try {
byte[] bytes = _cg.getJavaClass().getBytes();
InjectingClassLoader icl = new InjectingClassLoader();
Method find =
find(functionName, icl.load(classname, bytes));
map.put(functionName, find);
} catch (Exception e) {
e.printStackTrace();
}
}
Method method = map.get(functionName);
return method;
}
public static Object call(String functionname, Object[] arguments) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Navigation myTargetBean = getNavigation();
Method proxyMethod = find(functionname,myTargetBean);
Object result = proxyMethod.invoke(myTargetBean, arguments);
return result;
}
现在,我可以拨打#{cms:doSomething(1,2)}