Advertise with us

Search Mastertheboss

Our eBooks

Java EE 6 CDI example application

Handling Persistence

In order to be able to use JPA, you need to add a persistence.xml file into the src\main\resources\META-INF folder of your project:

<persistence version="2.0"
   xmlns="" xmlns:xsi=""
   <persistence-unit name="primary">

         <!-- Properties for Hibernate -->
         <property name="" value="create-drop" />
         <property name="hibernate.show_sql" value="false" />

In this example we're using the default H2 datasource which is bound into the JNDI java:jboss/datasources/ExampleDS

Now about the JSF view, we will add an index.xhtml page that will capture the SimpleProperty fields in the upper panel and display them in a data table in the lower panel:

<html xmlns=""
    <style type="text/css">
<!-- styles not included for brevity -->
    <h2>JSF 2 example on JBoss 7</h2>
    <h:form id="jsfexample">
        <h:panelGrid columns="2" styleClass="default">

            <h:outputText value="Enter key:" />
            <h:inputText value="#{property.key}" />

            <h:outputText value="Enter value:" />
            <h:inputText value="#{property.value}" />

            <h:commandButton actionListener="#{}"
                styleClass="buttons" value="Save key/value" />
            <h:messages />


        <h:dataTable value="#{propertyList}" var="item"
            styleClass="table" headerClass="table-header"
                <f:facet name="header">Key</f:facet>
                <h:outputText value="#{item.key}" />
                <f:facet name="header">Value</f:facet>
                <h:outputText value="#{item.value}" />
                <f:facet name="header">Delete</f:facet>
                <h:commandButton actionListener="#{manager.clear(item)}"
                styleClass="buttons" value="Delete" />


Finally, in order to be able to include this page as welcome page, include the following index.jsp into your project:

  <meta http-equiv="Refresh" content="0; URL=index.xhtml">

Running the Project from Eclipse

Once you are done with the Project set up your can deploy it from within your Eclipse Environment. Create a new Maven run configuration and add as Base Directory the Project's base directory ( you can explore through your projects using the Browse_Workspace button). Next enter the Goals "clean package jboss-as:deploy" which will respectively delete build files, compile & package, deploy on a running JBoss AS instance.

java ee 6 tutorial CDI maven eclipse plugin


  Please remember to add the jboss.home into the pom.xml file in order to be able to use the JBoss Maven plugin.


Once done click Apply and Run. This will deploy your javaee6example.war to JBoss AS 7 which can be accessed at http://localhost:8080/javaee6example/

java ee 6 tutorial CDI maven eclipse plugin

Download the sources: you can access the source code at my fork of JBoss developer framework

© 2021 All Rights Reserved.

Please publish modules in offcanvas position.