I am trying to create a simple "hello world" test using JSF and CDI, however the variable from the CDI bean is not displayed on the JSF page. If the bean is changed to a managed bean, then the variable is displayed.
The test uses Apache MyFaces 2.1.5 and runs on the WebSphere 8.5 application server.
Sure, there is one reason for the problem, but I have not been able to establish why this works with a managed bean, but not with a CDI bean. Can someone ask where I am wrong?
The contents 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 also created an empty beans.xml file in WEB-INF.
source share