我的问题是我想重定向到索引页,而不必查看 URL 中附加的属性。
我发现解决方案是addFlashAttributes
,它不会在 URL 中附加属性,但我看不到此方法保存的任何消息。
Code:
控制器
@RequestMapping(value="/login",method = RequestMethod.POST)
public ModelAndView loginResult(HttpServletRequest req,
HttpServletResponse res,
RedirectAttributes redir) {
if (uname.equals(inf.getUsername()) & &pwd.equals(inf.getPassword()) && dept.equals(inf.getDept()))
{
req.getSession().setAttribute("uname",inf.getName());
return new ModelAndView("employeeLoginResult", "message", message1);
}
else if (uname.equals(inf2.getUsername()) && pwd.equals(inf2.getPassword()) && dept.equals(inf2.getDept()))
{
req.getSession().setAttribute("uname", inf2.getName());
return new ModelAndView("adminLoginResult", "message", message2);
}
else
{
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("redirect:/index.jsp");
redir.addFlashAttribute("message","Sorry Username Password Error");
return modelAndView;
}
}
现在我已经硬编码了验证值,并且将来会将验证与数据库的 DAO 层集成。这addFlashAttribute
我不需要将消息附加到 URL,但我也想在索引页上显示消息。
索引页
<head><tile>index</title>
</head>
<body>
<b><span class="heading">LOGIN USER</span></b>
<div class="container">
<form action="login.html" method="Post">
<div class="form_style">
<input type="text" name="username" placeholder="Enter Username"/>
<input type="password" name="pwd" placeholder="Enter password"/>
<select name="dept">
<option>IT</option>
<option>Admin</option>
<option>HR</option>
<option>Marketing</option>
</select>
<input type="Submit" value="submit">
<p>${message}</p>
</div>
</form>
</div>
</body>
我究竟做错了什么?谁能解释一下吗?
Why is ${message}
不显示属性?
任何帮助表示赞赏。谢谢你同样的。