Saturday, 15 March 2014

struts2 - call struts action from jquery - need help on how to make struts call and define struts action -



struts2 - call struts action from jquery - need help on how to make struts call and define struts action -

i new struts need apologize lack of understanding. trying execute struts action update on database using jquery , need guidance on how it. have had crack @ it's not right.

i have jquery dialog in jsp(see below). can see on click of button in dialog phone call function named 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>

then have jquery function named myajaxcall() looks this. telling run ajax function using json.

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)); }});

}

the url variable beingness populated struts action not beingness called. first question datatype sending json. automatically work. , sec question have how define struts action in struts.xml file work. this? homecoming success.

<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>

thanks

if sending info in json format improve utilize post method , serialize form below

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

you can find sample struts2 action class , xml mapping here json

jquery struts2

No comments:

Post a Comment