用于带参数的 getter 的 JSTL 或 JSP 2.0 EL [英] JSTL or JSP 2.0 EL for getter with argument
问题描述
如何使用 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) {
....
}
我可以使用 映射属性 在 Struts 中访问它但不知道在 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屋!