java.lang.IllegalStateException : Neither BindingResult nor plain target object for bean name 'subscription' available as request attribute

标签 java spring thymeleaf

我开始使用 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/

相关文章:

java - cron4j descheduling servlet/actionbean 上的任务

java - 如何在单个 Eclipse 项目中设置多个源文件夹?

java - 在 Spring 中动态添加 Google map (MVC 或 Boot)

java - 使用 Eclipse Indigo 在 Tomcat 6.0.35 上使用 Spring Web Flow

spring - 使用 thymeleaf 将表单数据发布到使用 @ModelAttribute (复杂对象)的 Controller

thymeleaf - 如何在不重新启动服务器的情况下查看 Broadleaf 中的 Thymeleaf 模板更改?

java - 节俭 : Unable to get a generator for "python"

java - Spring4配置CommonsMultipartResolver失败

spring-boot - Spring Boot 不使用 Thymeleaf 提供静态内容

java - 遍历 HashMap 中的 ArrayList