使用restful java和相应的JSP时如何找到/获取我的相对路径

发布于 2024-11-15 04:46:48 字数 1233 浏览 2 评论 0原文

假设我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羞稚 2024-11-22 04:46:48

只需使用域相关链接。即,让它以 / 开头。您可以通过 HttpServletRequest#getContextPath()

<a href="${pageContext.request.contextPath}/index">index</a>

它将以 HTML 形式结束,如:

<a href="/yourwebappcontextpath/index">index</a>

如果您厌倦了每次都重复它,请使用 。另请参阅 这个答案

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文