从jquery调用struts动作 - 需要有关如何调用struts和定义struts动作的帮助

时间:2014-06-23 07:28:19

标签: jquery struts2

我对struts相当新,所以我需要为我缺乏理解而道歉。我正在尝试执行struts操作,使用jquery对我的数据库进行更新,并需要一些指导如何执行此操作。我有一个裂缝,但它不对。

我的jsp中有一个jquery对话框(见下文)。您可以看到,在单击此对话框中的按钮时,我调用了一个名为myAjaxCall()的函数...

<s:div id="dialog-form" title="Update" style="display:none">
<s:form id="dform" enctype="multipart/form-data" >
    <s:textfield  name="strid" id="strid" label="Store" readonly="true"  />
    <s:textfield  name="strName" id="strName" label="Store Name" readonly="true" />
    <s:textfield  name="businessDt" id="businessDt" label="Business Date" readonly="true"  />
    <s:textfield  name="openingtime" id="openingtime"  label="Opening time(EST)" />
    <s:textfield  name="closingtime" id="closingtime"  label="Closing time(EST)" />
    <s:textfield  name="reason" id="reason" label="Reason" />
    <s:url id="ajaxDialog" value="/storeCurrentStatusDialog.action"/>
    <table style="border:0"  >
     <tr>
      <td align="center"><input type="button" value="Update" onclick="myAjaxCall()"  ><br/></td>
     </tr>
    </table>
 </s:form>
</s:div>

然后我有一个名为myAjaxCall()的jquery函数,看起来像这样。我告诉它使用json运行ajax函数。

function myAjaxCall() {
    var url="${pageContext.request.contextPath}/storeCurrentStatusDialog.action?strid="+document.dform.strid.value+"&closingtime="+document.dform.closingtime.value+"&openingtime="+document.dform.openingtime.value+"&businessDt="+document.dform.businessDt.value+"&reason="+document.dform.reason.value;

$.ajax({
    type: 'GET',
    url: url,
    dataType: 'json',
    success: function(data){
        console.log(stringify(data));
    }});

}

正在正确填充URL变量,但我的struts操作未被调用。我的第一个问题是我发送的dataType是json。这将自动工作。我的第二个问题是如何在struts.xml文件中定义我的struts操作以使其工作。像这样的东西?这将使成功回归。

    <action name="storeCurrentStatusDialog"
        class="com.mycompany.eposweb.action.StoreCurrentStatusAction"
        method="update">
        <result name="success" type="stream">
            <param name="contentType">text/html</param>
            <param name="inputName">inputStream</param>
        </result>
    </action>

感谢

1 个答案:

答案 0 :(得分:1)

如果您以json格式发送数据,请更好地使用POST方法并序列化您的表单,如下所示

$.ajax({
    type: 'POST',
    url: 'storeCurrentStatusDialog.action',
    dataType:'json',
    data:$('#dform').serialize(),
    contentType: "application/json; charset=utf-8",
    success: function(data){
        console.log(stringify(data));
    }});

您可以为json

找到示例Struts2操作类和XML映射HERE
相关问题