How to do this using java expression language

Let's say I have the following java object that I want to read in my jsp using el:

class A {

 Map map = new HashMap();
 int count;

        String getAttribute(String attrName)
        {
                return map.get(attrName);
        }

       String getCount()
       { 
               return count;
      }

}

I can call up the account by doing ${a.count}

But how can I call getAttribute using el?

+3
source share
4 answers

Java EE 6 (for example, Glassfish 3 will accept $ {a.getAttribute ('foo')}) supports method calls, Java EE 5 does not. Here you will need to open your card as follows:

public Map getAttributes() {
  return map;
}

to make the expression $ {a.attributes ['foo']}) work.

If you are outside of Java EE / JSP, you can use an EE 6 compatible implementation, such as JUEL 2.2.x, which supports method calls.

+4

!

, , .

, el , , .

- . getAttribute , , . , .

+3

, java.util.Map, . EL:

${a["attribute"]}
+2

El 2.x , :

${a["attribute"]}
0
source

Source: https://habr.com/ru/post/1716760/


All Articles