0
model.addAttribute("error",ErrorMessages.USERLOGINERROR);

is not working while using redirect

return "redirect:/userlogin";

I have google it but not found approciate answer

if(name.equals("false")){
System.out.println(ErrorMessages.USERLOGINERROR);
model.addAttribute("error",ErrorMessages.USERLOGINERROR);
return "redirect:/getuserloginpage";
}

JSP:

<form:form action="userlogin" method="post" commandname="login">
<div align="center" style="color: red">${error}</div>
<div align="center" style="color: red">${thanks}</div>
...

expected result should be :

Please check your email or password. actual results : printed nothing. (no error or exception found)

Akash Agrawal
  • 2,219
  • 1
  • 17
  • 38
  • You might need to put the complete code,the jsp snippet where you are printing and the controller as well – Shubham Dixit Jul 16 '19 at 07:09
  • 1
    Possible duplicate of [How to pass model attributes from one Spring MVC controller to another controller?](https://stackoverflow.com/questions/7429649/how-to-pass-model-attributes-from-one-spring-mvc-controller-to-another-controlle) – Alan Hay Jul 16 '19 at 07:52
  • Actually, my query is different. I have to pass value from controller to jsp in case of redirection, not from controller to controller – Star Publicity Jul 16 '19 at 09:07

2 Answers2

0

Normal model attributes will not work with redirect.

Use redirect attributes instead like below.

@RequestMapping(value="/someURL", method=GET)
public String yourMethod(RedirectAttributes redirectAttributes)
{
   ...
   redirectAttributes.addAttribute("rd", "rdValue");
   redirectAttributes.addFlashAttribute("fa", faValue);
   return "redirect:/someOtherURL";
}
Alien
  • 15,141
  • 6
  • 37
  • 57
0

Do one thing, you are using

return "redirect:/userlogin";

it means you have

@RequestMapping(value="/userlogin", method=GET)
public String yourMethod(Model model)
{
 your code.....
 return some_page_name;// from tiles.xml or directly as per you configuration
}

now use the same return

if(name.equals("false")){
System.out.println(ErrorMessages.USERLOGINERROR);
model.addAttribute("error",ErrorMessages.USERLOGINERROR);
//return "redirect:/getuserloginpage";
return some_page_name;  // simple and easy but take care of functionality
}
Satish Verma
  • 146
  • 1
  • 4