簡體   English   中英

循環時獲取“既沒有 BindingResult 也沒有可用作請求屬性的 bean 名稱‘bean 名稱’的普通目標對象”

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

我正在嘗試使用 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:模板解析時出錯(模板:“類路徑資源[模板/索引.html]”)
引起:org.attoparser.ParseException:處理器執行期間出錯
'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor'(模板:“index” - 第 9 行,第 31 欄)
引起:org.thymeleaf.exceptions.TemplateProcessingException:執行處理器'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor'時出錯(模板:“索引”-第9行,第31列)
引起:java.lang.IllegalStateException:Bean 名稱“colorObj”的 BindingResult 和普通目標對象均不可用作請求屬性

這個 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>

我的控制器:

        @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("/");
    }

顏色等級

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

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

最后是我的 Type 類

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

        Type(String name){
            displayName=name;
        }

        private final String displayName;

        public String getDisplayName() {
            return displayName;
        }
    }

我能夠通過將以下方法添加到我的控制器來解決這個問題:

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

這是舊的! 不是真正的解決方案。 我的解決方案最終將 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>

最初的問題不起作用,因為顯然你不能在來自 th:each 的東西上使用 th:object。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM