带有参数的getter的JSTL或JSP 2.0 EL [英] JSTL or JSP 2.0 EL for getter with argument

查看:111
本文介绍了带有参数的getter的JSTL或JSP 2.0 EL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我想访问以下内容:

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

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

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

我尝试了一切,但没有工作。

I tried everything but is not working.

推荐答案

传递方法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规范,那么您应该能够按如下方式访问它

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.xml is declared conform Servlet 3.0 spec, then you should be able to access it as follows

${bean.getFieldInfo('fieldName')}

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

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

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

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

so that you can access it as follows

${bean.fieldInfo.fieldName}

${bean.fieldInfo['fieldName']}

${bean.fieldInfo[otherBean.fieldName]}

这篇关于带有参数的getter的JSTL或JSP 2.0 EL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆