user401445
user401445

Reputation: 1014

Working with dynamic variable names in EL

In EL, I want to access the value

${settings_123456.settingsMap[test].value}

The problem is that settings_123456 is variable. So I stored it in ${setting} variable and tried ${setting.settingsMap[test].value} , but it is not working

Edit:

Public class Setting {
  Map<String, myClass> settingsMap;
}
Public class myClass {
   private String myTest;
}

The model is set with Setting object and I need to fetch the value of myTest variable in my jsp using jstl. Also note that the key for settingsMap is also dynamic that is why you can see test variable in JSP code.

Upvotes: 4

Views: 4152

Answers (2)

BalusC
BalusC

Reputation: 1108577

You can access it by explicitly specifying the scope map.

${requestScope[settings_123456].settingsMap[test].value}

Use ${sessionScope} or ${applicationScope} instead when it's session or application scoped.

Upvotes: 6

david a.
david a.

Reputation: 5291

You have to make settingsMap a property, i.e. declare a setter and a getter for it:

Public class Setting {
  Map<String, myClass> settingsMap;

  public Map<String, myClass> getSettingsMap() {
    return this.settingsMap;
  }

  public void setSettingsMap(Map<String, myClass> settingsMap) {
    this.settingsMap = settingsMap;
  }   

}

Upvotes: 0

Related Questions