Search code examples
javaxpagesxpages-ssjs

generic set to access fields in a Java object via SSJS


Is there a way to access in a more flexible way fields on a Java object via SSJS?

So when my Java object is stored in a obj variable I can access the created field via obj.created.

However I would make my solution a bit more flexible so the name of the field will be provided via a property definition on a custom control.

The name of the field I get via:

compositeData.fieldName

How can I bend this to:

var field = compositeData.fieldName;
obj.????

I tried

obj.getField(field);

(source: https://www.tutorialspoint.com/java/lang/class_getfield.htm)

gives me an error:

com.ibm.xsp.binding.javascript.JavaScriptValueBinding.getValue(JavaScriptValueBinding.java:132)

Is there someone with a suggestion?


Solution

  • You should be able to access object properties by bracket notation:

    obj.fieldName

    is the same as

    obj[compositeData.fieldName]

    You may also want to look at java reflection which may allow you to do:

    obj.get(compositeData.fieldName)