4
votes

I am trying to create a simple “hello world” test using JSF and CDI however the JSF page is not displaying a variable from the CDI bean. If the bean is changed to a managed bean then the variable is displayed.

The test is using Apache MyFaces 2.1.5 and is running on WebSphere application server 8.5.

No doubt there is a simple reason for the problem but I have been unable to establish why this works with a managed bean but not a CDI bean. Could someone please suggest where I'm going wrong?

The content of the files in the test are as follows

test.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:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">

<h:body>
    <h:outputText>#{testBean.myval}</h:outputText>
</h:body>
</html>

TestBean.java

import javax.inject.Named;
import javax.enterprise.context.RequestScoped;

@Named
@RequestScoped
public class TestBean {
    private String myVal = "Hello World";

    public String getMyVal() {
        return myVal;
    }

    public void setMyVal(String myVal) {
        this.myVal = myVal;
    }
}

I have also created an empty beans.xml file in WEB-INF.

3

3 Answers

3
votes

Posted a similar question on a WebSphere forum and found that only the default JSF implementation (based on MyFaces) is supported for use with CDI.

See CDI integration with JavaServer Faces for futher details.

0
votes

It looks like WAS8 supports MyFaces 2.0.x and does cause problems with CDI when 2.1.x is used. So your best option would be to downgrade to 2.0.x until 2.1.x is supported.

You can read about these issues on ICEFACES JIRA here

0
votes

Looks like you used the wrong case in the xhtml page. Should be myVal instead of myval.