如何更新jsonResponse[Struts2+;Ajax+;Json]
index.jsp如何更新jsonResponse[Struts2+;Ajax+;Json],json,ajax,struts2,Json,Ajax,Struts2,index.jsp <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <title>AJAX in Struts 2 using JSON and jQuery</title> <script src="js/jquery-1.8.2.js" type="text/javascript"></script> <script> $(doc
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>AJAX in Struts 2 using JSON and jQuery</title>
<script src="js/jquery-1.8.2.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
$('#country').change(function(event) {
var country = $("select#country").val();
delete {countryName : country};
$.getJSON('ajaxAction', {
countryName : country
}, function(jsonResponse) {
$('#ajaxResponse').text(jsonResponse.dummyMsg);
var select = $('#states');
select.find('option').remove();
$.each(jsonResponse.stateMap, function(key, value) {
$('<option>').val(key).text(value).appendTo(select);
});
});
});
});
</script>
</head>
<body>
<h3>AJAX calls to Struts 2 using JSON and jQuery</h3>
<s:select id="country" name="country"
list="{'Select Country','India','US'}" label="Select Country" />
<br />
<br />
<s:select id="states" name="states" list="{'Select State'}"
label="Select State" />
<br />
<br />
<div id="ajaxResponse"></div>
</body>
</html>
在“选择国家”的Web UI中:“选择国家”下拉列表中显示以下国家的“印度”:
Kerala
Tamil Nadu
Jammu Kashmir
Assam
现在我正在更改AjaxJsonAction.java,并添加stateMap.put(“5”,“Odisha”)如下所示并再次运行:
package com.action;
import java.util.LinkedHashMap;
import java.util.Map;
import com.opensymphony.xwork2.Action;
public class AjaxJsonAction implements Action{
private Map<String, String> stateMap = new LinkedHashMap<String, String>();
private String dummyMsg;
//Parameter for Jquery
private String countryName;
public String execute() {
System.out.println(countryName);
if (countryName.equals("India")) {
System.out.println("hello");
stateMap.put("1", "Kerala");
stateMap.put("2", "Tamil Nadu");
stateMap.put("3", "Jammu Kashmir");
stateMap.put("4", "Assam");
stateMap.put("5", "Odisha"); //Newly added State
} else if (countryName.equals("US")) {
System.out.println("hello");
stateMap.put("1", "Georgia");
stateMap.put("2", "Utah");
stateMap.put("3", "Texas");
stateMap.put("4", "New Jersey");
} else if (countryName.equals("Select Country")) {
stateMap.put("1", "Select State");
}
dummyMsg = "Ajax action Triggered";
return SUCCESS;
}
public Map<String, String> getStateMap() {
return stateMap;
}
public String getDummyMsg() {
return dummyMsg;
}
public String getCountryName() {
return countryName;
}
public void setStateMap(Map<String, String> stateMap) {
this.stateMap = stateMap;
}
public void setDummyMsg(String dummyMsg) {
this.dummyMsg = dummyMsg;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
}
为什么没有控制台输出,状态“Odisha”没有被添加,正确的添加方式是什么?
/code>
?@Andrea Ligios:我不太明白你想说什么。类型为json
的结果不能返回任何结果jsp@AndreaLigios当前位置但在我的情况下它确实回来了。如果没有,那么它也不会第一次运行。你能建议我做任何代码转换来进行更新吗?你在使用Eclipse吗?@Andrea Ligios:我不太明白你想说什么。类型为json
的结果不能返回任何结果jsp@AndreaLigios当前位置但在我的情况下它确实回来了。如果没有,那么它也不会第一次运行。你能建议我做任何代码转换来进行更新吗?你在使用Eclipse吗?
India
Hello
Kerala
Tamil Nadu
Jammu Kashmir
Assam
package com.action;
import java.util.LinkedHashMap;
import java.util.Map;
import com.opensymphony.xwork2.Action;
public class AjaxJsonAction implements Action{
private Map<String, String> stateMap = new LinkedHashMap<String, String>();
private String dummyMsg;
//Parameter for Jquery
private String countryName;
public String execute() {
System.out.println(countryName);
if (countryName.equals("India")) {
System.out.println("hello");
stateMap.put("1", "Kerala");
stateMap.put("2", "Tamil Nadu");
stateMap.put("3", "Jammu Kashmir");
stateMap.put("4", "Assam");
stateMap.put("5", "Odisha"); //Newly added State
} else if (countryName.equals("US")) {
System.out.println("hello");
stateMap.put("1", "Georgia");
stateMap.put("2", "Utah");
stateMap.put("3", "Texas");
stateMap.put("4", "New Jersey");
} else if (countryName.equals("Select Country")) {
stateMap.put("1", "Select State");
}
dummyMsg = "Ajax action Triggered";
return SUCCESS;
}
public Map<String, String> getStateMap() {
return stateMap;
}
public String getDummyMsg() {
return dummyMsg;
}
public String getCountryName() {
return countryName;
}
public void setStateMap(Map<String, String> stateMap) {
this.stateMap = stateMap;
}
public void setDummyMsg(String dummyMsg) {
this.dummyMsg = dummyMsg;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
}
Kerala
Tamil Nadu
Jammu Kashmir
Assam