Posts Tagged ‘JSF’

JSF Error – Target Unreachable, identifier ‘MyBacking’ resolved to null

Friday, July 16th, 2010

My JSF application was throwing the following error message:


Target Unreachable, identifier 'MyBacking' resolved to null

All of my other backing beans were working so It was kind of perplexing and I could not immediately pinpoint the error source.
To cut a long story short, it turns out that the ManagedBean annotation requires the name attribute like so:


@ManagedBean(name = "MyBacking")
@RequestScoped
public class MyBacking {
[...]

This is kind of odd since the class name and the defined attribute name are equal.

My environment:

Netbeans 6.9
Mojarra 2.0.2
EclipseLink, version: Eclipse Persistence Services – 2.0.0.v20091127-r5931
GlassFish Server 3

How to register a ServletContextListener in JSF 2.0

Thursday, May 27th, 2010

In pre JSF 2.0 you were forced to use the WEB-INF/web.xml file to register your ServletContextListener



<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <listener>
        <listener-class>com.mycompany.myproject.MyServletContextListener</listener-class>
    </listener>
...
</web-app>

While you can still do this, in JSF 2.0 it is now possible to register your ServletContextListener or rather any of the following Listeners:

ServletContextListener, ServletContextAttributeListener, ServletRequestListener, ServletRequestAttributeListener, HttpSessionListener, HttpSessionAttributeListener

by using the @WebListener annotation.

So your listener implementation would look like the following:


@WebListener
public class MyServletContextListener implements ServletContextListener {
...
}

When registering my ServletContextListener I ran into the following error messages:


SCHWERWIEGEND: PWC1306: Startup of context /MyApp failed due to previous errors
SCHWERWIEGEND: PWC1305: Exception during cleanup after start failed
org.apache.catalina.LifecycleException: PWC2769: Manager has not yet been started
        at org.apache.catalina.session.StandardManager.stop(StandardManager.java:892)
        at org.apache.catalina.core.StandardContext.stop(StandardContext.java:5383)
        ...

SCHWERWIEGEND: ContainerBase.addChild: start:
org.apache.catalina.LifecycleException: java.lang.NullPointerException
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:5216)
        at com.sun.enterprise.web.WebModule.start(WebModule.java:499)
        ...

WARNUNG: java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: java.lang.NullPointerException
java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: java.lang.NullPointerException
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:932)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:912)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:694)
        ...

SCHWERWIEGEND: Exception while invoking class com.sun.enterprise.web.WebApplication start method
java.lang.Exception: java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: java.lang.NullPointerException
        at com.sun.enterprise.web.WebApplication.start(WebApplication.java:117)
        at org.glassfish.internal.data.EngineRef.start(EngineRef.java:126)
        at org.glassfish.internal.data.ModuleInfo.start(ModuleInfo.java:241)
        at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:236)

SCHWERWIEGEND: Exception while loading the app
java.lang.Exception: java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: java.lang.NullPointerException
        at com.sun.enterprise.web.WebApplication.start(WebApplication.java:117)
        at org.glassfish.internal.data.EngineRef.start(EngineRef.java:126)
        at org.glassfish.internal.data.ModuleInfo.start(ModuleInfo.java:241)

This was a simple oversight on my part. In my Listener I wanted to use an EJB to add some data to the database using JPA. I forgot to use the @EJB annotation when defining the EJB attribute:


@EJB
private MyFacade myfacade;
				

Processing GET request parameters in JSF 2.0

Friday, April 23rd, 2010

In JSF 2.0 <h:link> tags were introduced that allow users to navigate to a JSF page without using a postback request.

For example:


<h:link value="3 Month Subscription" outcome="subscribe" />

would create an HTML link similar to:


<a href="/MyApp/faces/subscribe.xhtml">3 Month Subscription</a>

The value of the “outcome” parameter utilizes JSF 2.0 implicit navigation rules that save you the trouble of defining navigation cases in the faces-config.xml file.

Furthermore query parameters may be added to the links:


<h:link value="3 Month Subscription" outcome="subscribe">
    <f:param name="subscriptionType" value="threeMonth" />
</h:link>

<h:link value="6 Month Subscription" outcome="subscribe">
    <f:param name="subscriptionType" value="sixMonth" />
</h:link>

The links would look like this:


<a href="/MyApp/faces/subscribe.xhtml?subscriptionType=threeMonth">3 Month Subscription</a>
<a href="/MyApp/faces/subscribe.xhtml?subscriptionType=sixMonth">6 Month Subscription</a>

This way we are able to preset the subscription type field on the subscribe.xhtml page. In subscribe.xhtml there might be the following Radio Buttons:


<h:selectOneRadio label="Subscription Type" value="#{BackingBean.subscriptionType}" id="subscriptionType">
    <f:selectItem itemLabel="3 Month Subscription" itemValue="threeMonth" />
    <f:selectItem itemLabel="6 Month Subscription" itemValue="sixMonth" />
</h:selectOneRadio>

Instead of representing the subscription type value as radio buttons you could use any other input component including for example a hidden field.

In order for the radio button to be selected upon loading the subscribe.xhtml page you must add the following annotation to the subscriptionType field in the corresponding JSF managed bean:


[...]
@ManagedProperty(value="#{param.subscriptionType}")
private String subscriptionType;
[...]