如何访问 EL 变量并将其作为参数传递给 EL 中的函数?

发布于 2024-08-16 01:06:13 字数 330 浏览 6 评论 0原文

我想调用 bean 上的函数,并使用 EL 在 jsp 中传递给它的参数。 问题是它不允许这样的事情: “${teacherBean.certificationFor(${pspecialField})

的问题是我想迭代一个数组并为作为参数传递的数组中的所有值调用函数certificationFor。 我通过以下方式获取数组中的值:

所以基本上我想做一些类似的事情: ${teacherBean.certificationFor(${pspecialField}) 但我不能那样做。 我可以用其他方式做到这一点吗?

我是 EL 的新手。 :) 任何帮助表示赞赏。

I want to call a function on a bean with an argument passsed to it in jsp using EL.
The problem is it does not allow something like:
"${teacherBean.certificationFor(${particularField})"

the thing is i want to iterate over an array and call the function certificationFor for all the values in the array passed as an argument.
I am getting the values in array by:

So Basically i want to do something like:

${teacherBean.certificationFor(${particularField})

but i cant do that.
can i do this in any other way?

I am a newbie in EL . :)
Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空袭的梦i 2024-08-23 01:06:13

您到底想在哪里这样做以及为了什么?只是为了获取显示值?至少,在 Java EE 6 之前的标准 EL 中,您不能像这样传递方法参数。在 JBoss-EL 或 < a href="http://java.sun.com/javaee/6/docs/tutorial/doc/gjddd.html" rel="noreferrer">Java EE 6 EL 你可以做到这一点。语法将是:

${teacherBean.certificationFor(particularField)}

请注意,您不能嵌套 EL 表达式,EL 表达式本身已经是一个完整的表达式。

然而,在标准 EL 实现中,您可以使用大括号表示法通过键访问 Map 值。因此,如果您有一个 MapCertifications 其中键对应于 preferredField 且值对应于关联值:

private Map<String, String> certifications = new HashMap<String, String>();

public Map<String, String> getCertificationFor() {
    return this.certifications;
}

那么您可以使用以下表示法:

${teacherBean.certificationFor[particularField]}

这在幕后解析为

teacherBean.getCertificationFor().get(particularField)

Where exactly do you want to do that and for what? Just to get a value for display? At least, in standard EL prior to Java EE 6 you cannot pass method arguments like that. In JBoss-EL or in Java EE 6 EL you can do that. The syntax would then just have been:

${teacherBean.certificationFor(particularField)}

Note that you cannot nest EL expressions, an EL expression is already a whole expression at its own.

In standard EL implementations you can however access Map values by keys using the brace notation. Thus, if you for example have a Map<String, String> certifications where the keys corresponds the particularField and the values the associated value:

private Map<String, String> certifications = new HashMap<String, String>();

public Map<String, String> getCertificationFor() {
    return this.certifications;
}

then you can use the following notation:

${teacherBean.certificationFor[particularField]}

this resolves behind the scenes to

teacherBean.getCertificationFor().get(particularField)
从来不烧饼 2024-08-23 01:06:13

我认为在标准 EL 中,除了定义封装在 EL 函数中的函数之外,您没有任何选择;

阅读: http://java.sun.com/j2ee/ 1.4/docs/tutorial/doc/JSPIntro7.html 靠近底部
文档;

但正如 BalusC 已经提到的,如果您有能力将这种依赖项添加到您的应用程序中,您是否可以使用其他 EL 实现

I think in the standard EL you don't have any options other than defining your functions wrapped in a EL function;

Read: http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPIntro7.html near the bottom of the
document;

but as BalusC mentioned already if you could use another EL implmentation if you have the ability to add that kind of dependency to your app

柒夜笙歌凉 2024-08-23 01:06:13

怎么样:

${teacherBean.certificationFor(particularField)}

What about:

${teacherBean.certificationFor(particularField)}
呆橘 2024-08-23 01:06:13

如果您要访问的通用功能更好地表达为单独的函数,则可以按如下方式编写:

${certificationFor[teacherBean][particularField]}

其中 CertificationFor 映射到扩展 ELMethod.java 类。您可以在 result(Object[] args) 方法中实现该功能。此方法的参数是您传递给 EL 中的 ${certificationFor} 对象的参数。

public class CertificationFor extends ELMethod {
  public Object result(Object[] args) {  
    TeacherBean teacherBean = (TeacherBean) args[0];  
    String property = (String) args[1];

    // your implementation goes here
    return ....;
  }
}  

技巧是使用你的对象作为映射的链式映射,这是将多个参数传递给 EL 函数的一种方法。

如果您有兴趣,可以在此处查看完整代码和代码片段:
http://www.vineetmanohar.com /2010/07/如何在 el-methods 中传递参数/

If you are accessing a general functionality that is better expressed as a separate function, then you can write it as follows:

${certificationFor[teacherBean][particularField]}

where certificationFor maps to the CertificationFor class which extends ELMethod.java class. You implement the functionality in the result(Object[] args) method. The args to this method are the args that you passed to the ${certificationFor} object in EL.

public class CertificationFor extends ELMethod {
  public Object result(Object[] args) {  
    TeacherBean teacherBean = (TeacherBean) args[0];  
    String property = (String) args[1];

    // your implementation goes here
    return ....;
  }
}  

The trick is to use your object as a chained map of maps, that is one way to pass multiple args to EL function.

If you are interested, you can see full code and code snippets here:
http://www.vineetmanohar.com/2010/07/how-to-pass-parameters-in-el-methods/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文