2

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

Community
  • 1
  • 1
nKognito
  • 6,297
  • 17
  • 77
  • 138

1 Answers1

4

Have a look at the editors under org.springframework.beans.propertyeditors (in the spring-beans jar), which contains the built-in editors provided with spring.

Basically, you need to overwrite setAsText() and getAsText(), which allow you to read the object from a String and write the object as string respectively.

CurrencyEditor could serve as a very simple example. CustomDateEditor could serve as a slightly more complex one. For an array example, have a look at the ClassArrayEditor, for example.


However, The best way to go about this is to use a JSON mapping technology instead of parsing the JSON yourself; have a look at this post.

Community
  • 1
  • 1
Costi Ciudatu
  • 37,042
  • 7
  • 56
  • 92
  • Thanks for your reply. The problem is that if I change ModelAttribute to RequestBody so I can't response ModelAndView object (415 error). Is there any way to get @RequestBody as input param and ModelAndView as output? Thanks – nKognito Jun 24 '11 at 09:34
  • There shouldn't be any problem with reading the input as `@RequestBody` and returning a `@ModelAndView`. Have you tested it ? – Costi Ciudatu Jun 24 '11 at 10:44
  • Thanks for replies. Its ok now, log4j helped to figure out where the problem was. – nKognito Jun 27 '11 at 03:48