2013-07-03 37 views
1

我的目标是通过调用选择框值的我strus2动作变化来显示由负荷消费ID的客户信息。如何在jQuery函数从动作类返回JSON结果

我的问题是:我应该从我的动作类返回得到JSON格式

我尝试下面的代码的价值,但我不知道什么是错的

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

选择框:

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

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

    } 
+0

你会得到什么错误? – mohkhan

+0

@mohkhan问题在这里'url:'showCustomerInfoById.action''我删除了.action函数正在调用。 –

+0

@mohkhan我更新了我的问题,请再阅读一次。 –

回答

1

尝试这种情况:

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

提示:去除属性:方法= “CustomerdetailsById”

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

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

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

您也可以拨打showCustomerInfoById.action在浏览器中,你应该看到JSON显示在浏览器中。

+0

我有一个新的疑问,我想用jQuery的Struts2的autocompleter而不是选择框,那么我将如何定义我的jQuery功能?请在你的问题中更新这个 –

+0

@Danny这很容易,你正在使用'struts2-jquery-plugin',查看这个例子https://code.google。com/p/struts2-jquery/wiki/SelectTag – Jaiwo99

+0

正如你所说,我遵循你的链接,并且在上面的例子中实现了struts2 autocompleter而不是'

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

我做了同样它为我工作。 我希望这也适合你。