2

On submitting the page, i need to do some process and send some data to the controller. I felt the 'Map' suits for my requirement to pass in the data. Here is what i am doing:

JSP:

<form:hidden id="passMapData" path="passMapData"/>

Controller:

@RequestMapping(value = "/newPage/testData", method = RequestMethod.POST)
public String newPageTestData(@Valid @ModelAttribute("npf") NewPageForm npf, BindingResult result, Model model) {

}

NewPageForm.java:

public class NewPageForm {
  private Map<String, String> passMapData = null;

  public Map<String, String> getPassMapData() {
    return passMapData;
  }

  public void setPassMapData(Map<String, String> passMapData) {
    this.passMapData = passMapData;
  }
}

Issue:

On submitting the form, the BindingResult in the controller is showing error "IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Map]"

Cant we pass a Map type to controller in form submit?

user2723039
  • 221
  • 2
  • 7
  • 16

1 Answers1

4

<form:hidden id="passMapData" path="passMapData"/> is used to pass the hidden String data. Basically it is a hidden field like <input type='hidden'/> in html.

You cannot pass the HashMap here,So your NewPageForm.java must be ,

public class NewPageForm {
  private String passMapData = null;

  public String getPassMapData() {
    return passMapData;
  }

  public void setPassMapData( String passMapData) {
    this.passMapData = passMapData;
  }
}

Note : If you need to pass the HashMap to the controller , just set the Hashmap in to the request of the JSP ,

request.setAttribute("passMapData",Your HashMap);

You can able to get the same Hashmap in the controller as ,

request.getAttribute("passMapData");

Hope this helps.

Human Being
  • 8,269
  • 28
  • 93
  • 136
  • Thanks for confirming that we cannot pass Map in form submit. I really appreciated. I am gonna make it as string. – user2723039 Mar 11 '14 at 15:22