使用restful java和相应的JSP时如何找到/获取我的相对路径
假设我有一个 Contoller.java,它具有这 3 个函数,可将流量引导至
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
}
}
我的组成 JSP
index.jsp 中的 3 个 jsps(index.jsp、create.jsp、show.jsp) 只是一个简单的页面来显示创建的类型 - 足够简单
create.jsp 只需表单即可创建类型并在提交时重定向回index.jsp -->问题:返回index.jsp的链接将是index
show.jsp 只是一个包含所选实例的默认值和返回索引的按钮的表单 --问题:链接回index.jsp将是 index
问题:如何在不同页面中引用相同的路径而不需要 就这样暧昧。休息吗 api 提供了一种解决这个问题的方法,无需 必须找出您的页面在哪里 相对于另一个?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需使用域相关链接。即,让它以
/
开头。您可以通过HttpServletRequest#getContextPath()
。它将以 HTML 形式结束,如:
如果您厌倦了每次都重复它,请使用
。另请参阅 这个答案。Just use domain-relative links. I.e., let it start with
/
. You can obtain the context path dynamically byHttpServletRequest#getContextPath()
.It'll end up in HTML like as
If you get tired of repeating it everytime, use the
<base>
. See also this answer.