2

假设我有一个 Contoller.java,它具有这 3 个函数来将流量定向到 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

  }
}

在我的组成 JSP 中

index.jsp 只是一个显示创建类型的简单页面 - 很简单

create.jsp 只需创建类型并在提交时重定向回 index.jsp --> 问题:链接回 index.jsp 将是<a href="index">index</a>

show.jsp 只是一个带有所选实例的默认值的表单和一个返回索引的按钮——问题:返回 index.jsp 的链接将是<a href="../index">index</a>

问题:我如何在不同的页面中引用相同的路径而不会像这样模棱两可。rest api 是否提供了一种解决此问题的方法,而无需找出您的页面相对于另一个页面的位置?

4

1 回答 1

2

只需使用域相关链接。即,让它以/. 您可以通过 动态获取上下文路径HttpServletRequest#getContextPath()

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

它将以 HTML 结尾,如

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

如果您厌倦了每次都重复,请使用<base>. 另请参阅此答案

于 2011-06-09T14:04:53.360 回答