这是我关于stackoverflow的第一篇文章,所以我希望我不会犯太多错误。
我在填写Java List并将其发送到aui时遇到问题:select as options。我的目标是在更改其中一个选项时动态填写 aui:select 。例如:如果您更改第一个选择项目(县),则清空第二个和第三个项目(分别为社区和城市),然后填充依赖于所选县的数据。
我得出的结论是,只要查询字符串参数中有“mvcPath”参数,此代码就会从整个页面中复制代码。只要没有“mvcPath”,此代码就可以正常运行。遗憾的是,我需要此参数来更改页面(从搜索结果到选定的结果详细信息)。
Image showing badly filled select item
Image showing correctly filled select item
Java代码:
@Override
public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse)
throws IOException, PortletException {
String communityName = "";
long communityId = 0;
String cityName = "";
long cityId = 0;
String countySelected = ParamUtil.getString(resourceRequest, "countySelected");
long countySelectedId = ParamUtil.getLong(resourceRequest, "countyDictionaryId");
String communitySelected = ParamUtil.getString(resourceRequest, "communitySelected");
long communitySelectedId = ParamUtil.getLong(resourceRequest, "communityDictionaryId");
JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
if (countySelected.equalsIgnoreCase("countySelected") && countySelectedId != 0) {
System.out.println("County id: " + countySelectedId);
try {
int communitiesCount = CommunityDictionaryLocalServiceUtil.getCommunityDictionariesCount();
List<CommunityDictionary> communities = CommunityDictionaryLocalServiceUtil.getCommunityDictionaries(0,
communitiesCount);
for (CommunityDictionary community : communities) {
if (community.getCountyDictionaryId() == countySelectedId) {
communityName = community.getCommunityName();
communityId = community.getCommunityDictionaryId();
JSONObject communityObject = JSONFactoryUtil.createJSONObject();
communityObject.put("communityName", communityName);
communityObject.put("communityDictionaryId", communityId);
jsonArray.put(communityObject);
System.out.print(jsonArray.toString());
}
}
} catch (SystemException e) {
e.printStackTrace();
}
} else if (countySelected.equalsIgnoreCase("countySelected") && countySelectedId == 0) {
System.out.println("No county chosen.");
try {
int communitiesCount = CommunityDictionaryLocalServiceUtil.getCommunityDictionariesCount();
List<CommunityDictionary> communities = CommunityDictionaryLocalServiceUtil.getCommunityDictionaries(0,
communitiesCount);
for (CommunityDictionary community : communities) {
communityName = community.getCommunityName();
communityId = community.getCommunityDictionaryId();
JSONObject communityObject = JSONFactoryUtil.createJSONObject();
communityObject.put("communityName", communityName);
communityObject.put("communityDictionaryId", communityId);
jsonArray.put(communityObject);
}
} catch (SystemException e) {
e.printStackTrace();
}
}
if (communitySelected.equalsIgnoreCase("communitySelected") && communitySelectedId != 0) {
System.out.println("Community id: " + communitySelectedId);
try {
int citiesCount = CityDictionaryLocalServiceUtil.getCityDictionariesCount();
List<CityDictionary> cities = CityDictionaryLocalServiceUtil.getCityDictionaries(0, citiesCount);
for (CityDictionary city : cities) {
if (city.getCommunityDictionaryId() == communitySelectedId) {
cityName = city.getCityName();
cityId = city.getCityDictionaryId();
JSONObject cityObject = JSONFactoryUtil.createJSONObject();
cityObject.put("cityName", cityName);
cityObject.put("cityDictionaryId", cityId);
jsonArray.put(cityObject);
System.out.print(jsonArray.toString());
}
}
} catch (SystemException e) {
e.printStackTrace();
}
} else if (communitySelected.equalsIgnoreCase("communitySelected") && communitySelectedId == 0) {
System.out.println("No community chosen.");
try {
int citiesCount = CityDictionaryLocalServiceUtil.getCityDictionariesCount();
List<CityDictionary> cities = CityDictionaryLocalServiceUtil.getCityDictionaries(0, citiesCount);
for (CityDictionary city : cities) {
cityName = city.getCityName();
cityId = city.getCityDictionaryId();
JSONObject cityObject = JSONFactoryUtil.createJSONObject();
cityObject.put("cityName", cityName);
cityObject.put("cityDictionaryId", cityId);
jsonArray.put(cityObject);
}
} catch (SystemException e) {
e.printStackTrace();
}
}
PrintWriter writer = resourceResponse.getWriter();
writer.write(jsonArray.toString());
writer.flush();
super.serveResource(resourceRequest, resourceResponse);
}
脚本:
<aui:script>
AUI().use('aui-base', 'aui-io-request', 'aui-node',
function(A) {A.one("#<portlet:namespace />countySelect").on('change', function() {
A.io.request('<%= selectionChangedURL %>',
{
method : 'POST',
data : {
"<portlet:namespace />countyDictionaryId" : A.one("#<portlet:namespace />countySelect").val(),
'<portlet:namespace />countySelected' : 'countySelected'
},
dataType : 'json',
on : {
success : function() {
var communitiesList = this.get('responseData');
A.one('#<portlet:namespace />communitySelect').empty();
A.one('#<portlet:namespace />citySelect').empty();
A.one('#<portlet:namespace />communitySelect').prepend("<option value='0'> </option>");
for (var i in communitiesList) {
console.info(communitiesList[i]);
A.one('#<portlet:namespace />communitySelect').append("<option value='" + communitiesList[i].communityDictionaryId + "'>" + communitiesList[i].communityName + "</option>");
}
}
}
});
});
A.one("#<portlet:namespace />communitySelect").on('change', function() {
A.io.request('<%= selectionChangedURL %>',
{
method : 'POST',
data : {
"<portlet:namespace />communityDictionaryId" : A.one("#<portlet:namespace />communitySelect").val(),
'<portlet:namespace />communitySelected' : 'communitySelected'
},
dataType : 'json',
on : {
success : function() {
var citiesList = this.get('responseData');
A.one('#<portlet:namespace />citySelect').empty();
A.one('#<portlet:namespace />citySelect').prepend("<option value='0'> </option>");
for (var i in citiesList) {
console.info(citiesList[i]);
A.one('#<portlet:namespace />citySelect').append("<option value='" + citiesList[i].cityDictionaryId + "'>" + citiesList[i].cityName + "</option>");
}
}
}
});
});
});
答案 0 :(得分:0)
在服务器端代码发生一些变化后,我设法解决了这个问题。我不确定那些小改变是如何帮助的,但我很高兴关闭这个帖子。
这是服务器端代码:
public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse)
throws IOException, PortletException {
String communityName = "";
long communityId = 0;
String cityName = "";
long cityId = 0;
String countySelected = ParamUtil.getString(resourceRequest, "countySelected");
long countySelectedId = ParamUtil.getLong(resourceRequest, "countyDictionaryId");
String communitySelected = ParamUtil.getString(resourceRequest, "communitySelected");
long communitySelectedId = ParamUtil.getLong(resourceRequest, "communityDictionaryId");
JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
if (countySelected.equalsIgnoreCase("countySelected")) {
try {
int communitiesCount = CommunityDictionaryLocalServiceUtil.getCommunityDictionariesCount();
List<CommunityDictionary> communities = CommunityDictionaryLocalServiceUtil.getCommunityDictionaries(0,
communitiesCount);
if (countySelectedId == 0) {
for (CommunityDictionary community : communities) {
communityName = community.getCommunityName();
communityId = community.getCommunityDictionaryId();
JSONObject communityObject = JSONFactoryUtil.createJSONObject();
communityObject.put("communityName", communityName);
communityObject.put("communityDictionaryId", communityId);
jsonArray.put(communityObject);
}
} else {
for (CommunityDictionary community : communities) {
if (community.getCountyDictionaryId() == countySelectedId) {
communityName = community.getCommunityName();
communityId = community.getCommunityDictionaryId();
JSONObject communityObject = JSONFactoryUtil.createJSONObject();
communityObject.put("communityName", communityName);
communityObject.put("communityDictionaryId", communityId);
jsonArray.put(communityObject);
}
}
}
} catch (SystemException e) {
e.printStackTrace();
}
}
if (communitySelected.equalsIgnoreCase("communitySelected")) {
try {
int citiesCount = CityDictionaryLocalServiceUtil.getCityDictionariesCount();
List<CityDictionary> cities = CityDictionaryLocalServiceUtil.getCityDictionaries(0, citiesCount);
if (communitySelectedId == 0) {
for (CityDictionary city : cities) {
cityName = city.getCityName();
cityId = city.getCityDictionaryId();
JSONObject cityObject = JSONFactoryUtil.createJSONObject();
cityObject.put("cityName", cityName);
cityObject.put("cityDictionaryId", cityId);
jsonArray.put(cityObject);
}
} else {
for (CityDictionary city : cities) {
if (city.getCommunityDictionaryId() == communitySelectedId) {
cityName = city.getCityName();
cityId = city.getCityDictionaryId();
JSONObject cityObject = JSONFactoryUtil.createJSONObject();
cityObject.put("cityName", cityName);
cityObject.put("cityDictionaryId", cityId);
jsonArray.put(cityObject);
}
}
}
} catch (SystemException e) {
e.printStackTrace();
}
}
PrintWriter writer = new PrintWriter(resourceResponse.getPortletOutputStream());
writer.write(jsonArray.toString());
writer.flush();
super.serveResource(resourceRequest, resourceResponse);
}