Jimmy
Jimmy

Reputation: 1875

java.lang.NumberFormatException: For input string: "com.connectors.entity.Templates@156d878f"

I am using Thymeleaf from some point in time and I am facing this issue several times.

I am getting below error when I add dynamic row using Spring Boot:

java.lang.NumberFormatException: For input string: "com.connectors.entity.Templates@156d878f"

When GET request method this works perfectly but when I redirect from post method I am getting above exception.

HTML code:

<div class="col-sm-5">
  <select th:field="*{templates}" class="form-control" name="queueMGR" id="queueMGR">
    <!-- <option selected="selected" disabled="disabled" value="Choose...">Choose...</option> -->
    <option th:each="type : ${templatesList}" th:value="${type}" th:text="${type.name}"></option>
  </select>
</div>

Spring request handler:

@RequestMapping(value = "/add", params = {"addRow"})
public String addRow(final AddIntegrations addIntegrations,
                     final BindingResult bindingResult, Model model) {

    addIntegrations.getHeaderProperties().add(new HeaderProperties());

    List<Templates> templatesList = new ArrayList<Templates>();
    Templates templates = new Templates();
    templates.setId(1);
    templates.setName("first template");
    templates.setContext("context");
    templatesList.add(templates);

    templates = new Templates();
    templates.setId(2);
    templates.setName("second template");
    templates.setContext("context");
    templatesList.add(templates);

    model.addAttribute("templatesList", templatesList);
}

Now if I add another select - option and for that if I add another list in modal it works fine.

Only getting problem for this select option. Here I am getting error for commented option tag as well. like below:

java.lang.NumberFormatException: For input string: "Choose..."

Please guide me.

thanks in advance.

Upvotes: 1

Views: 2182

Answers (1)

Angelo Immediata
Angelo Immediata

Reputation: 6944

When you call ${type} you call the toString() method of class Templates In your value you should use ${type.id} Something like this:

<div class="col-sm-5">
    <select th:field="*{templates}" class="form-control" name="queueMGR" id="queueMGR">
          <!-- <option selected="selected" disabled="disabled" value="Choose...">Choose...</option> -->
         <option th:each="type : ${templatesList}" th:value="${type.id}" th:text="${type.name}"></option>
    </select>
</div>

Upvotes: 1

Related Questions