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.