0

关于如何干净而稳健地将用户重定向回上一页的任何想法?

如果上一页在应用程序本身内,我只关心将它们重定向回来。我曾考虑在用户的会话中存储某种 DIY 历史记录,但这似乎并不干净或健壮。我知道我们可以重定向用户,Executions.sendRedirect(url)但我不确定获取上一页 URL 的最佳方式。

ZK 有自己的浏览器历史管理,但我认为这在尝试启动真正的浏览器重定向时没有用。

4

1 回答 1

2

尝试这个:

// check if request needs to be sent back
if (!isValidRequest()) {

  // get the current execution context
  Execution exec = Executions.getCurrent();

  // retrieve HTTP request, response objects
  HttpServletRequest request = (HttpServletRequest) exec.getNativeRequest();
  HttpServletResponse response = (HttpServletResponse) exec.getNativeResponse();

  // get the referring URL from HTTP header
  String referrer = request.getHeader("Referer");

  /* don't need to use this unless doing a forward (server-side)

  if (referrer != null) {
    URL ref = new URL(referrer);
    referrer = ref.getPath().substring(request.getContextPath().length());
  } else {
    referrer = "/index.jsp"; // home page
  }
  */

  // HTTP 301 redirect (client-side); encoding ensures URL rewriting if required
  response.sendRedirect(response.encodeRedirectURL(referrer));

  exec.setVoided(true); // no need to create UI; about to redirect
}
于 2013-07-28T14:42:22.213 回答