Spring MVC : How to pass Model object from one method in controller to another method in same controller? -
i have integrated spring security in application , , display error message user in case of bad credentials. jsp:
<c:out value='${secerror}' /> //prints nothing on screen <c:if test="${empty secerror}"> error empty or null. //always prints line on screen </c:if> <c:if test="${not empty secerror}"> <div class="errorblock"> login attempt not successful, try again.<br /> caused : ${sessionscope["spring_security_last_exception"].message} </div> </c:if> <c:set var = "url" value = "/j_spring_security_check" /> <form:form method="post" commandname="portallogin" action="${pagecontext.servletcontext.contextpath}${url}" name="f">
[update]: sorry all, realized model object getting overriden after redirect portallogin.html had created new model object created there previously. tried few easy options can pass model object 1 controller method method in same controller. nothing worked.
- i tried using forward: prefix instead of redirect prefix. this, didn't error message @ all.
i tried below code in loginerror method.
return new modelandview("portallogin","secerror","true");
i getting following error above code:
java.lang.illegalstateexception: neither bindingresult nor plain target object bean name 'portallogin' available request attribute
i did come across this link, found lengthy solution , wasn't sure if iv'e write code.
i wasn't sure if can use model object @modelattribute annotations@modelattribute.
please provide me code snippets / examples can try out.
my controller method this:
@requestmapping("/portallogin") public modelandview gotologin(model model) { try { system.out.println("enter gotologin************************"); } catch (exception exception) { } return new modelandview("portallogin", "portallogin", new loginmodel()); //return new modelandview("portallogin", model); } @requestmapping(value="/loginfailed", method = requestmethod.get) public modelandview loginerror(modelmap model) { //model.addattribute("secerror", "true"); //return "redirect:portallogin.html"; return new modelandview("portallogin","secerror","true"); }
[update]: work around added gotologin method logic inside loginerror method intention portallogin page. error thrown expected.
@requestmapping(value="/loginfailed", method = requestmethod.get) public modelandview loginerror(model model) { model.addattribute("secerror", "true"); return new modelandview("portallogin", "portallogin", new loginmodel()); }
but still know if can pass model object 1 controller method method in same controller through way.
you can try
<c:if test="${not empty secerror == 'true'}"> <tr> <td colspan="2" align="center"><font style="color: red">your login attempt not successful, try again</font> </td></tr></c:if>