1

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.

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Angelo, Thanks for the quick reply. But I need to pass whole object when we submit the form. Then what will I need to do? I have write two other dropdown below this one and it is working with th:value:${type}. I am getting problem in only this one. And even getting same error for commented option.
if you need to pass the whole object you must write it in the html page /maybe in hidden fields) or you must create a JS object to pass in rest call
oh okay. Thanks. Now I am using rest call to submit the object.. But question is still there for thymeleaf that why above code is not working for particular select where it is working with other select for same implementation. Strange.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.