0

I have a JSP page with textbox,drop down list and submit button. on-change of first dropdown list, items belonging to second drop down list should be populated dynamically in the dro down list.Using ajax call i'm calling spring controller where i have written logic to populate the list of items in second dropdown list. Requirement is i need to handle the exception in the spring controller and if any exception occurs redirect the entire page to error page which is other jsp page.

javascript function to get dynamic records to populate 2nd dropdown list records on change of 1st dropdownlist.

function showSID(Id)
{
    var xmlHttp;  
    if (window.XMLHttpRequest)
    {
        xmlHttp= new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
    }
    var url = contextPath+"/getAllSID.htm?Id="+Id;
    xmlHttp.onreadystatechange = function() {
        handleServerResponse(xmlHttp);
    };
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);

    function handleServerResponse(xmlHttp)
    {   
       if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
       {

          if (xmlHttp.responseText != "null")
          {
             //handled the response..

                  }         
          }
       }   
    }

spring controller to populate records in 2nd dropdown list on change of first dropdown list:

@RequestMapping(value = "/getAllSID", method = RequestMethod.GET)
public void getAllSIDs(HttpServletRequest request,
            HttpServletResponse response,
            @ModelAttribute SIDDTO dto, BindingResult beException,
            @RequestParam("SelectedList") String selList)
            throws IOException {
       try {
            SIDDTO dynamicData = myService.getSIDDynamicData();//Database call 
            //logic..
            response.setContentType("text");
            response.resetBuffer();
            response.getWriter().print(SID);
        }
      catch (Exception e) 
      {
          LOGGER.error("Exception occured", e);
      }
}

In the above controller, myService.getSIDDynamicData() retrieves data from database, sometimes database may be down or for any other reasons i may get some exceptions.So when some exception occurs i have to redirect to myErrorPage.jsp. I have tried using response.sendRedirect("/myErrorPage.jsp"); but could not able to redirect to errorpage, may be the reason is my page is already loaded and only when i change the dropdown control hits the above controller and as page is already loaded it could not able to redirect to error page. Please suggest how to handle the exceptions in this scenario and redirect to JSP page(error page) whenever error occurs.Thanks.

user3684675
  • 381
  • 4
  • 8
  • 32
  • See also http://stackoverflow.com/questions/2538031/spring-mvc-best-practice-handling-unrecoverable-exceptions-in-controller – Raedwald Jul 29 '14 at 12:21

1 Answers1

0

Consider adding an exception-handler method to your Spring controller. This is a method that has an @ExceptionHandler annotation. In the exception handler you can set the HTTP status to something suitable. For example:

 @ExceptionHandler(value = DataAccessException.class)
 public final void handleException(final DataAccessException exception,
       final HttpServletResponse response) {
    if (exception instanceof RecoverableDataAccessException
          || exception instanceof TransientDataAccessException) {
       response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
       response.setIntHeader("Retry-After", 60);
    } else {
       response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
 }
Raedwald
  • 46,613
  • 43
  • 151
  • 237
  • I tried this approach, but could not able to succeed as the page is already loaded and i'm calling controller only to load drop down list on change of 1st drop down list, its not directing to error page when any exception occurs in the above controller--@Raedwald. – user3684675 Jul 29 '14 at 12:31