0

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.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
juggernaut
  • 126
  • 18
  • How about your getters, setters and constructors in your Params class. – Ahmet Mar 01 '22 at 06:49
  • Store the object in the session using `@SessionAttributes` so it will be re-used again. Trying to shoehorn an objects toString back into an object (which is basically what you are trying to do) isn't going to work. – M. Deinum Mar 01 '22 at 09:18
  • @Ahmet the class is marked with data annotation which autogens getters setters and other boilerplate. They are there – juggernaut Mar 01 '22 at 13:55
  • @M.Deinum yeah I am aware I am trying to shoehorn the object and there is no easy way I can find to do it. Session attribute is a good option, will give it a shot – juggernaut Mar 01 '22 at 13:57

1 Answers1

0

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.