如何从javascript调用struts2操作

how to call struts2 action from javascript?

本文关键字:struts2 操作 调用 javascript      更新时间:2023-09-26

我正在调用Struts2.3dojo autocomplete-onChange,并使用jquery ajax根据autocomplete值更新文本字段。如果数据库中没有autocomplete输入值,那么我想调用一个操作,首先将该项添加到数据库中。

我的javascript代码:-

dojo.event.topic.subscribe("/value", function(value, key, text, widget){
var itemcode=value; 
var data = {itemID:itemcode,itemDes:''};          
$.post("jsondefault/callAJax.action", data, function(data, textStatus) {
    if(data.itemDes.length===0){
    alert("Ihis item is not saved please save it first."); 
    window.location = "/actionName.action";
    }else{
        $('#itdes').val(data.itemDes);
    }
}, "json");
});

jsp代码:-

<div class="pull-left " style="width: 48%;">
<label id="item1">Item Code<br></label>
<s:url id="itemList" action="/jsondefault/createCoupon2" method="getItems" />               
<sx:autocompleter id="itemC" href="%{itemList}" forceValidOption="true" size="24"              
name="item" autoComplete="false" showDownArrow="false" valueNotifyTopics="/value">
</sx:autocompleter>
</div> <div class="pull-right " style="width: 48%;"> 
<label id="itdes1">Item Description</label>
<input type="text" placeholder="Item Description" id="itdes">
</div>

我在javascript 中更改了一些代码行

dojo.event.topic.subscribe("/value", function(value, key, text, widget){
var itemcode=value;
alert(text);    
var data = {itemID:itemcode,itemDes:''};          
$.post("jsondefault/callAJax.action", data, function(data, textStatus) {
    if(data.itemDes.length===0){
        var where_to= confirm("This item is not saved, Do you want to save it?");
         if (where_to== true)
         {
         window.location="addItem.action";
         }
         else
         {
         window.location="createCoupon1.action";
         }
    }else{
        $('#itdes').val(data.itemDes);
    }
}, "json");
});