The incoming request URL looks like: /{root}/Details/View/{id}
. I have a servlet DetailsController
which based on if a person is logged in or not will show either the "Edit" view or the "Details" view:
if (user != null) {
detailsViewModel.setUser((User) user);
getServletContext().getRequestDispatcher("/edit.jsp").forward(request, response);
} else {
getServletContext().getRequestDispatcher("/details.jsp").forward(request, response);
}
开发者_开发百科
In the edit view there is a form that has an action="Update"
and I just want it to go to the UpdateController
on /{root}/Update
but it's trying to go /{root}/Details/View/{id}/Update
. I've set the context root but to avail.
If I put action="/Update"
, it tries to go localhost:8080/Update
without the root. Is this because I am using the forward? How can I fix this? Let me know if I'm missing details you need.
You need to prepend the context path in the form action URL with help of HttpServletRequest#getContextPath()
in EL.
<form action="${pageContext.request.contextPath}/Update">
This way it becomes "/{root}/Update"
.
See also:
- Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP
精彩评论