简体   繁体   English

嗨,我想用 thyleaf 填充 html 的 select 并出现错误

[英]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.

相关问题 我想打印 hi GrandFather;但它似乎打印 hifather - I want to print hi GrandFather;but it seems to print hi father 嗨,我正在尝试填充数组以计算平均值。 但是它一直在给出错误 - Hi, I'm trying to fill an array to compute the average. However it keeps giving an error 大家好,我想使用Java代码在我的文本文件中搜索单词“((错误:87)”) - Hi All, I want to search this word “(Error: 87)” in my text file using Java code 我想在一个组合中选择一个值,另一个将在同一页面上填充 - I want on select value in one combo the other will fill on same page 嗨,我想使用math.random设置重复的可能性 - Hi I want to use math.random to set the likely for it to repeat 返回hi出现的次数 - Return the number of times hi appears 嗨,我想从我的应用中禁用(删除)这些权限 - Hi i want to disable (delete) these permissions from my app 嗨,我想通过单击菜单从汉堡更改为交叉 - Hi i want to change from hamburgur to cross on click of the menu 嗨,我有一个 findViewById 错误 - HI.I have an error with findViewById 我想在挂毯页面中发布包含酒店的列表,但是此错误反复出现 - I want to post a lists with hotels in a tapestry page but this error appears over and over
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM