我正在尝试使用以下代码中的getContextName()方法从支持bean获取基于JSF的Web应用程序的上下文路径,但返回的值始终为null。
String contextPath = FacesContext.getCurrentInstance().getExternalContext().getContextName();
支持bean是CDI托管的@ViewScoped,并且从JSF页面调用包含上述代码的函数。为了排除函数中任何其他代码导致问题的原因,我已经将这一行代码移动到它自己的函数中,但问题仍然存在。
我检查FacesContext.getCurrentInstance()是否为null,因此我不知道为什么getContextName()返回null。我还在服务器日志中检查了没有任何异常被抛出。
这是获取辅助bean中的上下文路径的正确方法吗?如果是这样,为什么getContextName()返回null的任何想法?如果没有,请您建议我如何获取上下文名称?
我在GlassFish第4版上运行Mojarra 2.2.6版。
更新
faces-config.xml文件的内容是
<?xml version="1.0" encoding="UTF-8" ?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
</faces-config>
web.xml文件中的内容是
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>facesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.validator.DISABLE_DEFAULT_BEAN_VALIDATOR</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>org.omnifaces.FACES_VIEWS_SCAN_PATHS</param-name>
<param-value>/*.xhtml</param-value>
</context-param>
<context-param>
<param-name>org.omnifaces.FACES_VIEWS_DISPATCH_METHOD</param-name>
<param-value>FORWARD</param-value>
</context-param>
<filter>
<filter-name>facesExceptionFilter</filter-name>
<filter-class>org.omnifaces.filter.FacesExceptionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>facesExceptionFilter</filter-name>
<servlet-name>facesServlet</servlet-name>
</filter-mapping>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/WEB-INF/errorpages/bug.xhtml</location>
</error-page>
<error-page>
<exception-type>javax.faces.application.ViewExpiredException</exception-type>
<location>/WEB-INF/errorpages/viewExpired.xhtml</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/errorpages/http404.xhtml</location>
</error-page>
<welcome-file-list>
<welcome-file>login.xhtml</welcome-file>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
我创建了一个新的支持bean来重现问题,同时排除原始支持bean中的其他代码。代码是
import java.io.Serializable;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
@Named
@ViewScoped
public class TestBacking implements Serializable {
private static final long serialVersionUID = 1L;
public void printContextPath() {
String contextPath = FacesContext.getCurrentInstance().getExternalContext().getContextName();
System.out.println("Context path: " + contextPath);
}
}