1

My aim is to display customer info by costumer id by calling my strus2 action change of selectbox value.

My problem is: what should i return from my action class to get the value in json format

I tried the following code, but i don't know what is wrong with it

    <script type="text/javascript">  
  $(function()
   {    alert("This function is calling, on change event of selectbox and customer id="+$(this).val());
     $("#customersjsonlstid").change(function(e)
         { 
                    $.ajax({
                        url: 'showCustomerInfoById.action',  //action name
                        data: "customerid=" + $(this).val(),    
                        dataType: "json",
                        success: function(data) {    

                            // Set the inputs from the json object , json output : {"price": "123.40", "distributor": "XYZ Inc."}          
                            $('#autocompleter_div_result1').val(data.distributor); //showing data on particular div
                            $('#autocompleter_div_result2').val(data.price);  //showing data on particular div
                        }
                    });
                });
    });             
</script>

Selectbox:

                            <sj:select 
                                id="customersjsonlstid" 
                                name="editionType" 
                                href="%{customerJsonselecturl}" 
                                list="lstcust"
                                listValue="name" 
                                listKey="id"   
                                autocomplete="false"
                            />     
             <div id="autocompleter_div_result">`

struts.xml

        <action name="showCustomerInfoById" class="v.esoft.actions.customerdetails.CustomerSelectAction" method="CustomerdetailsById">
             <result name="success" type="json"/> 
        </action> 

Action class

    public class CustomerSelectAction extends ActionSupport {

     private String  customerid;


//---------------------------------------------------------
   public String CustomerdetailsById()
    {
        System.out.print("--------Method called, customer id-----------"+customerid);
        return customerid;
    }
     //---------------------------------------------------------


public String getCustomerid() {
    return customerid;
}


public void setCustomerid(String customerid) {
    this.customerid = customerid;
}

  }
4

2 回答 2

1

尝试这个:

<action name="showCustomerInfoById" 
        class="v.esoft.actions.customerdetails.CustomerSelectAction">
             <result name="success" type="json"/> 
</action>

提示:已删除属性:method = "CustomerdetailsById"

@Override
public String execute() {
    return SUCCESS;
}

在这种情况下,你会得到一个像这样的 json 对象:

{
    "customerId":"value(customerId)"
}

您也可以在浏览器中调用 showCustomerInfoById.action,您应该会在浏览器中看到 json 显示。

于 2013-07-03T09:23:30.440 回答
1

我遇到了同样的问题,终于解决了。你可以这样尝试:

$("#customersjsonlstid").change(function(e)
         { 
         var   customeridJson =    $('#customersjsonlstid').val();
                    $.ajax({
                        url: 'showCustomerInfoById',  //action name   same name in struts.xml
                        data: "customerid=" + customeridJson,    
                        dataType: "json",
                        async: true,
                        success: function(data) {    

                            // Set the inputs from the json object , json output : {"price": "123.40", "distributor": "XYZ Inc."}          
                            $('#autocompleter_div_result1').val(data.distributor); //showing data on particular div
                            $('#autocompleter_div_result2').val(data.price);  //showing data on particular div
                        }
                    });
                });
    }); 

我做了同样的事情,它对我有用。我希望这也适用于你。

于 2014-11-11T15:16:28.187 回答