开发散记(ModelAndView配置返回视图无效)

1、When arguments passed in a method, you best to verify these arguments;
2、使用SpringMVC开发时,发现通过ModelAndView配置返回视图无效,代码如下

   @RequestMapping("/loginCheck.html")
    public ModelAndView loginCheck(HttpServletRequest request, LoginCommand loginCommand){
        boolean isValidUser = userService.hasMatchUser(loginCommand.getUserName(), loginCommand.getPassword());
        if (!isValidUser){
            return new ModelAndView("login", "error", "用户名密码错误");
        } else {
            User user = userService.findUserByUserName(loginCommand.getUserName());
            user.setLastIp(request.getRemoteAddr());
            user.setLastVisit(new Date());
            userService.loginSuccess(user);
            request.getSession().setAttribute("user", user);
            return new ModelAndView("main");
        }
    }

本意将loginCheck.html请求跳转到main.jsp页面,但是在实际测试中跳转到loginCheck.jsp页面,经查询,发现是由于ModelAndView引用错误导致,应该引用org.springframework.web.servlet.ModelAndView类,实际引用了import org.springframework.web.portlet.ModelAndView,将引用修改正确即可

你可能感兴趣的:(开发散记(ModelAndView配置返回视图无效))