首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Thymeleaf表单绑定对象

Thymeleaf表单绑定对象
EN

Stack Overflow用户
提问于 2022-02-28 22:50:55
回答 1查看 534关注 0票数 0

我正在尝试绑定我从调用thymeleaf模板中的控制器中获得的属性,以便在保存方法中将其作为param来形成和传递。这是我的代码:

代码语言:javascript
复制
@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";
}
代码语言:javascript
复制
@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;
}
代码语言:javascript
复制
@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;

}

我想把params对象像它一样传递给我的窗体控制器。

代码语言:javascript
复制
 <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}" />
...
代码语言:javascript
复制
@PostMapping("/alert")
    public String edit(@Valid ElasticAlert alert, BindingResult bindingResult, RedirectAttributes redirAttrs, Model model) throws IOException {
        int i = 1;
        final Params params = alert.getParams();
...
       
    }

我的params对象总是空的,它不是将对象绑定到输入,有什么方法使它绑定或替代的方式来处理这个问题。我对你很陌生。

EN

回答 1

Stack Overflow用户

发布于 2022-03-01 09:21:39

我想问题是在第一种方法中,您的请求是@GetMapping(“/告警/{id}”),但是您的/alert是/alert,但是/alert请求不发送消息。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71301901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档