assume you have something like this in normal servlet without using spring mvc:
if(username == "X" && password =="Y"){
response.sendRedirect("./user/welcomePage.jsp");
}else{
response.sendRedirect("./login.jsp?wrongPass=true");
}
and if the username && password is wrong, after redirection to login.jsp page, inform user that the username or password is wrong, somewhere in the login.jsp page in red color:
<p style="color:red";
<%= (request.getParameter("wrongPass")==null)? "visibility:hidden":"" %>;">
Your username/password is wrong !</p>
My question is, is there any way to pass an argument like:
response.sendRedirect("./login.jsp?wrongPass=true");
inside Spring MVC controller ?
coz if we have a controller like this:
@Controller
@RequestMapping(value ="/")
public class Welcome{
public String welcome(@RequestParam("j_username") String username,
@RequestParam("j_password") String password){
.
.
.
return "login";
as log as we need to return a String to form a page & because we have InternalResourceViewResolver defined:
<bean >class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
if we pass the argument in our controller like: return “login?wrongPass=true”
after using InternalResourceViewResolver that will create something like :
login?wrongPass=true.jsp Which is totally wrong ...
now, what is your solution to solve this problem (without) adding that String to model inside our controller & fetch the parameter inside JSP ?
I want the browser url become something like:
http://localhost:8080/myProject/login.jsp?wrongPass=true
You can use the redirect prefix and then you can use absolute URL
You can build the absolute url dynamically also using the methods of HttpServletRequest