自從最近幾天以來,我已經嘗試了很多,但仍無法解決此問題。Liferay portlet + struts2 + Ajax
我正在使用liferay 6.1和struts 2.基本上我的liferay portlet中有兩個下拉列表,一個是國家和其他州的。選擇國家時,州列表需要根據所選國家在下拉列表中。我試圖通過AJAX來做到這一點。 (下面的代碼,我從谷歌發現,其工作在一個單獨的動態Web項目,但是當我試圖在此portlet的Liferay的投擲的錯誤
下面是我的代碼:
StartPage.jsp
<script >
$(document).ready(function() {
$('#country').change(function(event) {
var country = $("select#country").val();
alert(country);
$.getJSON("<s:url action='ajaxAction' namespace='ajax' includeParams='none' />", {countryName : country}, function(jsonResponse) {
$('#ajaxResponse').text(jsonResponse.dummyMsg);
alert($('#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>
<s:form name="StartPage" id="StartPage">
<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>
</s:form>
struts.xml的
<package name="default" extends="json-default">
<action name="ajaxAction" class="com.action.AjaxJsonAction">
<result type="json">/WEB-INF/view/StartPage.jsp
</result>
</action>
</package>
Action類:
public class AjaxJsonAction extends DefaultActionSupport{
private Map<String, String> stateMap = new LinkedHashMap<String, String>();
private String dummyMsg;
//Parameter for Jquery
private String countryName;
@Override
public String execute() {
System.out.println("i am executed...");
System.out.println("CountryName: " + countryName);
if (countryName.equals("India")) {
stateMap.put("1", "Kerala");
stateMap.put("2", "Tamil Nadu");
stateMap.put("3", "Jammu Kashmir");
stateMap.put("4", "Assam");
} else if (countryName.equals("US")) {
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";
System.out.println("exiting.....");
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;
}
}
Errro登錄:
java.lang.IllegalArgumentException: application/json;charset=UTF-8 is not a supported mime type
at com.liferay.portlet.MimeResponseImpl.setContentType(MimeResponseImpl.java:159)
at org.apache.struts2.portlet.servlet.PortletServletResponse.setContentType(PortletServletResponse.java:219)
at org.apache.struts2.json.JSONUtil.writeJSONToResponse(JSONUtil.java:225)
at org.apache.struts2.json.JSONResult.writeToResponse(JSONResult.java:211)
at org.apache.struts2.json.JSONResult.execute(JSONResult.java:172)
at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:373)