Skip to Content
0
Former Member
Nov 21, 2012 at 01:54 PM

WCEM 2.0 FacesContext.getCurrentInstance() is null

72 Views

Hi,

I'm trying to implement a custom page that will logout the user (preferably while navigating to that page, and preferably without resorting to javascript).

However: all of the standard WCEM code for logging in and out requires access to a HttpRequestServlet and HttpResponseServlet which are of course needed to delete sessions, cookies and whatnot. All ViewComponentHandler code that i've been looking through receives these objects trough the FacesContext.getCurrentInstance() method.

For example:

package com.sap.wec.app.common.module.user.ui.user.mainpage.vc.impl;... snip ...public class MainPageLoginRegisterAnchorVCHandler extends ViewComponentHandlerCommonFunctions {... snip ...

/**
* The method implements the logoff process triggered from a xhtml view. On
* logoff the session will be invalidated and a redirect to the
* application's start page will be triggered.<br/>
*
* @throws ApplicationBaseRuntimeException in case of any problem
*/
public String doLogoff() {
String newPath = null; FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest httpServletRequest = (HttpServletRequest) context.getExternalContext()
.getRequest();
... snip ...

However, when I create my own page / include / view / viewhandler stack, the FacesContext.getCurrentInstance() always return null:

ui-repository.xml:

<UIRepository xmlns="http://www.sap.com/wec/frw/tc/ui/runtime/ui-repository"
xmlns:wec="com.sap.wec.core.config" module="user">

<PageReference moduleInterface="com.sap.main"
moduleInterfacePage="basePage" name="refBasePage" />

<Page name="customerRegionSelectionPage" defaultLayout="1column" extends="refBasePage"
contentArea="content" ownLayoutUsed="true">
<Layout name="1column" />
<Area defaultInclude="customerRegionSelection" name="content" />
</Page>

<UIInclude name="customerRegionSelection"/>

<ViewComponent name="customerRegionSelectionView"
componentHandlerClassName="eu.customer.wec.app.common.module.user.handler.CustomerRegionSelectionViewHandler"/></UIRepository>

customerRegionSelectionPage.xhtml:

<!DOCTYPE ui:composition SYSTEM "-//W3C//DTD XHTML 1.0 Transitional//EN">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:wcf="http://java.sun.com/jsf/composite/wec"
xmlns:wecf="http://java.sap.com/wec/wcf/composition"
template="#{wecf:getBasePage('main','customerServicePage')}">

<ui:define name="baseExtension">
<ui:decorate template="#{wecf:getPageLayout('user','customerRegionSelectionPage')}">
<ui:define name="content">
<wcf:pageArea areaName="content" id="paCCS"/>
</ui:define>
</ui:decorate>
</ui:define>
</ui:composition>customerRegionSelection.xhtml (include): <!DOCTYPE ui:composition SYSTEM "-//W3C//DTD XHTML 1.0 Transitional//EN">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:user="http://java.sun.com/jsf/composite/sap/user/components"> <userSystemLinks:customerRegionSelectionView id="cSLnok" />
</ui:composition>

customerRegionSelectionView.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:main="http://java.sun.com/jsf/composite/sap/main/components"
xmlns:wcf="http://java.sun.com/jsf/composite/wec"
xmlns:wec="http://java.sap.com/wec/wcf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html">
<cc:interface componentType="sap.wec.ViewComponent"/>
<cc:implementation> <wec:form id="frmDownload">
<wec:commandButton id="btn1" type="submit" action="#{cc.vch.logoff}" value="Logoff1" />
</wec:form>

</cc:implementation>
</html>

CustomerRegionSelectionViewHandler:

package eu.customer.wec.app.common.module.user.handler;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.sap.wec.app.common.module.user.modulemgmt.UserModuleAccess;
import com.sap.wec.tc.core.ui.vc.BuildTime;
import com.sap.wec.tc.core.ui.vc.RenderTime;
import com.sap.wec.tc.core.ui.vc.impl.ViewComponentHandlerBaseImpl;
import com.sap.wec.tc.core.user.UserCore;public class CustomerRegionSelectionViewHandler extends
ViewComponentHandlerBaseImpl<UserModuleAccess> { @Override
public void initialize() {
// TODO Auto-generated method stub
super.initialize();

// test context value, in debugger it will be null
FacesContext context = FacesContext.getCurrentInstance();
}

@BuildTime
public String testBuildTime() {
// test context value, in debugger it will be null
FacesContext context = FacesContext.getCurrentInstance();
return "";
}

@RenderTime
public void testRenderTime() {
// test context value, in debugger it will be null
FacesContext context = FacesContext.getCurrentInstance();
}

public String logoff() {
// test context value, in debugger it will be null
FacesContext context = FacesContext.getCurrentInstance();
return "";
}
}

As you can see, I test the value of the FacesContext.getCurrentInstance() method in multiple stages of the view life cycle (build, render, action), but it is always null.

Any idea what I'm doing wrong? Am I missing some configuration in my customer module that prevents the faces context from being available? Is there any other way to retrieve the HttpServletRequest and HttpServletResponse objects in my ViewHandler? Is there any other way to log off without having access to these objects?

regards,

Wilco Menge