【问题标题】:Spring MVC controller returns null data to the viewSpring MVC 控制器向视图返回空数据
【发布时间】:2015-04-23 22:22:05
【问题描述】:

我有一个返回用户列表的控制器。当用户存在时,程序运行良好。但是,当找不到用户时,控制器将 null 返回到 jsp 页面,在这种情况下,我得到了一个空白屏幕,没有打印 jsp 页面中的“警报”。请告诉我为什么无法打印警报(“用户不存在”)以及当控制器将 null 返回到 jsp 页面时我如何处理这种情况。

   @RequestMapping(value = "/userSearch",
                   method = RequestMethod.POST,
                   produces = "application/json")
   @ResponseBody
   public UserList search @RequestParam String username) 
   {     
          UserList userListObj = search(username); // search database

          if (userListObj.getUserList().size() > 0) 
          {
              return userListObj;
          } 
          else 
          {
              return null;
          }
     }

JSP 代码:

    function ajaxUserSearch() 
    {   
         var uname = $("#userName").val();
         if ( uname )
         {              
              $.ajax({
                type: "POST",
                url: "userSearch.htm",
                data: "username=" + uname ,
                dataType: "json",
                success: function(data) 
                {   
                     alert("data=" + data);
                     if ( data!=null )
                     {      
                          alert("data not null");
                     } 
                     else 
                     {            
                         alert(" user does not exist, please check");
                     }                 
                },                   
                error: function(jqXHR, status, error)
                {
                    alert("error=" + error + "\nstatus=" + status);
                }              
             });
       }
       else // If no name is entered but Search button is pressed
       {
            alert("Please enter a user name.");
       }
} 

【问题讨论】:

    标签: java spring jsp spring-mvc


    【解决方案1】:

    不要返回空值。它基本上是一个空的 http 响应。

    在 Java 代码中,尝试返回类似:return "notfound"

    然后在 Javacript success 中尝试执行以下操作:

    if (data != null) {
       if (data === "notfound") {
         alert("user not found"); 
       } else {
          alert("user found" + data);
       }
    } else {
       alert("unexpected error when finding user. please try again"); 
    }
    

    【讨论】:

      【解决方案2】:

      实际上甚至不需要检查控制器中userListObj 的大小...只需将对象返回到页面即可:

      @ResponseBody
      public UserList search @RequestParam String username) {     
            return search(username); // search database
      }
      

      然后在你的ajax成功函数中,解析json并检查UserList的长度:

      success: function(data) {   
          var userListObj = $.parseJSON(data);
          if(userListObj.UserList.length > 0) {      
              alert("data not null");
          } else {
              alert("user does not exist, please check");
          }
      

      【讨论】:

        【解决方案3】:

        我通过禁用客户端输入字段上的回车键解决了我自己的问题。

          $("form").bind("keypress", function (event) 
                    {
                        if (event.keyCode == 13) 
                        {
                            event.preventDefault();
                            return false;
                        }
                    });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多