"This method must return a result of type ModelAndView." - 返回模型和视图类型的结果时



LoginView方法中,它显示一个错误,即必须返回类型为ModelAndView的错误。但是我返回的是类型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","","");                
}
}

我已经有一个如何解决这个问题

只有return statement并不能使你的方法返回一些东西,你应该指定你的方法将返回的数据类型(如果你想返回一些东西)否则void

所以正确的方法实现如下:

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

注意:在方法声明中添加 return 语句后,您还必须从else块返回一些内容,以处理密码不符合预期结果的情况。

您的返回语句位于 if 中。

尝试在公开后添加返回类型 [...] 方法名称

如果是这种情况,还会从您那里返回。

public Object method(boolean test) { 
if (!test) {
return test;
} 
return null;
}

最新更新