【问题标题】:Thymeleaf Neither BindingResult nor plain target object for bean name 'person' available as request attributeThymeleaf Bean 名称“person”的 BindingResult 和普通目标对象都不能用作请求属性
【发布时间】:2016-08-12 02:41:25
【问题描述】:

据我所知,这是正确设置的,但我收到以下错误:

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

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 and Thmeleaf setup,看起来我的设置是相同的。

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

我已更改我的 post 方法以包含 BindingResult,但没有成功。

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

【问题讨论】:

    标签: java spring-boot thymeleaf


    【解决方案1】:

    您忘记在您的@ModelAttribute 之后添加BindingResult

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

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

    【讨论】:

      【解决方案2】:

      在调用 post 方法之前,必须初始化模型属性(使用 GET 方法)。 在您的情况下,您需要在控制器中再使用一种方法来执行 model.addAttribute("person",new Person()); 并且必须在发布之前调用它。

      参考以下链接: https://spring.io/guides/gs/handling-form-submission/ 要么 http://forum.thymeleaf.org/Neither-BindingResult-nor-plain-target-object-for-bean-name-miniDoniie-available-as-request-attribute-td4027859.html

      它在控制器中有GetMappingPostMapping

      【讨论】:

        【解决方案3】:

        首先我有index.html中的表格

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

        所以当我的表格:

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

        正在寻找/,它正在使用上述方法,而不是:

        @RequestMapping(value="/", method=RequestMethod.GET)
            public String contactForm(@Valid @ModelAttribute("person") Person person, BindingResult bindingResult,
                                      HttpServletRequest request, Model model) throws IOException {
        
                if(bindingResult.hasErrors()){
                    System.out.println("There was a error "+bindingResult);
        
                    return "index";
                }
        
                model.addAttribute("person", new Person());
                return "index";
            }
        

        这是正确的!

        我不得不删除第一个方法,它奏效了。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-23
          • 2015-08-31
          • 2013-04-04
          • 2011-11-25
          • 2019-10-29
          • 2013-08-15
          相关资源
          最近更新 更多