9

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

I want to access something like this:

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

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.

user82164783
  • 93
  • 1
  • 1
  • 3

1 Answers1

23

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')}

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}

or

${bean.fieldInfo['fieldName']}

or

${bean.fieldInfo[otherBean.fieldName]}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I'm on a servlet 2.4 and can't change that. I can't modify the bean with the `getFieldInfo` method and was trying to avoid wrapping it in a Map or using custom tags/functions to access it. Is there no way to access it with a parameter? – user82164783 Apr 25 '11 at 16:40
  • That's the drawback of sticking to old technologies. If there was another way, I would have mentioned that in the answer. You can however supply a custom implementation of the `Map` where you overridde the `get()` method with some kind of lazy loading so that you don't need to prepopulate the entire map beforehand. – BalusC Apr 25 '11 at 16:40