gpt4 book ai didi

java - Spring 相当于波浪号 (~) 用于从 ASP.NET 进行路由?

转载 作者:太空宇宙 更新时间:2023-11-04 12:56:01 25 4
gpt4 key购买 nike

如果这是一件微不足道的事情,请提前表示歉意

在 ASP.NET 中,我记得你可以将路由定向到

<a href="~/User/Login">Login</a>

并且您不必担心任何与 URL 相关的内容(如果您的项目位于 localhost:1234localhost:1234/myproject/ 或其他任何位置),ASP.NET 将为您完成这项工作。

Spring 中是否有等效的东西?目前,当我使用 GlassFish 启动我的项目时,它从 url localhost:8080/myproject 启动。在我添加另一个 / 之前,资源(css、js...)甚至都没有被加载。最后(localhost:8080/myproject/)。

我的所有路由都在第一个 URL 参数上,而我的 GET 变量甚至不是 RESTful,只是因为如果我选择 /而且,我的路由会出错,即。

<a href="/home">Home</a> //will go to localhost:8080/home (not the project scope)
<a href="home">Home</a> //is fine until I go to another / in url...
//...(/foo/bar), then it goes all the way up
//to localhost:8080/foo/home

我尝试用 google 搜索这个内容,但每次我尝试用 google 搜索与 ASP.NET 类似的内容时,我都只是得到很多 ASP.NET 教程(与 Spring 无关)。

那么..有什么方法可以保持网址一致,例如:

<a href="~/user/login">Login</a>

<a href="${ projectUrl }/user/login">Login</a>

在(商业)应用程序中通常是如何完成的?这方面的最佳实践是什么?

最佳答案

您需要使用 Spring 标签库。

首先在 JSP 中导入标签库:

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>

现在,您不再编写原始 URL,而是使用 <spring:url> ,例如:

<a href="<spring:url value="user/login"/>">Login</a>

或者您可以将值分配给变量并引用它:

<spring:url value="user/login" var="login"/>
<a href="${login}">Login</a>

您更喜欢哪个是偏好问题。

然后,该标签将计算出 Web 应用程序上下文等,并替换 <spring:url ...带有完整的 URL。

关于java - Spring 相当于波浪号 (~) 用于从 ASP.NET 进行路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35383514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com