gpt4 book ai didi

java - 从 @ActionMapping 重定向,不调用毫无意义的 @RenderMapping

转载 作者:行者123 更新时间:2023-12-01 09:30:28 26 4
gpt4 key购买 nike

如何从 @ActionMapping 处理程序方法重定向并避免后续自动/默认 @RenderMapping 调用?

为什么我(认为我)需要这个

我有 @ActionMapping 方法,它“做一些事情”,然后重定向到同一 Controller 中的 @RenderMapping 方法。我想在这两个同级方法之间执行重定向,以防止在用户刷新页面时调用 @ActionMapping 处理程序(并执行操作)。如果用户这样做,这次只会调用 @RenderMapping 处理程序。

当前,当我单击指向我的 @ActionMapping 处理程序的链接时,@RenderMapping 处理程序会被调用两次。一次作为 @ActionMapping 后的默认后续操作,一次作为成功重定向的结果。

最佳答案

I had somewhat similar problem of portlet's action being called on refreshing the page (unintentional), once I had previously invoked action using button or link (intentional).

解决方法:

我在 View 上放置了一个隐藏字段,并使用javascript(在单击按钮或链接时调用)将当前URL填充为不包括queryString,如下所示:

<input type="hidden" name="redirect" id="redirect" value="" />

function setRedirectURL() {
document.getElementById('redirect').value = window.location.href.split('?')[0];
}

然后在操作方法中检索该request参数,并在成功完成操作时手动重定向到其值,如下所示:

String redirect = actionRequest.getParameter("redirect");

if (redirect != null && redirect != "") {
actionResponse.sendRedirect(redirect);
}

这导致刷新 queryString 参数。因此,下次 URL 变得简单时,甚至在刷新页面时,也只会调用 portlet 的 render 方法。

关于java - 从 @ActionMapping 重定向,不调用毫无意义的 @RenderMapping,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39471533/

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