简体   繁体   English

循环时获取“既没有 BindingResult 也没有可用作请求属性的 bean 名称‘bean 名称’的普通目标对象”

[英]Getting "Neither BindingResult nor plain target object for bean name 'bean name' available as request attribute" when looping

I am trying to get Multipile Forms to put information into.我正在尝试使用 Multipile Forms 来放入信息。 Those forms are all identical, except in the type information.除了类型信息之外,这些形式都是相同的。 That is represented in Java in the Type enun interface.这在 Java 中的 Type enun 接口中表示。 All fields of the form should go into a Color object preferrably.表单的所有字段都应该最好进入 Color 对象。

If I do this without the loop, and change, what is given to thymeleaf, it works.如果我在没有循环的情况下执行此操作,并更改给 thymeleaf 的内容,它会起作用。

I am running this with Java 1.8.0_231, spring-core 5.1.6, springboot 2.1.4, thymeleaf 3.0.11 Note: this is not the full list!我正在使用 Java 1.8.0_231、spring-core 5.1.6、springboot 2.1.4、thymeleaf 3.0.11 运行它 注意:这不是完整列表!

Below is my current relavent code:以下是我目前的相关代码:

My problem is, that thymeleaf thows the following error:我的问题是,百里香叶出现以下错误:

org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/index.html]") org.thymeleaf.exceptions.TemplateInputException:模板解析时出错(模板:“类路径资源[模板/索引.html]”)
Caused by: org.attoparser.ParseException: Error during execution of processor引起:org.attoparser.ParseException:处理器执行期间出错
'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "index" - line 9, col 31) 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor'(模板:“index” - 第 9 行,第 31 栏)
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "index" - line 9, col 31)引起:org.thymeleaf.exceptions.TemplateProcessingException:执行处理器'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor'时出错(模板:“索引”-第9行,第31列)
Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'colorObj' available as request attribute引起:java.lang.IllegalStateException:Bean 名称“colorObj”的 BindingResult 和普通目标对象均不可用作请求属性

this index.html starts at line 7这个 index.html 从第 7 行开始

       <th:block th:each="colorObj : ${ColorDevs}">
        <form method="POST" action="#" th:action="@{/}" th:object="${colorObj}">
            <label th:text="${colorObj.type.displayName}+':&nbsp'" th:for="${#ids.next('color')}"></label>
            <input type=color th:field="*{color}"/>
            <br>
            <label th:text="'Passwort:&nbsp'" th:for="${#ids.next('pass')}"></label>
            <input type=password th:field="*{pass}"/>
            <br>
            <input type="hidden" th:field="*{type}"/>
            <input type="submit"/>
        </form>
        <br>
    </th:block>

My controllers:我的控制器:

        @GetMapping("/")
    public String index(Model model){
        List<Color> colors = new java.util.ArrayList<>(Collections.emptyList());
        for (int i =0;i<Type.values().length;i++) {
            colors.add(new Color(Type.values()[i]));
        }

        model.addAttribute("ColorDevs",colors);
        return "index";
    }

    @PostMapping("/")
    public RedirectView color(@ModelAttribute("color") Color color, BindingResult bindingResult){
        System.err.println(color.toString());
        return new RedirectView("/");
    }

Color class颜色等级

        @Data @Getter
    public class Color {
        private String color = "";
        private String pass = "";
        private Type type;

        public Color(Type type){
            this.type=type;
        }
    }

And lastly my Type class最后是我的 Type 类

    public enum Type {
        COLOR("Color"),
        PANE("Pane");

        Type(String name){
            displayName=name;
        }

        private final String displayName;

        public String getDisplayName() {
            return displayName;
        }
    }

I was able to solve the issue by adding the following method to My controller:我能够通过将以下方法添加到我的控制器来解决这个问题:

    @ModelAttribute
    Color setupForm () {
        return new Color();
    }

This is old!这是旧的! Not a real solution.不是真正的解决方案。 My solution ended up editing index.html to the following:我的解决方案最终将 index.html 编辑为以下内容:

        <div class="col-sm-3" th:each="type,iter : ${T(com.piinfo.service.Type).values()}">
            <form class="card form-color"
                  method="POST" action="#"
                  th:action="@{/}"
            >
                <div class="card-header text-center h2" th:text="${type.displayName}"></div>
                <div class="card-body">
                    <div class="form-group">
                        <label th:text="${type.displayName}+':&nbsp'" th:for="'color'+${iter.count}"></label>
                        <input class="form-control" type=color th:id="'color'+${iter.count}" name="color"
                               th:value="${colors.get(type)}"/>
                    </div>
                    <div class="form-group">
                        <label th:text="'Passwort:&nbsp'" th:for="'pass'+${iter.count}"></label>
                        <input class="form-control" type=password name="pass" th:id="'pass'+${iter.count}"/>
                    </div>
                    <div class="form-group">
                        <input type="hidden" th:value="${type}" name="type" th:id="'type'+${iter.count}"/>
                        <input class="form-control" type="submit">
                    </div>
                </div>
            </form>
        </div>

The original Question didn't work, because aparrently you can't use th:object on something, that came from th:each.最初的问题不起作用,因为显然你不能在来自 th:each 的东西上使用 th:object。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不断获取:BeanResult&#39;index&#39;的BindingResult和普通目标对象都不可用作请求属性 - Keep getting: Neither BindingResult nor plain target object for bean name 'index' available as request attribute BeanResult的BindingResult和普通目标对象都不能用作请求属性 - Neither BindingResult nor plain target object for bean name available as request attribute Bean名称“所有者”的BindingResult和普通目标对象都不能用作请求属性 - Neither BindingResult nor plain target object for bean name 'owner' available as request attribute IllegalStateException:BeanResult&#39;getAppointment&#39;的BindingResult和普通目标对象都不能用作请求属性 - IllegalStateException: Neither BindingResult nor plain target object for bean name 'getAppointment' available as request attribute Bean名称“ categorie”的BindingResult或普通目标对象都不能用作请求属性 - Neither BindingResult nor plain target object for bean name 'categorie' available as request attribute BindingResult和bean名称&#39;team&#39;的普通目标对象都不能作为请求属性使用 - Neither BindingResult nor plain target object for bean name 'team' available as request attribute BeanResult&#39;user&#39;的BindingResult和普通目标对象都不能用作请求属性 - Neither BindingResult nor plain target object for bean name 'user' available as request attribute bean 名称“categoryOptions”的 BindingResult 和普通目标对象都不能用作请求属性 - Neither BindingResult nor plain target object for bean name 'categoryOptions' available as request attribute BeanResult&#39;tweets&#39;的BindingResult和普通目标对象都不能用作请求属性 - Neither BindingResult nor plain target object for bean name 'tweets' available as request attribute 简单的Java Web验证Bean名称“数字”的BindingResult和普通目标对象都不能用作请求属性 - Simple Java Web validation Neither BindingResult nor plain target object for bean name 'number' available as request attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM