0

I am using Viewable in my REST environment to display JSPs. But when I am passing parameters through Viewable, I am not able access this parameter in JSP.

I have following code in my java file -

    Map<String, Object> map = new HashMap<String, Object>();
    map.put("posts", posts);
    map.put("name", "satpute");
    return new Viewable("/home",map);

I used different options to access this map parameter in JSP, but this returns null. I have used following ways -

<% String name = (String)request.getAttribute("name"); out.print(name); %>

Returns NULL.

Other way I tried to use it is -

<% String name = (String) pageContext.getAttribute("name"); out.print(name); %>

Returns NULL.

Sorry if this is a very basic question, but I have used request in JSP with servlet. But I am not able to figure out how to use JSP with Viewable.

Please help.

RSatpute
  • 59
  • 1
  • 1
  • 8
  • You need to get the `map` first, then have to iterate. Since you're sending the map and your `name` is inside of map. – Vinoth Krishnan Mar 03 '16 at 05:25
  • @Vinoth Krishnan can u pls tell me how can I access it exactly, is it getAttribute("map.name") or something else – RSatpute Mar 03 '16 at 05:27
  • [Here](http://stackoverflow.com/questions/2117557/how-to-iterate-an-arraylist-inside-a-hashmap-using-jstl) is the [BalusC](http://stackoverflow.com/users/157882/balusc) explanation. – Vinoth Krishnan Mar 03 '16 at 05:29
  • Yes, I tried that too, but this helps to access parameters in JSTL, but not in java scriptlet. And I am not using JSTL or JSP-EL right now. It is just JSP and java code. – RSatpute Mar 03 '16 at 05:32
  • He explained scriptlet too, check his answer's last part. – Vinoth Krishnan Mar 03 '16 at 05:34
  • I read complete post and links too. He gave similar plain java code. This does not solve my problem of how to access it in scriptlet. I have this requirement because 'posts' Object passed through 'map' is complex and I need java code in JSP to read it properly. – RSatpute Mar 03 '16 at 05:45
  • I have answered how to iterate your map, but i'm not sure about your `posts` object (What kind of object). from `value` you can get the object, then cast it and iterate again to get the values. – Vinoth Krishnan Mar 03 '16 at 05:58

1 Answers1

0

You have to receive the map parameter instead of name,

<% Map<String, Object> map= Map<String, Object>request.getAttribute("map"); 
for (Map.Entry<String, Object> entry : map.entrySet()) {
    String key = entry.getKey();
    Object value = entry.getValue();
}%>

I'm not sure about your posts object (What kind of object). from value you can get the object, then cast it and iterate again to get the values.

But I would suggest to go with JSTL hereafter.

Vinoth Krishnan
  • 2,925
  • 6
  • 29
  • 34
  • I access map using `Map map= (Map) request.getAttribute("map"); ` and `name` using `Name : <% String name = (String) map.get("name"); out.print(name); %>`. This throws `java.lang.NullPointerException`. Am I accessing name in wrong way ? – RSatpute Mar 03 '16 at 06:02
  • Yes, you can't access the name like that. In my answer `key` will return the value `name` and `value` will return the specific value for `name`. – Vinoth Krishnan Mar 03 '16 at 06:04
  • `posts` is a [protocol buffer](https://developers.google.com/protocol-buffers) object. It has similar structure like JSON, but in object. I have some hierarchy in this object and I doubt that I will be able to use it well in JSTL as it has its own functions. And for now, I am just accessing plain `String` in map, but I am not able to access it. – RSatpute Mar 03 '16 at 06:09
  • I think the problem is in first line itself 'Map map= (Map) request.getAttribute("map");'. When I am printing it, it is returning `null`. Sorry :-(. I really think I should check JSTL once again. – RSatpute Mar 03 '16 at 06:28
  • I hope problem with where you're setting the value meaning `map` – Vinoth Krishnan Mar 03 '16 at 06:32