I am trying to send custom javascript object (actually, its array of JSON objects) via jquery post request to spring controller. The server-side fails with "bla-bla no matching editors or conversion strategy found" error. It can't convert from string to server-side object. I found this and this examples but I can't put it together to make it work. Which methods of PropertyEditor should I override? Does anybody knows link to any example? Thank you.
Controller:
private class AddressFormEditor extends PropertyEditorSupport {
protected Object convertElement(Object element)
{
String form = "";
if (element instanceof String)
form = (String) element;
Logger logger = Logger.getLogger(getClass().getName());
logger.warning(form);
return form != null ? new AddressForm(form) : null;
}
}
@InitBinder
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception
{
binder.registerCustomEditor(AddressForm.class, new AddressFormEditor());
}
@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView saveUser(@ModelAttribute UserForm form, HttpServletResponse response) {...}
UserForm:
public class UserForm extends BaseForm {
private Long id;
private String login;
private String password;
private String confirmPassword;
private String type;
private String franchise;
private String roles;
private String firstName;
private String lastName;
private String salutation;
private String position;
private String birthDate;
private String passportNumber;
private String passportIssueBy;
private String passportIssueDate;
private String passportExpireDate;
private String nationality;
private AddressForm[] addresses;
private String notes; ... }
jQuery request:
var data = $('#userForm').serializeObject();
data.addresses = addresses;
$.ajax({
traditional: true,
type: 'POST',
url: url + 'save',
data: data,
success: function(response) {
if (response) {
initializeUser(response);
hideWait();
}
else {
showUsers();
$('#closeUserBtn').click();
}
},
error: function() {
error();
hideWait();
}
});
Logger writes that addresses is '[object Object]' but actually the valid array was sent to server