如何从javascript调用struts2动作?

时间:2013-02-27 06:31:38

标签: javascript struts2 dojo struts2-json-plugin

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

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

1 个答案:

答案 0 :(得分:1)

我改变了我的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");
});