'Thymeleaf form bind object

I am trying to bind attributes that I get from calling controller in thymeleaf template to form and pass it as param in save method. here is my code:

@GetMapping("/alert/{id}")
public String getForm(Model model, @PathVariable(required = false, name = "id") String id) throws IOException {
    final Datum datum = alertService.findById(id);
    final List<ElasticAlert> elasticAlerts = ApplicationUtil.datumToElasticAlertModel(Collections.singletonList(datum), objectMapper);
    if (elasticAlerts.size() != 1)
        throw new IllegalArgumentException("Wrong id for editing data");
    final ElasticAlert elasticAlert = elasticAlerts.stream().findFirst().orElseThrow(IllegalArgumentException::new);
    elasticAlert.setParams(datum.getParams());
    model.addAttribute("elasticAlert", elasticAlert);
    return "update";
}
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Params {
    private String aggType;
    private String esQuery;
    private Integer termSize;
    private String thresholdComparator;
    private Integer timeWindowSize;
    private String timeWindowUnit;
    private String groupBy;
    private List<Integer> threshold;
    private List<String> index;
    private String timeField;
    private String aggField;
    private String termField;
    private String level;
    private String message;
    private List<String> to;
    private String subject;
    private int size;
}
@Data
public class ElasticAlert {

    private String id;

    @NotBlank (message = "Event Name can not be blank")
    @Size(max = 40, min = 5, message = "event name should be between 5-20 characters")
    private String eventName;

    @NotBlank (message = "Application Name can not be blank")
    private String applicationName;

    @NotBlank (message = "Email Subject can not be blank")
    private String emailSubject;

    @NotBlank (message = "Email to can not be blank")
    @Pattern(regexp = "[a-zA-Z0-9]+@[abc|xyz]+goninv\\.com$", message = "incorrect email format")
    private String emailTo;

    @Valid
    private List<ElasticException> elasticExceptionList = new ArrayList<>();

    private String schedule;
    private String notifyWhen;
    private Params params;

}

I want to pass params object as it is to my form controller

 <form method="post" th:action="@{/alert}" th:object="${elasticAlert}"
          name="createAlertForm" id="createAlertForm" class="mb-3">
        <input type="hidden" th:field="${elasticAlert.id}"/>
        <input type="hidden" th:field="${elasticAlert.applicationName}"/>
        <input type="hidden" th:valuetype="test.pojo.Params" th:field="${elasticAlert.params}" />
...



@PostMapping("/alert")
    public String edit(@Valid ElasticAlert alert, BindingResult bindingResult, RedirectAttributes redirAttrs, Model model) throws IOException {
        int i = 1;
        final Params params = alert.getParams();
...
       
    }

my params object is always null, it's not binding object to the input, is there any way to make it bind or an alternate way to approach this. I am new to thymeleaf.



Solution 1:[1]

I guess the problem is in the first method,your request is @GetMapping("/alert/{id}"),but your thymeleaf is /alert,but the /alert request don't send messages.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 xy tang