Liferay - 选择填充了 json 响应数据的项目



这是我在stackoverflow上的第一篇文章,所以我希望我不会犯太多错误。

我在填写 Java 列表并将其发送到 aui:select 作为选项时遇到问题。我的目标是在他们的一个选项发生变化时动态填充 aui:select。例如:如果更改第一个选择项目(县),则第二个和第三个项目(分别为社区和城市)将被清空,然后填充依赖于所选县的数据。

我得出的结论是,每当查询字符串参数中存在"mvcPath"参数时,该代码基本上会从整个页面复制代码。但是,只要没有"mvcPath",此代码就可以很好地工作。可悲的是,我需要这个参数来更改页面(从搜索结果到选定的结果详细信息)。

显示未填充的选定项目的图像

显示正确填充的选择项的图像

爪哇代码:

    @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>");
                                }
                            }
                    }
                    });
            });
        });

在服务器端代码进行一些更改后,我设法解决了这个问题。我不太确定这些小更改是如何帮助的,但我很高兴关闭这个线程。

这是服务器端代码:

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);
}

最新更新