获取“既没有 BindingResult 也没有可用作请求属性的 bean 名称‘bean name’的普通目标对象"循环时 [英] Getting "Neither BindingResult nor plain target object for bean name 'bean name' available as request attribute" when looping

查看:26
本文介绍了获取“既没有 BindingResult 也没有可用作请求属性的 bean 名称‘bean name’的普通目标对象"循环时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Multipile Forms 来放入信息.除了类型信息之外,这些形式都是相同的.这在 Java 中的 Type enun 接口中表示.最好将表单的所有字段放入 Color 对象中.

如果我在没有循环的情况下执行此操作,并更改给 thymeleaf 的内容,它会起作用.

我使用 Java 1.8.0_231、spring-core 5.1.6、springboot 2.1.4、thymeleaf 3.0.11 运行它注意:这不是完整列表!

以下是我当前的相关代码:

我的问题是,百里香叶出现以下错误:

<块引用>

org.thymeleaf.exceptions.TemplateInputException: 模板解析时发生错误(模板:类路径资源[模板/index.html]")
引起:org.attoparser.ParseException:处理器执行过程中出错
'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor'(模板:索引" - 第 9 行,第 31 栏)
引起:org.thymeleaf.exceptions.TemplateProcessingException:处理器执行过程中出错'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor'(模板:索引" - 第 9 行,第 31 栏)
引起:java.lang.IllegalStateException:对于 bean 名称colorObj"既没有 BindingResult 也没有普通目标对象作为请求可用属性

这个 index.html 从第 7 行开始

 <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}"/><输入类型=提交"/></表单><br></th:block>

我的控制器:

 @GetMapping("/")公共字符串索引(模型模型){列表<颜色>颜色 = 新 java.util.ArrayList<>(Collections.emptyList());for (int i =0;i

颜色分类

 @Data @Getter公共类颜色{私人字符串颜色=";私人字符串传递 = "";私有类型;公共颜色(类型类型){this.type=type;}}

最后是我的 Type 类

 公共枚举类型 {颜色(颜色"),窗格(窗格");类型(字符串名称){显示名称=名称;}私有最终字符串显示名称;公共字符串 getDisplayName() {返回显示名称;}}

解决方案

通过将以下方法添加到我的控制器中,我能够解决该问题:

 @ModelAttribute颜色设置表格 () {返回新颜色();}

这是旧的!不是真正的解决方案.我的解决方案最终将 index.html 编辑为以下内容:

 <div class="col-sm-3" th:each="type,iter : ${T(com.piinfo.service.Type).值()}"><form class="卡片形式-颜色"方法=POST"动作=#"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 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 class="form-group"><input type="hidden" th:value="${type}" name="type" th:id="'type'+${iter.count}"/><input class="form-control" type="submit">

</表单>

最初的问题不起作用,因为显然你不能在来自 th:each 的东西上使用 th:object.

I am trying to get Multipile Forms to put information into. Those forms are all identical, except in the type information. That is represented in Java in the Type enun interface. All fields of the form should go into a Color object preferrably.

If I do this without the loop, and change, what is given to thymeleaf, it works.

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!

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]")
Caused by: org.attoparser.ParseException: Error during execution of processor
'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "index" - line 9, col 31)
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "index" - line 9, col 31)
Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'colorObj' available as request attribute

this index.html starts at line 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

    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:

        <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.

这篇关于获取“既没有 BindingResult 也没有可用作请求属性的 bean 名称‘bean name’的普通目标对象"循环时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆