values in multiple tables spring validation prob

不想你离开。 提交于 2019-12-12 03:36:00

问题


I have problems to use multiple database tables what I have:

Input:User (table User) Inputfeld:Email(table User) Input:Bookingnumber(table Bookingnumber)

using only inputfields form "user" works, adding bookingnumber from other table throws this error:

Invalid target for Validator     [shuttleservice.form.validator.UserFormValidator@1588512]:   shuttleservice.form.model.Bookingnumber@18d7616

the problem is simmilar to How insert values to multiple tables using spring and hibernate

and Invalid target for Validator in spring error?

I have just two forms one output, shows just the db data one input with field for several db tables

should I use more than one validator?

// show add user form
@RequestMapping(value = "/users/add", method = RequestMethod.GET)
public String showAddUserForm(Model model) {

    logger.debug("showAddUserForm()");

    User user = new User();
    Bookingnumber bookingnumber = new Bookingnumber();

    // set default value
    user.setName("setNewUserName");     
    user.setEmail("test123@gmail.com");

    bookingnumber.setBookingnumber("123.456");

    model.addAttribute("userForm", user);
    //model.addAttribute("bookingnumberForm", bookingnumber);
    model.addAttribute("userForm", bookingnumber);


    populateDefaultModel(model);
System.out.println("#8");
    return "users/userform";

}

回答1:


Create one model class to wrap all your form fields and pass object of this new wrapper class to your form. Then write a single validator class to validate the fields.

private class FormModel {
    private User user;
    private BookingNumber bookingNumber;

    //Getter Setter ....
}

// show add user form
@RequestMapping(value = "/users/add", method = RequestMethod.GET)
public String showAddUserForm(Model model) {

    logger.debug("showAddUserForm()");
    FormModel formModel = new FormModel ();
    User user = new User();
    Bookingnumber bookingnumber = new Bookingnumber();

    // set default value
    user.setName("setNewUserName");     
    user.setEmail("test123@gmail.com");

    bookingnumber.setBookingnumber("123.456");
    formModel.setUser(user);
    formModel.setBookingNumber(bookingNumber);
    model.addAttribute("formModel", formModel);
    return "users/userform";
}


来源:https://stackoverflow.com/questions/42226011/values-in-multiple-tables-spring-validation-prob

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!