Note that there are some explanatory texts on larger screens.

plurals
  1. POOGNL expression in Core Java - Access a Map with dynamic keys
    primarykey
    data
    text
    <p>I am new to OGNL. I am trying to write OGNL expressions in core java to retrieve properties/call methods.</p> <p>Here is my test class.It has a List of keys and a Map which uses these keys to store values. Please see below</p> <pre><code>package ognl.test; import java.util.List; import java.util.Map; public class OGNLTestProg { List&lt;String&gt; keys = null; Map&lt;String, Object&gt; data = null; public OGNLTestProg(List&lt;String&gt; keys, Map&lt;String, Object&gt; data){ this.keys = keys; this.data = data; } public List&lt;String&gt; getKeys(){ return keys; } public Object getData(String key){ return data.get(key); } } </code></pre> <p>Here is the test program that instantiates an object of the above class and uses OGNL to call some methods.</p> <pre><code>package ognl.test; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import ognl.Ognl; public class Test { public static void main(String[] args) throws Exception { String str1 = "1"; String str2 = "2"; List&lt;String&gt; keys = new ArrayList&lt;String&gt;(); keys.add(str1); keys.add(str2); User u1 = new User(); u1.setFirstName("x"); u1.setLastName("y"); User u2 = new User(); u2.setFirstName("x1"); u2.setLastName("y1"); Map&lt;String, Object&gt; data = new HashMap&lt;String, Object&gt;(); data.put(str1, u1); data.put(str2, u2); OGNLTestProg o = new OGNLTestProg(keys, data); /** #{ #outerObj = #this, 'users': getKeys(). { #{ #user = outerObj.getData(#this), #{ 'firstName': user.getFirstName(), 'lastName':user.getLastName() } } } } */ Object expression1 = Ognl.parseExpression("#{"+ "#outerObj = #this,"+ "'users': getKeys()."+ "{"+ "#{"+ "#user = outerObj.getData(#this),"+ "#{ 'firstName': user.getFirstName(),"+ "'lastName':user.getLastName()"+ "}"+ "}" + "}"+ "}"); Map&lt;String,Object&gt; map = (Map&lt;String,Object&gt;)Ognl.getValue(expression1, o); } } </code></pre> <p>User is a custom class that has firstName and lastName as properties.</p> <p>What I am trying to achieve using the OGNL expr is to iterate the list, retrieve each key and then pass that key to the map to retrieve the value (User object) and then call some property on that object.</p> <p>While I am able to iterate the list using</p> <pre><code>'users': getKeys().{ //I need to access the getData() method of the root object here passing the current #this object } </code></pre> <p>My OGNL expression throws the following error Exception in thread "main" ognl.NoSuchPropertyException: java.lang.String.outerObj</p> <p>Please help. I found tons of stuff on google on using OGNL with struts2 tags, but there's nothing much on its usage to access object properties like above.</p> <p>Regards, Suv</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload