jQuery 和 Java Servlet:一个从未被检测为空的请求参数,尽管它是这样



我有一个JSP文件,其中jQuery应该向服务器发送一个POST请求,其中包含我在多选框中勾选的元素(类别)。服务器将分析包含所有勾选类别的字符串并返回主题列表,供jQuery对其进行分析并在另一个多选中显示主题。

唯一的问题是,当我勾选没有类别时,jQuery 确实会向服务器发送一个空参数,但服务器永远不会将其检测为 null!

我尝试了一个显示"的警报,但它不起作用!服务器崩溃,显然是与if条件中的代码相关的NullPointerException。

这是我的代码:

 === JSP ===
<script>
                $(document).ready(function ()  
                    {
                        $('#subjectCategories').change(function()
                            {
                                var selectedOptions = $('#subjectCategories option:selected');
                                var selectedValues = $.map(selectedOptions ,function(option) 
                                {
                                    return option.value;
                                }).join(',');
                                $.ajax({
                                       type: "POST",
                                       url: "creation",
                                       data: 'selectedSubjectCategories='+selectedValues,
                                       success: function(data)
                                       {
                                           if (data!=null)
                                            {
                                                   //alert("""+data+""");
                                                   var subjects = data.split(',');
                                                   var select = $('#subjectslist');
                                                    $('#subjectslist').children().remove();
                                                   $.each(subjects, function(key, subject) 
                                                   {
                                                       if (select.find('option[value="' + subject + '"]').length === 0 && subject!="") 
                                                       {
                                                             //Ajouter la nouvelle catégorie dans la liste
                                                               $('<option>', {
                                                                 value: subject,
                                                                 text: subject
                                                                 }).appendTo(select);
                                                       }
                                                   });
                                            }
                                           else
                                            {
                                                $('#subjectslist').children().remove();
                                            }
                                       }
                                     });
                            }
                        );
                    }).change();
              </script>

=== SERVLET ===

            if (request.getParameter("selectedSubjectCategories")!=null)
        {
            String[] categoryList = request.getParameter("selectedSubjectCategories").split(",");
            ArrayList<String> pourDoublons = new ArrayList<String>();
            String subjectsToShow = new String();
            for (int i=0; i<categoryList.length; i++)
            {
                if (categorySubjectsHashMap.containsKey(categoryList[i]))
                {
                    ArrayList<String> subjectsOfCategory = categorySubjectsHashMap.get(categoryList[i]);
                    for (int j=0; j<subjectsOfCategory.size(); j++)
                    {
                        if (!pourDoublons.contains(subjectsOfCategory.get(j)))
                        {
                            subjectsToShow += subjectsOfCategory.get(j)+",";
                            pourDoublons.add(subjectsOfCategory.get(j));
                        }
                    }
                }
            }
            categoryList = null;
            pourDoublons = null;
            response.setContentType("text/plain");  
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(subjectsToShow.substring(0,subjectsToShow.length()-1));
        }
        else
        {
            response.setContentType("text/plain");  
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write("");
        }

为什么我的参数无法检测为空?我也尝试了 .equals(null) 和很多类似的东西,但没有任何效果!:(

提前谢谢。

request.getParameter(name) 只有在根本不发送参数时才返回null。发送参数时,它将返回一个空字符串,但没有值。这样可以更轻松地区分已发送的参数和未发送的参数。

如果你也想检查空虚,你基本上需要使用String#isEmpty()检查。

所以,你需要更换

if (request.getParameter("selectedSubjectCategories")!=null)

String selectedSubjectCategories = request.getParameter("selectedSubjectCategories");
if (selectedSubjectCategories != null && !selectedSubjectCategories.isEmpty())

或者也许

if (selectedSubjectCategories != null && !selectedSubjectCategories.trim().isEmpty())

请注意,charlieftl评论中建议的string != ""检查绝对不是检查空字符串的正确方法。!=(和==)通过引用而不是按值比较对象。

if (request.getParameter("selectedSubjectCategories")!='')

工作正常。:)

最新更新