0

I have a map in Java

Map<String, Object> model = new HashMap<>();

Each (key,value) pair of this map is a list of urls:

List<String> thumbnailImgUrl = new ArrayList<>();
for (....) {
    thumbnailUrl.add(url);
}
model.put("photoUrl", thumbnailImgUrl);

I am passing the object "model" to the StringTemplate.

How can I access individual urls in string template?

Notification(model) ::= <<
  {how can I access the individual urls here?}
>>
Oleg Estekhin
  • 8,063
  • 5
  • 49
  • 52
hari shankar
  • 27
  • 1
  • 7
  • `List urls = (List)(model.get("photoUrl"));` – Luiggi Mendoza May 21 '14 at 07:19
  • what @LuiggiMendoza suggested is theoretically correct, assuming that you always have key-value pairs of type `String,List`. If that is not the case, you will get an exception (casting will fail). If your map indeed always have such key-value pairs, consider creating it like this `Map> model = new HashMap<>();` – Anton Sarov May 21 '14 at 07:28
  • @Anton you won't get an exception, you will get a compiler warning. The only way OP could get a `ClassCastException` is if he/she stores something different than a `List` in `model` using `"photoUrl"` as key. – Luiggi Mendoza May 21 '14 at 07:29
  • @LuiggiMendoza that's what I meant to write, my bad. But still, converting the map to `Map>` would make life much more easier. Though I fear the OP uses the map for all kind of object types. I personally see `Object` as a last resort but that's a different topic – Anton Sarov May 21 '14 at 07:40
  • @Anton we don't know if indeed there are `String`, `Integer` or other kind of data in `model`. This `model` object seems like a small `HttpSession.setAttributes(String, Object)` for me. – Luiggi Mendoza May 21 '14 at 07:42
  • possible duplicate of [how do I iterate though a java list in stringtemplate?](http://stackoverflow.com/questions/18437005/how-do-i-iterate-though-a-java-list-in-stringtemplate) – Oleg Estekhin May 21 '14 at 08:06

4 Answers4

1

I have already flagged this question as a duplicate, but just to prevent any more comments or answers that discuss how to cast or iterate in Java, the correct StringTemplate template that answers the question is

Notification(model) ::= <<
    <model.photoUrl:{v | model.photoUrl[<i>]=<v> }>
>>

The map.key accesses the item in a map and the <list:{v|<v>}> part iterates over a list and applies an anonimous sub-template to each element of the list.

Oleg Estekhin
  • 8,063
  • 5
  • 49
  • 52
  • I am trying to put these in a html block like this }> but it's not working. What am I doing wrong?? – hari shankar May 23 '14 at 13:18
  • You need to use some other characters as ST delimiters instead of '<' and '>'. The ['$' char is usually used](http://stackoverflow.com/questions/8490351/stringtemplate-remove-as-delimiters?lq=1) when mixing ST and HTML. [Another example](http://stackoverflow.com/questions/11146113/how-to-get-stringtemplate-v4-to-ignore-as-delimiter) of using ST and HTML together. – Oleg Estekhin May 23 '14 at 13:45
  • You can also define ST delimiters directly in the group file by using `delimiters "$", "$"` [directive](https://theantlrguy.atlassian.net/wiki/display/ST4/Group+file+syntax). – Oleg Estekhin May 23 '14 at 13:50
  • 1
    I found this site [cheat sheet](https://theantlrguy.atlassian.net/wiki/display/ST4/StringTemplate+cheat+sheet) very useful for understanding stringTemplate. Hope fully it will help others too. – hari shankar May 26 '14 at 05:33
0

To achieve this, You have to use a loop and a temporary variable in the following way:

Notification(model) ::= <<
$model.thumbnailImgUrl: {thumbnailImgUrl | URL is :- $thumbnailImgUrl.url$}$
>>

I hope this helps.

AnirbanDebnath
  • 990
  • 16
  • 26
-1

Get the Object and cast it to your class type-

List<String> urls = (List<String>)(model.get("photoUrl"));
Deepu--Java
  • 3,742
  • 3
  • 19
  • 30
-1

Try this work for me

for (Map.Entry<String, String[]> entry : model.entrySet()) {
        ArrayList<String> urlLst= (ArrayList<String>) entry.getValue();
        for(String url:urlLst)
            System.out.println(url);
}
gifpif
  • 4,507
  • 4
  • 31
  • 45