solikang
solikang

Reputation: 53

Using model map in Velocity

I'm using Spring MVC with velocity.

I'm curious about how to post map key-value from controller to another site.

@RequestMapping(value = "/test/postMap.*", method = RequestMethod.GET)
public String postMap(HttpServletRequest request, HttpServletResponse response,
          Model model) throws Exception {

  model.addAttribute(receiver, "solikang.com"); // not in real, just for example
  model.addAttribute(id, "solikang");
  model.addAttribute(pwd, "1234");
  return "postThis";
}

postThis.vm

<form id="oneItem" method="post" action=${receiver} >
  <input type="hidden" name="id" value="$!{id}" />
  <input type="hidden" name="pwd" value="$!{pwd}" />

</form>
<script type="text/javascript" language="JavaScript">
    document.getElementById("oneItem").submit();
</script>

In 'postThis.vm', I fixed property name like 'id' and 'pwd'.

This works, but if there are another parameters like 'name', 'email'. I have to modify postThis.vm to handle those parameters.

I think, using model map and for loop, no need to modify.

So, I want to know how to read model as map in velocity.

If you have any idea or experience, please let me know.

Thanks in advance.

Upvotes: 0

Views: 1269

Answers (1)

stevecass
stevecass

Reputation: 141

One option along the lines of what you already have should be to add the attributes to the model as a map. So in java

map = new HashMap(); 
map.put("id", "solikang")
map.put("pwd", "1234")
map.put("email", "[email protected]");
model.addAttribute("data", map);

Then in velocity

#foreach ($key in $data.keySet())
  <input type="hidden" name="$key" value="$data.get($key)">
#end

Something like that should work. But I'm not sure it's the "best" way to do it.

Upvotes: 1

Related Questions