如果当前 url 不以“/”(斜杠)结尾,则链接无效

Links not working if current url doesn't end with "/" (slash)

我目前遇到了非常奇怪的行为。

如果我的 url 没有以斜杠结尾,所有 link 都被破坏了。具体来说,如果我的url是“http://localhost:8080/SharedTodoListDemo/todolist”,那么这段代码

<a href="delete" class="btn btn-primary"><spring:message code="todolist.button.delete"/></a>

重定向到“http://localhost:8080/SharedTodoListDemo/delete

如果我的 url 改为“http://localhost:8080/SharedTodoListDemo/todolist/

然后相同的代码重定向(正确)到“http://localhost:8080/SharedTodoListDemo/todolist/delete

您是否知道为什么会发生这种情况以及如何应对?我正在使用 Spring + Spring MVC 并且需要统一 link 以便能够映射到控制器方法。

谢谢。

编辑:

我也试过 href="/delete"href="./delete"。第一个更奇怪地重定向到 url 中有和没有反斜杠的“http://localhost:8080/delete”,第二个与上面的工作相同。

好的,过了一段时间我自己解决了

这样就可以了:

<spring:url value="/product/new" var="springLink" />
<a href="${springLink}">spring link</a>

以上在两种情况下都有效(原来的 url 以斜线结尾或不带斜线)。

与这些比较,它们不起作用:

<a href="new">my link</a>
<a href="/new">my link</a>
<a href="product/new">my link</a>
<a href="/product/new">my link</a>