问题
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