ExternalContext#redirect()不会重定向到父目录 [英] ExternalContext#redirect() does not redirect to parent directory

查看:150
本文介绍了ExternalContext#redirect()不会重定向到父目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两页:

String page1 = "user/newuser.jsf";
String page2 = "department/newdepartment.jsf";

如果我这样重定向到page1:

FacesContext.getCurrentInstance().getExternalContext().redirect(page1);

URL更改为localhost:8080/NavFile/user/newuser.jsf.

在此页面上,我重定向到page2:

On this page I redirect to page2:

FacesContext.getCurrentInstance().getExternalContext().redirect(page2);

URL更改为localhost:8080/NavFile/user/department/newdepartment.jsf.但是我的应用程序中没有user/department目录.我的目标是重定向到localhost:8080/NavFile/department/newdepartment.jsf.

URL changes to localhost:8080/NavFile/user/department/newdepartment.jsf. But there is no user/department directory in my application. My goal was to redirect to localhost:8080/NavFile/department/newdepartment.jsf.

这是怎么引起的,我该如何解决?

How is this caused and how can I solve it?

推荐答案

相对重定向URL(即,当不以/或scheme开头时)相对于当前请求URL(最终用户在浏览器的地址栏中看到) .相对于服务器端的上下文路径而言,这并不是神奇的,因为此信息在客户端是完全未知的(您知道,重定向是由Web浏览器执行的,而不是由Web服务器执行的.)

A relative redirect URL (i.e. when not starting with / or scheme) is relative to the current request URL (as the enduser sees in browser's address bar). It's not magically relative to the context path in the server side as this information is completely unknown in the client side (you know, a redirect is performed by the webbrowser, not by the webserver).

如果要相对于上下文路径进行重定向,则应包括上下文路径,以使其成为相对于域的路径.您可以通过 ExternalContext#getRequestContextPath() .

If you want to redirect relative to the context path, then you should include the context path so that it becomes domain-relative. You can get the context path dynamically via ExternalContext#getRequestContextPath().

ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.redirect(ec.getRequestContextPath() + "/" + page1);

对于page2,完整的重定向URL将变为/user/department/newdepartment.jsf,而斜杠/将使其相对于域http://localhost:8080,这正是您想要的.

In case of page2, the full redirect URL becomes /user/department/newdepartment.jsf and the leading slash / would make it relative to the domain http://localhost:8080, which is exactly what you want.

  • What URL to use to link / navigate to other JSF pages
  • Redirect to external URL in JSF
  • Hit a bean method and redirect on a GET request
  • How to navigate in JSF? How to make URL reflect current page (and not previous one)

这篇关于ExternalContext#redirect()不会重定向到父目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆