How do I find/get my relative paths when using restful java and corresponding JSPs
Say I have a Contoller.java that has these 3 functions to direct traffic to the 3 jsps (index.jsp, create.jsp, show.jsp)
public class Controller
{
@GET
@Path("/index")
public void index(@Context HttpServletRequest request,
@Context HttpServletResponse response)
{
//If I get hit I redirect to index.jsp
}
@POST
@Path("/create")
public void create(@FormParam ("name") String name, @Context HttpServletRequest request)
@Context HttpServletResponse response
{
//if i get hit I take the form param name do stuff to create new instance of whatever
}
@GET
@Path("/show/{id}")
public void show (@PathParam(id) String id, @Context HttpServletRequest request,
@Context HttpServletResponse response)
{
//if I get hit I show the object with the given id from the url
}
}
In my constituent JSP
index.jsp just a simple page to display created types - easy enough
create.jsp
Just form to create the type and redirect back to index.jsp on submit
-->Problem: link back to index.jsp would be <a href="index">index</a>
show.jsp
just a form with the defualt values of the selected instance and a button to return to index
--Problem: link back to index.jsp would be <a href="../index">index</a>
Question: How do I reference the same path in different pages without being ambiguous like this. Does rest api offer a way to around this without having to find out where your page is relative to the other?
Just use domain-relative links. I.e., let it start with /
. You can obtain the context path dynamically by HttpServletRequest#getContextPath()
.
<a href="${pageContext.request.contextPath}/index">index</a>
It'll end up in HTML like as
<a href="/yourwebappcontextpath/index">index</a>
If you get tired of repeating it everytime, use the <base>
. See also this answer.
精彩评论