MiraTech
MiraTech

Reputation: 1302

@PostMapping doesn't work: Neither BindingResult nor plain target object for bean name 'XXX' available as request attribute

I am new to thymeleaf and Spring-Boot, I followed this tutorial to create a submit form I made exactly the same thing, but I am getting the following exception:

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

Can someone tell me what's wrong in my code, knowing that this is my Conjugation class

public class Conjugation {
    private String v;

    public String getV() {
        return this.v;
    }

    public void setV(String v) {
        this.verb = v;
    }

}

This is what I used in my controller

//The Controller
@GetMapping("/conjugate")
  public String vForm(Model model) {
    model.addAttribute("conjugate", new Conjugation());
    return "conjugate";
  }

@PostMapping("/conjugate")
  public String vbSubmit(@ModelAttribute Conjugation conjugate) {
    System.out.println("-->"+conjugate.getV());
    return "index";
  }

And this my index.html file:

<form action="#" th:action="@{/conjugate}" th:object="${conjugation}" method="post">
    <div class="cell">
        V
    </div>
    <div class="cell">
        <input class="cell" type="text"  th:field="*{v}" /> 
    </div>
    <div class="cell">
        <input type="submit" value="Submit" />
    </div>

Upvotes: 0

Views: 194

Answers (1)

dsarrrrrr443
dsarrrrrr443

Reputation: 48

You add the attribute named as conjugate:

model.addAttribute("conjugate", new Conjugation());

but in your html file you say conjugation

th:object="${conjugation}"

This will work if you replace your current model.addAttribute, with:

model.addAttribute("conjugation", new Conjugation());

Upvotes: 2

Related Questions