春天和阿贾克斯 [英] Spring and Ajax

查看:119
本文介绍了春天和阿贾克斯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用Spring表单标签库与Ajax的结合? 我不能够检索控制器内的表单输入参数。他们总是

Can I use spring form tag library in conjunction with Ajax? I'm not able to retrieve the form input parameters inside the controller. They are always null.

其实,有一个逻辑形式是永远不会提交。但后来我只能发送字符串我的控制器,而不是一个对象作为一个表单情况提交被映射到春的 commandBean

Actually there is a logic that the form is never submitted. But then I can only send strings to my controller and not an object as happens with a form submit that gets mapped to a Spring commandBean.

形式接受 commandBean

<form:form method="POST" commandName="clinicBean">
    Clinic Name: <form:input path="name" type="text" /><br/>
    Clinic Address: <form:input path="address" type="text"/><br/>
    <input type="button" value="Create Clinic" onclick="clinicAjax()"/>
</form:form>

阿贾克斯函数调用在春控制器

function clinicAjax(){
    alert('Inside Clinic Ajax Method');
    $.ajax({
        url: 'clinicAjax',
        type: 'POST',
        success: alert('Ajax Successful')
    });
}

春季控制器的方法:

@RequestMapping(value="clinicAjax",method=RequestMethod.POST)


 public @ResponseBody String createClinic(@ModelAttribute("clinicBean") Clinic clinic){
        System.out.println("Ajax call successful");
        System.out.println(clinic);
        System.out.println(clinic.getName());
        System.out.println(clinic.getAddress());
        return "SUCCESS";
    }

它总是得到的中的System.out.println()语句。

It always gets null in the System.out.println() statements.

推荐答案

现在的问题是,你不序列化表单任何地方,所以它没有被发送到服务器。

The problem is that you're not serializing your form anywhere so it's not being sent to the server.

更改您的javascript code到:

Change your javascript code to:

function clinicAjax(){
    alert('Inside Clinic Ajax Method');
    $.ajax({
        url: 'clinicAjax',
        data: yourFormElement.serialize(); /* i.e. $('#formId').serialize(); */
        type: 'POST',
        success: alert('Ajax Successful')
    });
}

代替 yourFormElement 与jQuery对象重新presenting表单,它应该工作。

substitute yourFormElement with jQuery object representing your form and it should work.

这篇关于春天和阿贾克斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆