循环时"Neither BindingResult nor plain target object for bean name 'bean name' available as request a



我正在尝试获取多堆表单以放入信息。这些形式都是相同的,除了类型信息。这在 Java 中的 Type enun 接口中表示。表单的所有字段最好都进入 Color 对象。

如果我在没有循环的情况下这样做,并改变给百里香叶的东西,它就会起作用。

我正在使用 Java 1.8.0_231、弹簧核心 5.1.6、弹簧引导 2.1.4、百里香叶 3.0.11 运行它 注意:这不是完整列表!

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

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

org.thymeleaf.exceptions.TemplateInputException:模板解析期间发生错误(模板:"类路径资源 [templates/index.html]"(
由以下原因引起:org.attoparser.ParseException:执行处理器'
org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' 时出错 (模板:"索引" - 第 9 行,第 31 行(
由以下原因引起:org.thymeleaf.exceptions.TemplateProcessing异常:执行处理器期间出错 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (模板:"索引" - 第 9 行,第 31 行(
由以下原因引起:java.lang.IllegalStateException:BindingResult 和 Bean 名称 'colorObj' 的纯目标对象都不能作为请求使用 属性

此索引.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;
}
}

最后是我的类型类

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

这太老了!不是真正的解决方案。 我的解决方案最终编辑索引.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。

相关内容

最新更新