简体   繁体   English

Mojarra JSF 2.1升级到JDK7后无法调用正确的方法

[英]Mojarra JSF 2.1 fails to call correct method after upgrade to JDK7

We're looking into switching our fairly big webapp (using Primefaces, Mojarra 2.1, Jetty) from Sun JDK6 + Jetty 6 to Oracle JDK7 + Jetty 9. Mostly this was painless, but it seems the method access is different on Java 7. 我们正在考虑将相当大的Web应用程序(使用Primefaces,Mojarra 2.1,Jetty)从Sun JDK6 + Jetty 6切换到Oracle JDK7 + Jetty9。这通常很容易,但是在Java 7上,方法访问似乎有所不同。

When having a class with the two methods 当使用这两种方法时

public Object getValue(int _index) {...}

and

public Object getValue(String _name) {...}

and using 和使用

#{myBean.getValue(index)}

would call the correct method (with int parameter) and return the correct value under Java 6. In Java 7, however, it would silently fail and not return/print out anything. 将在Java 6下调用正确的方法(带有int参数)并返回正确的值。但是,在Java 7中,它将无提示地失败并且不会返回/打印出任何内容。 When I change the method name and use the appropriate name in the JSF file, the access works as expected. 当我更改方法名称并在JSF文件中使用适当的名称时,访问将按预期进行。

Why am I seeing this only on Java 7 and is there a way to restore the old behaviour (which, basically, worked like it does in Java code - the correct method is used based on the class(es) of the parameters)? 为什么我只能在Java 7上看到这种情况,并且有一种方法可以还原旧的行为(基本上,它的行为与Java代码中的行为相同-正确的方法基于参数的类)?

This is not specific to JSF/Mojarra, but to EL and reflection API . 这并非特定于JSF / Mojarra,而是特定于ELReflection API You'd have had exactly the same problem when using MyFaces, for example. 例如,在使用MyFaces时,您将遇到完全相同的问题。

You were facing a bug in the EL implementation being used by Jetty. 您正面临Jetty使用的EL实施中的错误。 It does apparently not take method parameter types into account while finding a method (exactly like as how EL functions work). 显然,在找到方法时并没有考虑方法参数类型(就像EL函数的工作原理一样)。 Buggy EL implementations will just loop over Class#getDeclaredMethods() and pick the first one matching the name and ignore the parameter types. Buggy EL实现将仅遍历Class#getDeclaredMethods()并选择与名称匹配的第一个,并忽略参数类型。 As you can read in Class#getDeclaredMethods() javadoc , 正如您可以在Class#getDeclaredMethods() javadoc中阅读的那样,

... The elements in the array returned are not sorted and are not in any particular order. ...返回的数组中的元素未排序,并且没有任何特定顺序。 ... ...

the order is undefinied. 订单不确定。 Actually, under the covers, it's dependent on the JVM make/version. 实际上,实际上,它取决于JVM的版本。 That at least explains the difference when switching the JVM. 这至少可以解释切换JVM时的区别。

I recommend to upgrade the EL implementation being used or to rename your method. 我建议升级正在使用的EL实现或重命名您的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM