JSF page does not extract variable from CDI bean

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.

+4
source share
3 answers

A similar question was posted on the 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 more details.

+3
source

WAS8 seems to support MyFaces 2.0.x and is causing CDI issues when using 2.1.x. Thus, your best option is to downgrade to 2.0.x to version 2.1.x.

You can read about these issues at ICEFACES JIRA here.

0
source

It looks like you used the wrong case on the xhtml page. Must be myVal instead of myval.

0
source

Source: https://habr.com/ru/post/1433472/


All Articles