Mike Alba
Mike Alba

Reputation: 11

Resolve variables inside a string - JSTL?

I have been trying to figure out if what I am trying is possible using JSTL. I am storing the following inside a database table

<c:when test="${row.actionType == 'com'}">  
  ${row.actionText}  
</c:when>       

thus I am storing the following String in a column in a table

<assign name="LocationID" expr="'${row.locationId}'"/> 
<submit next="/aotg/dynApp" namelist="UserID AppID LocationID"/> 

thus the above string for example gets written to ${row.actionText} and gets resolved. But of course the output in the JSTL is exactly as above without resolving the variable ${row.locationId} inside the String. Can you also somehow resolve the inner variable as well?

Is something like this even possible or do I need to hard code any parameters I might be using inside the JSTL page instead of trying to dynamically read it from the table?

Upvotes: 1

Views: 568

Answers (2)

Mike Alba
Mike Alba

Reputation: 11

For now I just used a map to solve this. When I have more time I will see if there is a more elegant way to do this.

  Map<String, String> varMap = new HashMap<String, String>();
  varMap.put("#locationId#", locationId.toString());    
  varMap.put("#mainMenu#", mainMenu);   
  varMap.put("#userId#", userId.toString());
  varMap.put("#appId#", appId.toString());    

  command = string;

  for (Map.Entry<String, String> entry : varMap.entrySet()) {

      command = command.replace(entry.getKey(), entry.getValue());
  }      

Thanks

Upvotes: 0

Stephen P
Stephen P

Reputation: 14800

The .jsp file that contains the JSTL gets compiled into a .java file, which is then compiled into a .class file and run in the JVM.

Since the c:when block was not in the JSP at the time it was compiled it just becomes some output text when the class file is running.

Upvotes: 2

Related Questions