1
我想做的事與Spring MVC + MyBatis的登錄頁面,則控制器如下:Spring MVC的重定向失敗
@Controller
public class LoginController {
private final UserService userService;
@Autowired
public LoginController(@Qualifier("userService") UserService userService) {
this.userService = userService;
}
@RequestMapping(value = "/login")
public ModelAndView Login(String username, String password,
ModelAndView mv, HttpSession session) {
User user = userService.login(username, password);
// **This is where problems occured**
if (user != null) {
session.setAttribute("user", user);
mv.setView(new RedirectView("/main"));
} else {
mv.addObject("message","Login failed.");
mv.setViewName("loginForm");
}
return mv; // with correct value, but responses 404 error
}
@RequestMapping(value = "/loginForm")
public String LoginForm() {
return "loginForm";
}
@RequestMapping(value = "/main")
public String Main(Model model) {
return "main";
}
}
我有兩個JSP文件:
/WEB-INF/jsp/loginForm.jsp
/WEB-INF/jsp/main.jsp
我是否提交正確或不登錄形式,有一個404錯誤:
HTTP Status 404 - /WEB-INF/jsp/login.jsp
在調試模式下運行時,MV與「/ main」正確返回,所以令我困惑。
下面的代碼工作,但我不能添加錯誤消息來查看了。
@RequestMapping(value = "/login")
public String Login(String username, String password, HttpSession session) {
User user = userService.login(username, password);
if (user != null) {
session.setAttribute("user", user);
return "redirect:/main";
} else {
session.setAttribute("message","Login failed");
return "redirect:/loginForm";
}
}
你用什麼樣的Spring配置,XML或Java基於我的幫助? – NotionBit
解決了,謝謝,原因是_null_參數。 – sczhg