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

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

问题描述

我有两页:

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

如果我像这样重定向到 page1:

If I redirect to page1 like this:

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(如最终用户所见)在浏览器的地址栏中).它并不神奇地与服务器端的上下文路径相关,因为这些信息在客户端是完全未知的(您知道,重定向是由网络浏览器执行的,而不是由网络服务器执行).

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.

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

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