[英]Hi, I want fill an select of html with thyleaf and appears an Error
I want to fill a select with a Map as ModelAttribute but when I execute the app and try load my load appears a error.我想用 Map 填充 select 作为 ModelAttribute,但是当我执行应用程序并尝试加载时,我的负载出现错误。
java.lang.IllegalStateException: Invalid target for Validator [com.bolsadeideas.springboot.form.app.validation.UserValidator@2343043c]: España at org.springframework.validation.DataBinder.assertValidators(DataBinder.java:542) ~[spring-context-5.2.9.RELEASE.jar:5.2.9.RELEASE] at org.springframework.validation.DataBinder.addValidators(DataBinder.java:553) ~[spring-context-5.2.9.RELEASE.jar:5.2.9.RELEASE] at com.bolsadeideas.springboot.form.app.controller.FormController.initBinder(FormController.java:54) ~[classes/:na]
My html is this我的html是这个
<form th:action="@{/form}" method="post" th:object="${user}">
<div>
<label for="country">Country</label>
</div>
<div>
<select id="country" th:field="*{country}">
<option value="">--Select Country--</option>
<option th:each="country: ${countriesMap.entrySet()}"
th:text="${country.value}" th:value="${country.key}"></option>
</select>
</div>
<div th:if="${#fields.hasErrors('country')}" th:errors="*{country}"></div>
and my controller code:和我的 controller 代码:
@InitBinder // elemento del ciclo de vida del controlador
public void initBinder(WebDataBinder binder) {
binder.addValidators(validator);
// CustomEditor cuztomizar un campo que obtemenos y convertirlo en otro tipo de
// dato es como un filtro
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
// Eso es la indulgencia, es decir, define si en analizdor que va a realizar un
// análisis un (parse) de la fecha con false se vuelve mas estricto
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}// llenando la vista Select con un Map
@ModelAttribute("countriesMap")
public Map<String, String> countriesMap() {
Map<String, String> countries = new HashMap<String, String>();
countries.put("ES", "España");
countries.put("MX", "México");
countries.put("CL", "Chile");
countries.put("EC", "Ecuador");
countries.put("CO", "Colombia");
countries.put("BO", "Bolivia");
countries.put("FR", "Francia");
return countries;
}
before I did not have problems using a simple List of java.util在我使用 java.util 的简单列表之前没有问题
@PostMapping("/form")
public String procesar(@Valid User user, BindingResult bindingResult, Model model, SessionStatus status) {
// asi validamos de forma explicita
// validator.validate(user, bindingResult);
model.addAttribute("title", "Resultado Form");
if (bindingResult.hasErrors()) {
return "form";
}
model.addAttribute("user", user);
/**
* completa el proceso manejo de datos y elimina los atributos u objetos
* almacenados en la sesion
*/
status.setComplete();
return "resultform";
}
I had model.addAttribute("user", "España");我有 model.addAttribute("user", "España"); was error type
是错误类型
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.