Spring MVC,百里香叶 URL

Spring MVC, Thymeleaf URL

我有 settings page 和控制器给他:

@RequestMapping(value = "/settings/password/change", method = RequestMethod.GET)
public String changePassword(Model model) {
    return "account/changepassword";
}

在此页面上有 header,其中包含 myaccount 上的 link: <a class="link" th:href="${currentUser.nickname}"><li class="li">User profile</li></a> 但是,如果我在 header 上打开 settings page link,则有 URL:localhost:8080/settings/password/change/profilename。而不是这个,我需要 URL,比如:localhost:8080/profilename。我该怎么做?

你得到 /settings/password/change/profilename 因为 th:href="${currentUser.nickname}" 创建了相对于当前路径的 URL:<a href="profilename"/>

相反,请尝试使用此 Thymeleaf syntax 来构建上下文相关 URL:

<a th:href="@{/{profilename}(profilename=${currentUser.nickname})}">link</a>