我正在尝试绑定我从调用thymeleaf模板中的控制器中获得的属性,以便在保存方法中将其作为param来形成和传递。这是我的代码:
@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;
}我想把params对象像它一样传递给我的窗体控制器。
<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();
...
}我的params对象总是空的,它不是将对象绑定到输入,有什么方法使它绑定或替代的方式来处理这个问题。我对你很陌生。
发布于 2022-03-01 09:21:39
我想问题是在第一种方法中,您的请求是@GetMapping(“/告警/{id}”),但是您的/alert是/alert,但是/alert请求不发送消息。
https://stackoverflow.com/questions/71301901
复制相似问题