3
votes

Issue

I tried below sources. but occurred an error 'Validation failed for object'.
Referenced by this link(http://bitbybitblog.com/forms-and-data-models-in-spring-mvc/).
I guess how to form data(input values) send to the controller for the model 'Shop' without any error.
I think unmatched model 'Shop' and HTML form data. cannot figure out the solution.
How to fix 'tag' input name?

Sources

public class Tag {
    private Long id;
    private String name;
    private Date regDate = new Date();
}
public class Shop {
    private Long id;
    private String name;
    private String url;
    private String featureImagePath;
    private List&ltTag&gt tag = new ArrayList&lt&gt();
    private ShopStatus status = ShopStatus.SHOW;
    private Date expireDate;
    private Date updateDate;
    private Date regDate;
}
Controller
@RequestMapping(value = "/edit/update", method = RequestMethod.POST) public String update(@ModelAttribute Shop shop) { if (shop.getId() == null) { shopService.createShop(shop); } return "redirect:/"; }
HTML/Thymeleaf
&ltform name="editor" method="post" action="/edit/update"&gt &ltfieldset&gt &ltinput name='name' th:value='${data.name}'/&gt &ltinput name='tag[0].id'/&gt&ltinput name='tag[0].name'/&gt &lt/fieldset&gt &lt/form&gt


Result

occurred this problem then submit the form 'editor' on the browser.

This application has no explicit mapping for /error, so you are seeing this as a fallback.
There was an unexpected error (type=Bad Request, status=400). Validation failed for object='shop'. Error count: 1

1
That blog post about jsp spring but you have tried thymeleaf instead of jsp. You need to check thymleaf documentation. I can see few things are missing here. Form doesn't have th:object and form objects attributes need to be binded using th:field. Documentation is here thymeleaf.org/doc/tutorials/2.1/…SAP
@SAP thanks your reply. please refer to this thymeleaf version is 3.x. I already tried added "th:object" attribute several times and checked document. didn't occurred the other pages without 'th:object'.Matthew Shin
You are not following Thymeleaf documentation. Check this: thymeleaf.org/doc/tutorials/2.1/…. you haven't included any attributes like 'th:field' in your input field which used to map your fields with ModelAttribute in controllerAfridi
@Afridi I see. but it's not sure. I found the exact problems. thanks your replyMatthew Shin

1 Answers

3
votes

My source has no problem. occurred another side.
Actually HTML side has 'expireDate' input. but sent to the controller NULL value of the input (model 'Shop' didn't define a default value).
It's my mistake.

in the addition, we can use "FORM" without 'th:object' if follow below the controller source.


Controller

@RequestMapping(value = "/edit", method = RequestMethod.GET)
public String edit(Model model) {
    model.addAttribute("data", new Shop());
    return "shop/edit";
}

@RequestMapping(value = "/edit/update", method = RequestMethod.POST)
public String update(@ModelAttribute Shop shop) {
    if (shop.getId() == null) {
        shopService.createShop(shop);
    }

    return "redirect:/";
}