我的目标是通过调用我的strus2操作更改selectbox值来显示客户信息。
我的问题是:我应该从我的动作类返回什么来获取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>
动作类
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 :(得分: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的显示。
答案 1 :(得分: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
}
});
});
});
&#13;
我做了同样的事,它对我有用。 我希望这也适合你。