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="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
        http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
   <persistence-unit name="primary">

      <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
  
      <properties>
         <!-- Properties for Hibernate -->
         <property name="hibernate.hbm2ddl.auto" value="create-drop" />
         <property name="hibernate.show_sql" value="false" />
      </properties>
   </persistence-unit>
</persistence>

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="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"
    xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
    <style type="text/css">
<!-- styles not included for brevity -->
</style>
</h:head>
<h:body>
    <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="#{manager.save}"
                styleClass="buttons" value="Save key/value" />
        
            <h:messages />

        </h:panelGrid>

        <h:dataTable value="#{propertyList}" var="item"
            styleClass="table" headerClass="table-header"
            rowClasses="table-odd-row,table-even-row">
            <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:column>
                <f:facet name="header">Delete</f:facet>
                <h:commandButton actionListener="#{manager.clear(item)}"
                styleClass="buttons" value="Delete" />
            </h:column>

        </h:dataTable>
    </h:form>
</h:body>
</html>

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


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

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

0
0
0
s2smodern