spring模型不会自动填充json数据

时间:2014-01-14 07:37:28

标签: java jquery json spring spring-mvc

我编写了一些代码来将数据从客户端发送到spring模型。并希望json数据应该自动填充java类。但不能这样做。

Jquery看起来像这样

$('#login-form').submit(function(event){
        var username = $('#id_username').val();  
        var password = $('#id_password').val();
        alert(username + password)
        var json = { "loginId" : username, "password" : password};

      $.ajax({  
        type: "POST",  
        url: $("#login-form").attr("action"),
        data: JSON.stringify(json),
        beforeSend: function(xhr){
            var mess = validateForm();
            if(mess.length != 0){
                $('#error-mes').show();
                $('#error-mes').html(mess);
                event.preventDefault();
                return false;
            }

            xhr.setRequestHeader("Accept", "application/json");  
            xhr.setRequestHeader("Content-Type", "application/json");
            return true;
        },
        success: function(response){
             $('#error-mes').html(response);
             $('#error-mes').show();
        },  
        error: function(e){  
          alert('Error: ' + e);  
        }  
      });  
      event.preventDefault();
      $('#userName').hide();
      $('#spn_password').hide();
});

像这样的Spring代码

@RequestMapping(value = "/signin", method = RequestMethod.POST )
    public @ResponseBody String submitCustSignInForm(HttpServletRequest request, @ModelAttribute("model") Person model,
            HttpSession sess) {

        String response = "";
        Person person = null;

        if (sess.getAttribute("USER_INFO") == null) {
            person = tsService.login(model);
            if (person == null) {
                response = "User name or password does not match.";
            } else {
                response = "success";
                sess.setAttribute("USER_INFO", person);
            }
        }
        return response;
    }

Person模型在类中具有相同的loginId和password属性以及setter和getter。

某人可以告诉我它是如何实现的。

0 个答案:

没有答案