【问题标题】:How to remove project name after port number [duplicate]如何在端口号后删除项目名称[重复]
【发布时间】:2016-03-21 14:29:00
【问题描述】:

我正在开发 jsf 项目。我想删除 URL 中的项目名称。我想要http://localhost:8080 而不是http://localhost:8080/simpleJSF1/。问题出在我的 java 代码中,我必须编写项目名称才能重定向到另一个页面,就像这样。

FacesContext.getCurrentInstance().getExternalContext().redirect("/simpleJSF1/login.xhtml");

如果我能够以某种方式从 url 中删除项目名称,那么我可以这样写 FacesContext.getCurrentInstance().getExternalContext().redirect("/login.xhtml");

而不是 FacesContext.getCurrentInstance().getExternalContext().redirect("/simpleJSF1/login.xhtml");

任何帮助将不胜感激。提前致谢。

【问题讨论】:

  • 等等,你只是想根据分隔符分割一个字符串?
  • 不,我想当我运行项目并输入localhost:8080 我的项目应该运行。现在要运行项目,我需要在端口号之后输入项目名称,例如localhost:8080/simpleJSF1。其中 simpleJSF1 是我的项目名称。

标签: jsf


【解决方案1】:

您可以简单地使用 ExternalContext 提供的上下文 - 这样它既不是硬编码也不是仅限于 ROOT 上下文。

FacesContext ctx= FacesContext.getCurrentInstance();
ServletContext servletContext = (ServletContext) ctx.getExternalContext().getContext();
ctx.getExternalContext().redirect(
    servletContext.getContextPath() +"/login.xhtml");

【讨论】:

  • 非常感谢您解决问题。但是有没有办法从 URL 中删除项目名称,如 localhost:8080 以直接打开项目。我不想在 URL 中看到项目名称。
  • 将其部署为 root webapp(在 tomcat ROOT.war 中)
猜你喜欢
  • 1970-01-01
  • 2015-08-04
  • 2014-03-19
  • 2016-07-24
  • 2014-07-15
  • 1970-01-01
  • 2010-11-29
  • 2018-04-11
  • 1970-01-01
相关资源
最近更新 更多