Spring przy adnotacji ModelAttribute cały czas zwraca wyjątek

0

Cześć.
Próbuję napisać obsługę formularza w Springu za pomocą adnotacji @ModelAttribute, walidacja jest w 'obieckie transferowym' (?) Ale wypluwa mi ciągle wyjątek: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'formDto' available as request attribute.

Szukałem na Stacku, ale nie bardzo te odpowiedzi działają cały czas to samo...
Znalazłem tutaj wzmiankę o tym : http://www.baeldung.com/spring-mvc-form-tutorial , ale adnotacje mam wpisaną, próbowałem również na piechotę dodać obiekt z walidacją "formDto" jako atrybut do modelu, nic to nie dało.

Wskazuje na 7 linijke w jsp, przy deklaracji formularza, zmieniłem nawet modelAttribute na commandName chociaż to bez sensu...

Kontroler:

@Controller
public class CatsController {

    @Autowired
    private CatDAO dao;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String showListOfCats(ModelMap modelMap) {
        modelMap.addAttribute("cats", dao.getCats());
        return "catsList";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String showForm() {
        return "addCat";
    }
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addCatsDetails(@ModelAttribute ("formDto") @Valid FormDTO formDto, BindingResult result, ModelMap model){
        if (!result.hasErrors()) {
            Cat kitten = new Cat();
            kitten.setCatName(formDto.getCatName());
            kitten.setGuardian(formDto.getGuardian());
            kitten.setWeight(formDto.getWeight());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy");
            try {
                kitten.setDate(simpleDateFormat.parse(formDto.getDate()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            dao.addCat(kitten);
            model.clear();
            return "redirect:/details";
        }return "addCat";
    }

    @RequestMapping(value = "/details", method = RequestMethod.GET)
    public String showDetails (ModelMap modelMap, @RequestParam Integer id) {
       modelMap.addAttribute("catById", dao.getCatById(id));
        return "details";
    }

}

JSP:

<%@include file="common/header.jspf"%>
<div class="container">
    <h2>Cat's details</h2>
    <c:set var="ifPost" value="${pageContext.request.method=='POST'}"/>
    <form:form method="post" modelAttribute="formDto" action="/add">
        <div class="form-group">
            <form:label path="catName">Cat's name</form:label>
            <form:input path="catName" type="text" class="form-control"  placeholder="Enter your cat's name" />
            <c:if test="${ifPost}"><form:errors path="catName" cssClass="error"></form:errors></c:if>
        </div>
        <div class="form-group">
            <form:label path="guardian">Guardian's cat</form:label>
            <form:input path="guardian" type="text" class="form-control"  placeholder="Enter guardian's name"/>
            <c:if test="${ifPost}"><form:errors path="guardian" cssClass="error"></form:errors> </c:if>
        </div>
        <div class="form-group">
            <form:label path="weight" >Weight</form:label>
            <form:input path="weight" type="number" class="form-control"  placeholder="Enter cat's weight"/>
            <c:if test="${ifPost}"><form:errors path="weight" cssClass="error"></form:errors> </c:if>
        </div>
        <div class="form-group">
            <form:label path="date" >Date of Birth</form:label>
            <form:input path="date" type="text" class="form-control"  placeholder="Enter cat's date of birth dd.MM.yyyy"/>
            <c:if test="${ifPost}"><form:errors path="date" cssClass="error"></form:errors></c:if>
        </div>
        <div>
            <input path="submit" type="submit" class="btn btn-default" value="Submit">
        </div>
    </form:form>
</div> 
<%@include file="common/footer.jspf"%>

Klasa z walidacją:

public class FormDTO {
   @NotEmpty
    @Size(min=3)
    private String catName;

    @NotEmpty
    @Size(min=4)
    private String guardian;

    @NotEmpty
    @Range(min = 1, max = 10)
    private double weight;

    @NotEmpty
    @DateTimeFormat(pattern = "dd.MM.yyyy")
    @Min(1)
    @Max(20)
    private String date;

    public String getCatName() {
        return catName;
    }

    public void setCatName(String catName) {
        this.catName = catName;
    }

    public String getGuardian() {
        return guardian;
    }

    public void setGuardian(String guardian) {
        this.guardian = guardian;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }
}
0

Rozwiązane, RequestMethod przy /add musi być wypełnione modelem

0

Co to znaczy że RequestMethod musi być wypełnione modelem?

1 użytkowników online, w tym zalogowanych: 0, gości: 1