[jboss-dev-forums] [JBoss AS 7 Development] - CDI vs. ManagedBean

Hans-Michael Rupp do-not-reply at jboss.com
Thu Aug 8 08:15:15 EDT 2013


Hans-Michael Rupp [https://community.jboss.org/people/MarcusDidiusFalco] created the discussion

"CDI vs. ManagedBean"

To view the discussion, visit: https://community.jboss.org/message/831931#831931

--------------------------------------------------------------
JBoss 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 http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml http://www.w3.org/1999/xhtml"
      xmlns:h=" http://java.sun.com/jsf/html http://java.sun.com/jsf/html"
      xmlns:f=" http://java.sun.com/jsf/core http://java.sun.com/jsf/core"
      xmlns:ui=" http://java.sun.com/jsf/facelets 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 http://java.sun.com/xml/ns/javaee" xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee  http://docs.jboss.org/cdi/beans_1_0.xsd http://docs.jboss.org/cdi/beans_1_0.xsd">

</beans>
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/831931#831931]

Start a new discussion in JBoss AS 7 Development at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2225]

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-dev-forums/attachments/20130808/8d51484f/attachment.html 


More information about the jboss-dev-forums mailing list