Thymeleaf BindingResult 或 bean 名称“person"的普通目标对象都可用作请求属性 [英] Thymeleaf Neither BindingResult nor plain target object for bean name 'person' available as request attribute

查看:38
本文介绍了Thymeleaf BindingResult 或 bean 名称“person"的普通目标对象都可用作请求属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,设置正确,但出现以下错误:

From what I can tell this is set up correctly but I am getting the following error:

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

表格

<form action="#" th:action="@{/person}" th:object="${person}" method="post" th:required="required">
    <input type="text" th:field="*{subject}" class="contact col-md-6" placeholder="Name *" th:required="required"/>
    <input type="text" th:field="*{name}" class="contact col-md-6" placeholder="Name *" th:required="required"/>
    <input type="text" th:field="*{lastName}" class="contact col-md-6" placeholder="Name *" th:required="required"/>
    <input type="email" th:field="*{email}" class="contact noMarr col-md-6" placeholder="E-mail address *" th:required="required"/>
    <textarea name="comment" class="contact col-md-12" th:field="*{message}" placeholder="Message *"></textarea>
    <input type="submit" id="submit" class="contact submit" value="Send message"/>
</form>

Person.java

Person.java

public class Person {

    private int id;
    private String name;
    private String lastName;
    private String email;
    private String subject;
    private String message;

    ....
}

控制器

@Controller
public class ApplicationController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String indexPage() {
        return "index";
    }

    @RequestMapping(value="/person", method=RequestMethod.GET)
    public String contactForm(Model model) {
        model.addAttribute("person", new Person());
        return "index";
    }

    @RequestMapping(value="/person", method=RequestMethod.POST)
    public String contactSubmit(@ModelAttribute Person person, Model model) {
        model.addAttribute("person", person);
        return "result";
    }
}

我查看了 Spring-boot 和 Thmeleaf 设置,它看起来就像我的设置一样.

I looked at Spring-boot and Thmeleaf setup and it looks like my setup is identical.

--------------------- 更新 1 ---------------

--------------------- Update 1 -----------------------

我已将我的 post 方法更改为包含 BindingResult,但没有成功.

I have changed my post method to include BindingResult with no success.

@RequestMapping(value="/person", method=RequestMethod.POST)
public String contactSubmit(@Valid @ModelAttribute Person person, BindingResult bindingResult, Model model) {

    if(bindingResult.hasErrors()){
        System.out.println("There was a error "+bindingResult);
        System.out.println("Person is: "+ person.getEmail());
        return "index";
    }

    model.addAttribute("person", person);
    return "result";
}

推荐答案

你忘记添加 BindingResult after 你的 @ModelAttribute :

You forgot to add BindingResult after your @ModelAttribute :

@RequestMapping(value="/person", method=RequestMethod.POST)
public String contactSubmit(@ModelAttribute Person person, BindingResult bindingResult, Model model) {
    if (bindingResult.hasErrors()) {
        //errors processing
    }  
    model.addAttribute("person", person);
    return "result";
}

我已经回答过这样的问题:

I'm already have answered to question like this :

这篇关于Thymeleaf BindingResult 或 bean 名称“person"的普通目标对象都可用作请求属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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