Search code examples
javahttpservermodelandview

"This method must return a result of type ModelAndView." - when returning a result of type ModelAndView


In the LoginView method it shows an error that there must be a return of type ModelAndView. But I am returning a result of type ModelAndView.

@RequestMapping("/login")
public  loginView( HttpServletRequest request, HttpServletResponse res)
{
    String name = request.getParameter("username");
    String password = request.getParameter("password");

    if(password.equals("admin")){
        return new ModelAndView("hipage","","");                
    }
}

Solution

  • I already have one how I fix this

    Only having a return statement doesn't make your method to return something you should specify which type of data your method is going to return (if you want to return something) else void

    So Correct method implementation is below:

    @RequestMapping("/login")
    public ModelAndView loginView( HttpServletRequest request, HttpServletResponse res)
    {
      // Your code logic with the proper return statement.
    }
    

    Note: Once you add the return statement in method declaration you also have to return something from the else block to handle the case when password doesn't meet your expected result.