2

I want to create a hidden input field:

<input type="hidden" th:value="${map.version} name="version"/>

Problem:
version maybe a non existing attribute yet (I am not talking about null!).
Right now I am getting an Exception Property or field 'version' cannot be found on object

What I need:
If it does not exist, th:value statement maybe ignored or tag removed

CLARIFICATION:
map comes from Spring Controller in a handler-method:

@PostMapping("/new")
public String handleMapFormSubmit(
    @ModelAttribute("map") @Valid AddMapCommand command, BindingResult result ) {

    if ( result.hasErrors() ) {
        return "map-form";
    }

    // do some stuff

    return ".....";
}

Problem is that map (AddMapCommmand) in this handler-method does not contain the version attribute. In another handler-method (UpdateMapCommand) it does. The whole point is to reuse the map-form thymeleaf template in both scenarios which are almost similar.

Dachstein
  • 3,994
  • 6
  • 34
  • 61

1 Answers1

1

You can try the instanceof operator to be used only for the object that contains the property:

<input type="hidden" 
 th:if="${map instanceof T(my.project.UpdateMapCommand)}" 
 th:value="${map.version} name="version">

For future reference, it is extremely confusing using a variable like map and not have the reader interpret it as a java.util.Map. You should change your map variable name to make it less confusing, or at least for the purpose of asking the question on StackOverflow.

riddle_me_this
  • 8,575
  • 10
  • 55
  • 80