java - Spring MVC controller returns null data to the view -


i have controller returns list of users. when users exist, program works fine. however, when user not found, controller returns null jsp page, in case, got blank screen, none of "alert" in jsp page got printed. please let me know why alert("user doesn't exist") cannot printed , how handle situation when controller returns null jsp page.

   @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 code:

    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 not exist, please check");                      }                                  },                                    error: function(jqxhr, status, error)                 {                     alert("error=" + error + "\nstatus=" + status);                 }                            });        }        else // if no name entered search button pressed        {             alert("please enter user name.");        } }  

don't return null. empty http-reponse.

in java code, try return : return "notfound"

and in javacript success try like:

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

Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -