我有以下标签,需要它重定向到以下地址localhost/signup
。
如果我在一个深层地址中localhost/users/profile/view
,然后单击此链接,它将转到
localhost/users/profile/signup
.
如何强制它localhost/signup
从任何深度的地址重定向到而不添加localhost
到其地址的开头?那可能吗 ?
<a href="<s:url action="../signup"/>">Sign Up</a>
我的首选方法是永远不要硬编码斜杠前缀 ( /signup
) 或相对 ( ../signup
),而是使用以下方式对上下文根路径进行软编码<c:set>
:
<c:set var="root" value="${pageContext.request.contextPath}"/>
并将其用作任何内部 URL 的前缀
<a href="${root}/signup"/>Sign Up</a>
因此,即使深度发生变化,您也可以保证引用正确的上下文路径
Host/Context Path: http://localhost/blah Link: http://localhost/blah/signup
Host/Context Path: http://localhost/mycoolco/secret/blah Link: http://localhost/mycoolco/secret/blah/signup
如何尝试:
<a href="<s:url action="~/signup"/>">Sign Up</a>
以上似乎在 ASP.NET 中工作,我相信它也应该在 JSP 中工作!
简单的事情,当您放置时../SomeMethod
,服务器SomeMethod
会在单击链接的同一目录中查找。