java 用于带参数的 getter 的 JSTL 或 JSP 2.0 EL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5780504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:44:25  来源:igfitidea点击:

JSTL or JSP 2.0 EL for getter with argument

javajsppropertiesjstl

提问by user82164783

How can I access a getter that has a parameter using JSTL or JSP 2.0 EL?

如何使用 JSTL 或 JSP 2.0 EL 访问具有参数的 getter?

I want to access something like this:

我想访问这样的东西:

public FieldInfo getFieldInfo(String fieldName) {
 ....
}

I could access this in Struts by using mapped propertiesbut don't know if it is possible in JSTL or JSP 2.0.

我可以通过使用映射属性在 Struts 中访问它,但不知道在 JSTL 或 JSP 2.0 中是否可行。

I tried everything but is not working.

我尝试了一切,但没有奏效。

回答by BalusC

Passing method arguments in EL is only by EL spec supported in EL 2.2. EL 2.2 is by default shipped in Servlet 3.0 / JSP 2.2 containers. So if you're using a Servlet 3.0 container (Tomcat 7, Glassfish 3, JBoss 6, etc) and your web.xmlis declared conform Servlet 3.0 spec, then you should be able to access it as follows

在 EL 中传递方法参数仅受 EL 2.2 支持的 EL 规范。EL 2.2 默认在 Servlet 3.0 / JSP 2.2 容器中提供。因此,如果您使用的是 Servlet 3.0 容器(Tomcat 7、Glassfish 3、JBoss 6 等)并且您web.xml声明符合 Servlet 3.0 规范,那么您应该能够按如下方式访问它

${bean.getFieldInfo('fieldName')}

Since you explicitly mentioned JSP 2.0, which is part of the old Servlet 2.4 spec, I assume that there's no room for upgrading. Your best bet is to replace the method by

由于您明确提到 JSP 2.0,它是旧 Servlet 2.4 规范的一部分,我认为没有升级的空间。最好的办法是将方法替换为

public Map<String, FieldInfo> getFieldInfo() {
    // ...
}

so that you can access it as follows

以便您可以按如下方式访问它

${bean.fieldInfo.fieldName}

or

或者

${bean.fieldInfo['fieldName']}

or

或者

${bean.fieldInfo[otherBean.fieldName]}