My problem is that I want to redirect to my index page without having to see the attributes appended in the URL.
I found the solution to be addFlashAttributes, it doesn't append attributes in the URL but I can't see any message saved by this method.
Code:
Controller
@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;
}
}
Right now I have hardcoded values for validation and would integrate the validation with DAO layer of database in the future. The addFlashAttribute does the work for me of not appending the message to the URL, but I also want to display the message on the index page.