我开始使用 thymeleaf 。
我正在关注此页面:Spring MVC view layer: Thymeleaf vs. JSP
我有一个类:
public class MyMainObject {
private String a;
private String b;
private String c;
private String d;
private String e;
// getters and setters
}
我还有一个 Controller :
@Controller
public class MyMainObjectController extends AbstractController
@RequestMapping({"/subscribeth"})
public String getObj(final MyMainObject subscription) {
return "subscribeth";
}
}
这是我的 html 代码:
<form action="#" th:object="${subscription}" th:action="@{/subscribeth}">
<fieldset>
<div>
<label for="a" th:text="#{subscription.a}">a:
</label> <input type="text" th:field="*{a}" />
</div>
<div>
<label for="b" th:text="#{subscription.b}">b:
</label> <input type="text" th:field="*{b}" />
</div>
<div>
<label for="c" th:text="#{subscription.c}">c: </label>
<input type="text" th:field="*{c}" />
</div>
<div>
<label for="d" th:text="#{subscription.d}">d:
</label> <input type="text" th:field="*{d}" />
</div>
<div>
<label for="e" th:text="#{subscription.e}">e:
</label> <input type="text" th:field="*{e}" />
</div>
<div class="submit">
<button type="submit" name="save" th:text="#{subscription.submit}">Subscribe me!</button>
</div>
</fieldset>
</form>
当我运行 mu 应用程序时,出现错误:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring3.processor.attr.SpringInputGeneralFieldAttrProcessor'
org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring3.processor.attr.SpringInputGeneralFieldAttrProcessor'
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'subscription' available as request attribute
看起来,我必须创建 bean 订阅
。
这是我的问题,我该如何做到这一点?我从之前提到的教程中下载了源代码,但找不到它。
提前谢谢
最佳答案
使用 @ModelAttribute("subscription")
注释 Controller 处理程序方法的参数。默认情况下,如果没有给定值,Spring将根据参数的类型生成一个属性名称。因此MyMainObject
将变成myMainObject
.
关于java.lang.IllegalStateException : Neither BindingResult nor plain target object for bean name 'subscription' available as request attribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22838137/