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

  }
Dan
  • 2,086
  • 11
  • 71
  • 137
  • what error do you get ? – mohkhan Jul 03 '13 at 08:55
  • @mohkhan problem was here ` url: 'showCustomerInfoById.action'` i removed the .action now function is calling. – Dan Jul 03 '13 at 09:02
  • @mohkhan i updated my question, please read it once more. – Dan Jul 03 '13 at 09:04
  • http://stackoverflow.com/a/17149414/1654265 – Andrea Ligios Jul 03 '13 at 09:58
  • Post the `struts.xml`. – Roman C Jul 03 '13 at 10:21
  • @AndreaLigios The above action is calling on selectbox change event, if i would like to call it for autocompleter jquery function, then how to do? Please help me with this http://stackoverflow.com/questions/17104310/unable-to-get-id-from-the-struts2-auto-completer-on-change-event – Dan Jul 03 '13 at 14:23
  • @RomanC i want to call the above action on struts2 jquery autocompleter how to do this? http://stackoverflow.com/questions/17104310/unable-to-get-id-from-the-struts2-auto-completer-on-change-event – Dan Jul 03 '13 at 14:24

2 Answers2

1

Try this:

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

Hint: removed attribute: method = "CustomerdetailsById"

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

In this case, you will get a json object like this:

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

You can also call showCustomerInfoById.action in your browser, you should see the json shows in the browser.

Jaiwo99
  • 9,687
  • 3
  • 36
  • 53
  • I have a new doubt, i would like to use struts2 jquery autocompleter instead of selectbox, then how i will define my jquery function? Please update this also in your question – Dan Jul 03 '13 at 13:11
  • @Danny it is very easy, you are using `struts2-jquery-plugin`, just check out this example https://code.google.com/p/struts2-jquery/wiki/SelectTag – Jaiwo99 Jul 03 '13 at 19:11
  • As you said, i followed your link, and implemented struts2 autocompleter in above example instead of ` – Dan Jul 09 '13 at 12:08
  • @Danny it really hard to understand in this way, you may create a new question with this, wir should try to help you – Jaiwo99 Jul 09 '13 at 15:53
  • here http://stackoverflow.com/questions/17104310/unable-to-get-id-from-the-struts2-auto-completer-on-change-event – Dan Jul 09 '13 at 18:28
1

I faced the same issue and finally it got resolved. you can try like this :

$("#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
                        }
                    });
                });
    }); 

I did the same and it worked for me. I hope this works for you as well.

Tushar
  • 1,450
  • 6
  • 18
  • 30