Reputation: 6804
Here is the screen shot for the error i am facing.
Problem is that forgot-password is again appending in the URL.(See Address Bar of the attached image)
My Controller is below:
@Controller
@RequestMapping(value="/forgot-password")
public class ControllerForgotPassword {
@RequestMapping(value = "/email", method = RequestMethod.POST)
public ModelAndView sendMail(HttpServletRequest request) {
String email = (String) request.getParameter("email");
boolean flag=serviceForgotPassword.checkEmail(email);
ModelAndView modelAndView = new ModelAndView();
if(flag)
{
modelAndView.addObject("message", "Mail has been sent to your mail box");
modelAndView.setViewName("forgot-password-sucess");
return modelAndView;
}
else
{
modelAndView.addObject("message", "Please Enter Valid email address");
modelAndView.setViewName("forgot-password");
return modelAndView;
}
}
}
The forgot-password.jsp content is following:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="fmt"
uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Forgot-Password</title>
</head>
<body>
<p style="color:red;">${message}</p>
<form action="forgot-password/email" method="POST">
<input type="text" name="email"/>
<input type="submit" value="Send Mail">
</form>
</body>
</html>
I have tried to use modelAndView.setViewName("redirect:/forgot-password-sucess");
in the sendMail
method, but then i am not able to receive the message from controller.
EDIT :
If i add <form action="/CabFMS/forgot-password/email" method="POST">
that is my context/project name then it works with no problem.
Do i need to add my context name everywhere in the from action along with controller mapping?
Can't i use just forgot-password/email
in the form action??
Please Help.
Regards,
Arun
Upvotes: 1
Views: 560
Reputation: 43823
This form action needs to be absolute to match your controller. It is currently specifying a relative path.
<form action="/forgot-password/email" method="POST">
|
add this leading slash
Edit: This will only work if the application is at the root context.
If the application is deployed to a different context, the context will also need to be added to the form's action attribute.
A better approach would be to keep the relative path and use the HTML <base>
tag on the page which should include the context - see Is it recommended to use the <base> html tag?. I tend to write Spring applications with the following in the JSP.
<base href="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/">
That way, relative paths in the page can naturally be resolved against the correct context, wherever that may be.
Upvotes: 1