//struts.xml
<package name="ajax" namespace="/" extends="struts-default,json-default">
<action name="CheckUserAction" class="com.controller.ajax.AjaxAction">
<result type="json">
<param name="root">jsonData</param>
</result>
</action>
</package>
//Action Class
public class AjaxAction extends ActionSupport {
private String capacity;
private String greeting;
private List<String> countryList;
private Map<String, Object> jsonData;
public String execute() throws Exception {
jsonData = new LinkedHashMap<String, Object>();
greeting = "HI " + "AMIT";
countryList = new ArrayList<String>();
countryList.add("US");
countryList.add("UK");
countryList.add("Russia");
jsonData.put("capacity", this.getCapacity());
jsonData.put("countryList", countryList);
jsonData.put("countryMap", countryMap);
jsonData.put("greeting", greeting);
return SUCCESS;
}
//Getter and setters
}
//Script
$(document).ready(function() {
$("#capacity").change(function() {
var capacity = $("#capacity").val();
$.getJSON('CheckUserAction.action', {
capacity : capacity
}, function(data) {
$('#abc').append('<br>' + data.capacity);
$('#abc').append('<br>' + data.countryList);
$('#xyz').attr('value', data.greeting);
$('#pqr').attr('list', data.countryList);
});
return false;
});
});
//JSP code
<table border="0">
<tr>
<td><s:label for="capacity" value="Capacity" /></td>
<td><s:textfield name="capacity" placeholder="100" id="capacity"
value="abc"></s:textfield></td>
</tr>
<tr>
<td><s:label for="dynamic" value="Ajax" /></td>
<td><s:textfield name="dynamic" value="learning" id="xyz"></s:textfield></td>
</tr>
<tr>
<td><s:label for="flightId" value="Flights" /></td>
<td><s:select list="#{'1':'1' }" name="fightId" id="pqr"></s:select></td>
</tr>
</table>
<div id="abc">Initial</div>
我想用 json 返回列表 countryList 填充列表(id:pqr),我能够更改文本字段(id:xyz)的属性值,我也将其附加到 div(id:abc)但我无法更改属性struts 列表选择标签。它是否与如何在 HTML 中填充 struts 标记有关。
我也没有使用 struts-jquery 插件或 dojo 插件。谢谢