有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

javascript Liferay选择项填充json responseData

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

我在填写Java列表并将其发送到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>");
                                }
                            }
                    }
                    });
            });
        });


共 (1) 个答案

  1. # 1 楼答案

    在服务器端代码中做了一些更改之后,我设法解决了这个问题。我真的不确定这些小的改变是如何起作用的,但我很高兴结束这篇文章

    这是服务器端代码:

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