CDI vs. ManagedBean
marcusdidiusfalco Aug 8, 2013 8:15 AMJBoss EAP 6.1
I have the following JSF
<!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
</h:head>
<h:body>
<h2>JSF example on JBoss 7</h2>
<h:form id="jsfexample">
<h:panelGrid columns="2">
<h:outputText value="Enter key:" />
<h:inputText value="#{manager.key}" />
<h:outputText value="Enter value:" />
<h:inputText value="#{manager.value}" />
<h:commandButton actionListener="#{manager.save}" value="Save" />
<h:commandButton actionListener="#{manager.clear}" value="Clear Cach" />
<h:messages />
</h:panelGrid>
</h:form>
<h:dataTable value="#{manager.cacheList}" var="item">
<h:column>
<f:facet name="header">Key</f:facet>
<h:outputText value="#{item.key}" />
</h:column>
<h:column>
<f:facet name="header">Value</f:facet>
<h:outputText value="#{item.value}" />
</h:column>
</h:dataTable>
</h:body>
</html>
And a bean:
package com.packtpub.chapter4.bean;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.event.ActionEvent;
import javax.inject.Named;
import com.packtpub.chapter4.ejb.CacheBean;
import com.packtpub.chapter4.entity.Property;
@Named("manager")
//@ManagedBean(name="manager")
public class PropertyManager {
List<Property> cacheList = new ArrayList<Property>();
@EJB
CacheBean cacheBean;
private String key;
private String value;
//Getters and setters omitted
public void save(ActionEvent e) {
cacheBean.put(this.key, this.value);
System.out.println("Saved " + key + ":" + value);
}
public void clear(ActionEvent e) {
System.out.println("Called clear");
cacheBean.delete();
}
public List<Property> getCacheList() {
return cacheBean.getCache();
}
}
It works as ManagedBean. When I change it to a CDI Bean the key and value properties are not set on the bean:
14:05:58,818 INFO [stdout] (http-localhost/127.0.0.1:8080-1) Saved null:null
I do have a beans.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans 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://docs.jboss.org/cdi/beans_1_0.xsd">
</beans>