为什么 Liferay 6.0.6 在渲染后 URL 中包含 portlet 操作参数?

发布于 2024-12-19 17:29:39 字数 434 浏览 1 评论 0原文

如果我提交到由以下内容生成的 URL:

<portlet:actionURL name="myAction" />

我最终会得到类似 ff 的内容。渲染阶段后浏览器中的 URL:

http://localhost:8080/...&_myportlet_WAR_myportlet_javax.portlet.action=myAction&...

问题是,如果我单击浏览器的刷新按钮,该操作将再次执行。据推测,这是由于 URL 中存在该参数所致。

有谁知道为什么 Liferay 在 URL 渲染后包含该参数以及是否有修复或解决方法?

编辑:我的 portlet 类从 com.liferay.util.bridges.mvc.MVCPortlet 扩展。

If I submit to the URL produced by:

<portlet:actionURL name="myAction" />

I end up with the something like the ff. URL in the browser after the render phase:

http://localhost:8080/...&_myportlet_WAR_myportlet_javax.portlet.action=myAction&...

The problem with this is that, if I click on the browser's refresh button, the action gets executed again. Presumably, this is due to the presence of that parameter in the URL.

Does anyone know why Liferay includes that parameter in the URL post-render and if there is a fix or workaround for it?

EDIT: My portlet class extends from com.liferay.util.bridges.mvc.MVCPortlet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

场罚期间 2024-12-26 17:29:39

问题在于,如果我单击浏览器的刷新按钮,该操作将再次执行。据推测,这是由于 URL 中存在该参数所致。

我怀疑是这样。可能是因为您通过HTTP POST方法提交数据。或者您是通过 GET 提交数据吗?如果是这样,那将是一种奇怪的行为。

关于 URL 中的参数:我没有答案,但这种行为对我来说并不奇怪。例如,假设我们使用 doGet()doPost() 方法创建一个 servlet。如果我通过 post 向 URL 提交一些数据(可能是为了执行某些操作),则 doPost() 方法的响应将与提交的 URL 相关,因此结果页面的 URL 将是相同的。我们可以在这里遵循相同的逻辑:如果您提交到操作阶段,则提交的 URL 将是结果 URL。

如何处理?答案是POST-REDIRECT- GET 模式。您应该从 processAction()HTTP 302 响应> 方法,通常将浏览器重定向到原始页面。

做起来很简单。表单页面的 JSP 应将当前 URL 存储在表单的输入中:

<%
    String redirect = PortalUtil.getCurrentURL(renderRequest);
%>
<input type="hidden" name="<portlet:namespace />redirect" value="<%= redirect %>">

然后在 processAction() 中重定向到此 URL。 如果您使用的是 Liferay MVCPortlet,则只需在所有操作完成后调用 sendRedirect() 方法即可:

public void processAction(ActionRequest req, ActionResponse resp) {
    // Doing stuff
    sendRedirect(req, resp);
}

如果原始 URL 的值位于名为 “redirect” 那么这个方法会神奇地将你重定向回原始页面。

如果您使用 Liferay MVC,而只是使用 GenericPortlet 的子类,只需从请求中检索 URL 并使用方法 ActionResponse.sendRedirect():

public void processAction(ActionRequest req, ActionResponse resp) {
    // Doing stuff
    String redirect = (String)actionRequest.getAttribute("redirect");
    resp.sendRedirect(redirect);
}

The problem with this is that, if I click on the browser's refresh button, the action gets executed again. Presumably, this is due to the presence of that parameter in the URL.

I doubt so. It is probably because you submitted data through the HTTP POST method. Or are you submitting your data through GET? If so, that would be a strange behavior.

About the parameter in the URL: I do not have an answer but this behavor is no surprise to me. Suppose for example that we create a servlet with doGet() and doPost() methods. If I submit some data to a URL through post (presumably to execute some action) the response of the doPost() method would be relative to the submitted URL, so the URL of the resulting page will be the same. We can follow the same logic here: if you submitted to the action phase, the submitted URL will be the resulting one.

How to deal with it? The answer is the POST-REDIRECT-GET pattern. You should send a HTTP 302 response to the browser from your processAction() method, usually redirecting the browser to the original page.

To do it is simple. The JSP of the form page should store the current URL in an input of your form:

<%
    String redirect = PortalUtil.getCurrentURL(renderRequest);
%>
<input type="hidden" name="<portlet:namespace />redirect" value="<%= redirect %>">

Then you redirect to this URL in the processAction(). If you are using Liferay MVCPortlet, you just need to call the sendRedirect() method after all operations:

public void processAction(ActionRequest req, ActionResponse resp) {
    // Doing stuff
    sendRedirect(req, resp);
}

If the value of the original URL is in a request parameter called "redirect" then this method will magically redirect you back to the original page.

If you are not using Liferay MVC but instead a mere subclass of GenericPortlet, just retrieve the URL from the request and use the method ActionResponse.sendRedirect():

public void processAction(ActionRequest req, ActionResponse resp) {
    // Doing stuff
    String redirect = (String)actionRequest.getAttribute("redirect");
    resp.sendRedirect(redirect);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文