dcsimg
December 7, 2016
Hot Topics:

Packaging, Deploying, and Running the JBoss Seam Project

  • October 24, 2016
  • By Deepak Vohra
  • Send Email »
  • More Articles »

In "Getting Started with an MVC Application with JBoss Seam," the first article on JBoss Seam, we created a JBoss Seam project and in "Developing an Entity Bean and Session Bean Facade," the second article, we added an entity bean and a session bean. In this final article, we shall add a Web descriptor, a JSF user interface, and subsequently deploy and run the project. This tutorial has the following sections:

  • Creating the Web Descriptor
  • Creating a JSF User Interface
  • Deploying the JBoss Seam Project
  • Running the JBoss Seam Project

Creating the Web Descriptor

Seam requires the org.jboss.seam.servlet.SeamListener listener class to be configured in web.xml. Specify the default suffix for the JSF pages as .xhtml and configure a servlet for the servlet class javax.faces.webapp.FacesServlet. Add servlet mapping to the URL pattern *.seam. The web.xml file is listed below.

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0"
   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://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

   <!-- Seam -->

   <listener>
      <listener-class>
         org.jboss.seam.servlet.SeamListener
      </listener-class>
   </listener>

   <context-param>
      <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
      <param-value>.xhtml</param-value>
   </context-param>

   <servlet>
      <servlet-name>Faces Servlet</servlet-name>
      <servlet-class>
         javax.faces.webapp.FacesServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
      <servlet-name>Faces Servlet</servlet-name>
      <url-pattern>*.seam</url-pattern>
   </servlet-mapping>

   <session-config>
      <session-timeout>10</session-timeout>
   </session-config>

</web-app>

The web.xml file is shown in the Package Explorer.

Seam01
Figure 1: The web.xml file

Creating a JSF User Interface

In this section, we shall develop a JSF page, catalog.xhtml, to create a new catalog entry. Add a h:panelGrid within a h:form and add h:inputText tags for input fields for a catalog entry fields journal, publisher, edition, title, and author. We don't provide an input field for the catalog id because the ID is generated automatically using auto increment in MySQL database. Add a h:commandButton for a command button to submit the form. Set the action attribute of h:commandButton to catalogBean.create using EL expression. When the form is submitted, the create method of the catalogBean gets invoked. The input text fields have value binding to the properties of the catalog bean. When the form is submitted, the catalog context variable values get injected into the attributes of the Seam component catalog injected using the @In annotation. The catalog.xhtml is listed below.

<?xml version="1.0" encoding="utf-8"?>
<!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:s="http://jboss.org/schema/seam/taglib"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
   <head>
      <title>Create New Catalog Entry</title>
   </head>
   <body>
   <h2>Create New Catalog Entry</h2>
      <f:view>
         <h:form id="catalogform">
            <s:validateAll>
               <h:panelGrid columns="2">
                  Journal: <h:inputText id="journal"
                                        value="#{catalog.journal}"
                                        required="true"/>
                  Publisher: <h:inputText id="publisher"
                                          value="#{catalog.publisher}"
                                          required="true"/>
                  Edition: <h:inputText id="edition"
                                        value="#{catalog.edition}"
                                        required="true"/>
                  Title: <h:inputText id="title"
                                      value="#{catalog.title}"
                                      required="true"/>
                  Author: <h:inputText id="author"
                                       value="#{catalog.author}"
                                       required="true"/>
               </h:panelGrid>
            </s:validateAll>
            <h:messages id="messages"/>
            <h:commandButton id="createCatalog"
                             value="Create Catalog"
                             action="#{catalogBean.create}"/>
         </h:form>
      </f:view>
   </body>

</html>

Add a createdCatalog.xhtml JSF page to render if a catalog entry gets created.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt
<html xmlns="http://www.w3.org/1999/xhtml"
         xmlns:f="http://java.sun.com/jsf/core">

   <head>
      <title>Successfully created catalog</title>
   </head>
   <body>
      <f:view>
         Created catalog for #{catalog.title}.
      </f:view>
   </body>

</html>

Add an index.xhtml to redirect to catalog.seam, which invokes the javax.faces.webapp.FacesServlet Servlet.

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

The directory structure of the Seam catalog-web sub project is shown in the Package Explorer.

Seam02
Figure 2: The Seam's catalog-web directory structure

Deploying the JBoss Seam Project

In this section, we shall compile and package the Seam application using Maven. In addition to the pom.xml at the application root, three other Maven pom.xml files are provided, one each in the catalog-ear, catalog-ejb, and catalog-web sub-projects. In the pom.xml for the catalog-ear sub-project, specify packaging as ear. Specify dependency on the catalog-web and the catalog-ejb sub-projects. In the build, add the Maven EAR plugin. In the configuration for the Maven EAR plug-in, specify the output directory as C:\JBossAS7\jboss-as-7.1.1.Final\standalone\deployments. Include the Web module catalog-web and the EJB module catalog-ejb in the build. Add a <repository/> for the JBoss - Releases Repository. The pom.xml for catalog-ear is listed below.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/maven-v4_0_0.xsd">
   <repositories>
      <repository>
         <id>maven</id>
         <name>JBoss - Releases Repository</name>
         <url>http://repository.jboss.com/maven2</url>
         <layout>default</layout>
      </repository>

   </repositories>    <modelVersion>4.0.0</modelVersion>
   <parent>
      <artifactId>catalog</artifactId>
      <groupId>org.jboss.seam.catalog</groupId>
      <version>1.0.0</version>
      <relativePath>../pom.xml</relativePath>
   </parent>

   <groupId>org.jboss.seam.catalog</groupId>

   <artifactId>catalog-ear</artifactId>
   <packaging>ear</packaging>
   <name>Catalog EAR Module</name>

   <dependencies>
      <dependency>
         <groupId>org.jboss.seam.catalog</groupId>
         <artifactId>catalog-web</artifactId>
         <type>war</type>
      </dependency>
      <dependency>
         <groupId>org.jboss.seam.catalog</groupId>
         <artifactId>catalog-ejb</artifactId>
         <type>ejb</type>
      </dependency>
   </dependencies>

   <build>
      <finalName>seam-catalog</finalName>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-ear-plugin</artifactId>
            <configuration>
               <version>6</version>
               <outputDirectory>
                  C:\JBossAS8\wildfly-8.0.0.CR1\standalone\deployments
               =-</outputDirectory>
               <defaultLibBundleDir>lib</defaultLibBundleDir>
               <modules>
                     <webModule>
                     <groupId>org.jboss.seam.catalog</groupId>
                     <artifactId>catalog-web</artifactId>

                     <contextRoot>/seam-catalog</contextRoot>
                     <bundleFileName>catalog-web.war</bundleFileName>
                  </webModule>
                  <ejbModule>
                     <groupId>org.jboss.seam.catalog</groupId>

                     <artifactId>catalog-ejb</artifactId>
                     <bundleFileName>catalog-ejb.jar</bundleFileName>
                  </ejbModule>
               </modules>
             </configuration>
         </plugin>
     </plugins>
   </build>
</project>

Seam has added support for JSF 2.0, for which we include the following <dependency/> in the pom.xml for the catalog-ejb subproject.

<dependency>
   <groupId>org.jboss.seam</groupId>
   <artifactId>jboss-seam-jsf2</artifactId>
   <version>2.3.0-SNAPSHOT</version>
</dependency>

Specify packaging as ejb for the catalog-ejb sub-project. Add the other EJB, JPA, and JSF related dependencies. Add a <repository/> for the JBoss - Snapshots Repository. The pom.xml for catalog-ejb is listed below.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/maven-v4_0_0.xsd">
   <repositories>
      <repository>
         <id>maven</id>
         <name>JBoss - Releases Repository</name>
         <url>http://repository.jboss.com/maven2</url>
         <layout>default</layout>
      </repository>
      <repository>
         <id>public.jboss</id>
         <name>JBoss - Snapshots Repository</name>
         <url>https://repository.jboss.org/nexus/content/
            groups/public-jboss</url>
         <layout>default</layout>
      </repository>

      <repository>
         <id>repository.jboss.org</id>
         <name>JBoss - Snapshots Repository</name>
         <url>https://repository.jboss.org/nexus/content/
            repositories/snapshots</url>
         <layout>default</layout>
      </repository>

   6</repositories>    <modelVersion>4.0.0</modelVersion>
      <parent>
         <artifactId>catalog</artifactId>
         <groupId>org.jboss.seam.catalog</groupId>
         <version>1.0.0</version>
         <relativePath>../pom.xml</relativePath>
      </parent>

      <groupId>org.jboss.seam.catalog</groupId>
         <artifactId>catalog-ejb</artifactId>
         <packaging>ejb</packaging>
         <name>Catalog EJB Module</name>
      <dependencies>
         <dependency>
            <groupId>org.jboss.seam</groupId>
            <artifactId>jboss-seam-jsf2</artifactId>
            <version>2.3.0-SNAPSHOT</version>
         </dependency>
         <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.1.1-b04</version>
         </dependency>
         <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
         </dependency>
         <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.0.0.GA</version>
         </dependency>
         <dependency>
            <groupId>javax.ejb</groupId>
            <artifactId>ejb-api</artifactId>
            <version>3.0</version>
         </dependency>

         <dependency>
            <groupId>javax.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.1</version>
         </dependency>
         <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.3.1.Final</version>
         </dependency>
      </dependencies>
</project>

Specify packaging as war in pom.xml for the catalog-web sub-project. Add a filtering property for components.xml, in which we specify the <jndiPattern/> property.

<properties>
   <!-- filtering property for components.xml -->
   <jndiPattern>java:app/catalog-ejb/#{ejbName}</jndiPattern>
</properties>

The catalog-web sub-project has a dependency on the catalog-ejb sub-project, for which we add a <dependency/>. Add a <dependency/> on Seam for JSF 2, which is required in compiling and packaging the WAR file. But, we also included the Seam for JSF 2 dependency in the catalog-ejb sub-project. We may include only one JAR for the Seam for JSF 2 dependency in the compiled EAR file. We need to exclude the Seam for the JSF 2 JAR file from the WEB-INF/lib directory using resource filtering and the <packagingExcludes/> element in the <configuration/> element for the Maven WAR plugin. Because of version incompatibility with JBoss AS 7 runtime, also exclude the WEB-INF/lib/dom4j-1.6.1.jar, which is added as a module to export in the jboss-deployment-structure.xml. The pom.xml for the catalog-web subproject is listed below.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/maven-v4_0_0.xsd">
   <repositories>
      <repository>
         <id>maven</id>
         <name>JBoss - Releases Repository</name>
         <url>http://repository.jboss.com/maven2</url>
         <layout>default</layout>
      </repository>
      <repository>
         <id>public.jboss</id>
         <name>JBoss - Snapshots Repository</name>
         <url>https://repository.jboss.org/nexus/content/
            groups/public-jboss</url>
         <layout>default</layout>
      </repository>

      <repository>
         <id>repository.jboss.org</id>
         <name>JBoss - Snapshots Repository</name>
         <url>https://repository.jboss.org/nexus/content/
            repositories/snapshots</url>
         <layout>default</layout>
      </repository>
   </repositories>    <modelVersion>4.0.0</modelVersion>
   <parent>
      <artifactId>catalog</artifactId>
         <groupId>org.jboss.seam.catalog</groupId>
         <version>1.0.0</version>
         <relativePath>../pom.xml</relativePath>
   </parent>

   <groupId>org.jboss.seam.catalog</groupId>
   <artifactId>catalog-web</artifactId>
   <packaging>war</packaging>
   <name>Catalog Web Module</name>

   <properties>
      <!-- filtering property for components.xml -->
      <jndiPattern>java:app/catalog-ejb/#{ejbName}</jndiPattern>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.jboss.seam.catalog</groupId>
         <artifactId>catalog-ejb</artifactId>
         <type>ejb</type>
         <scope>provided</scope>
      </dependency>
      <dependency>
         <groupId>org.jboss.seam</groupId>
         <artifactId>jboss-seam-jsf2</artifactId>
         <version>2.3.0-SNAPSHOT</version>
      </dependency>
      <dependency>
         <groupId>dom4j</groupId>
         <artifactId>dom4j</artifactId>
         <version>1.6.1</version>
      </dependency>
      <dependency>
         <groupId>commons-beanutils</groupId>
         <artifactId>commons-beanutils</artifactId>

         <version>1.8.3</version>
      </dependency>
      <dependency>
         <groupId>commons-logging</groupId>
         <artifactId>commons-logging</artifactId>
         <version>1.1</version>
      </dependency>
   </dependencies>
   <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
               <webResources>
                  <resource>
                     <directory>src/main/webapp</directory>
                     <filtering>true</filtering>
                  </resource>
               </webResources>
               <packagingExcludes>
                  WEB-INF/lib/jboss-seam-jsf2-2.3.0-
                     20120313.045446-27.jar,WEB-INF/lib/
                     dom4j-1.6.1.jar
               </packagingExcludes>
            </configuration>
         </plugin>
      </plugins>
   </build>
</project>

The pom.xml for the root project is used to run the pom.xml Maven files for the sub-projects. Specify the <dependency/> for the MySQL JDBC Connector.

<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.22</version>
</dependency>

The pom.xml for the root project catalog is listed below.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
        http://maven.apache.org/maven-v4_0_0.xsd">
   <repositories>
      <repository>
         <id>maven</id>
         <name>JBoss - Releases Repository</name>
         <url>http://repository.jboss.com/maven2</url>
         <layout>default</layout>
      </repository>
   </repositories>

   <modelVersion>4.0.0</modelVersion>
   <groupId>org.jboss.seam.catalog</groupId>
   <artifactId>catalog</artifactId>
   <packaging>pom</packaging>
   <version>1.0.0</version>
   <name>Create Catalog with Seam</name>

   <modules>
      <module>catalog-ejb</module>
      <module>catalog-web</module>
      <module>catalog-ear</module>
   </modules>

   <properties>
      <example.name>catalog</example.name>
   </properties>

   <dependencyManagement>
      <dependencies>
         <dependency>
            <groupId>org.jboss.seam.catalog</groupId>
            <artifactId>catalog-ejb</artifactId>
            <version>${project.version}</version>
            <type>ejb</type>
         </dependency>
         <dependency>
            <groupId>org.jboss.seam.catalog</groupId>
            <artifactId>catalog-web</artifactId>
            <version>${project.version}</version>
            <type>war</type>
         </dependency>
         <dependency>
            <groupId>org.jboss.seam.catalog</groupId>
            <artifactId>catalog-ear</artifactId>
            <version>${project.version}</version>
         </dependency>
         <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.22</version>
         </dependency>
         <dependency>
            <groupId>org.jboss.spec.javax.servlet</groupId>
            <artifactId>jboss-servlet-api_3.0_spec</artifactId>
            <version>1.0.2.Final</version>
         </dependency>
         <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.1.1-b04</version>
         </dependency>
         <dependency>
            <groupId>org.jboss.spec.javax.faces</groupId>
            <artifactId>jboss-jsf-api_2.1_spec</artifactId>
            <version>2.0.9.Final</version>
         </dependency>
         <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
         </dependency>
         <dependency>
            <groupId>org.jboss.spec.javax.ejb</groupId>
            <artifactId>jboss-ejb-api_3.1_spec</artifactId>
            <version>1.0.2.Final</version>
         </dependency>
      </dependencies>
   </dependencyManagement>
</project>

Right-click pom.xml for the catalog project and select Run As|Maven install.

Seam03
Figure 3: Selecting Run As|Maven install

All the sub-projects get built and the EAR, WAR, and JAR modules get generated and packaged. The seam-catalog.ear file gets output to the deployments directory.

Seam04
Figure 4: Outputting the seam-catalog.ear file

Start the JBoss AS 7. The seam-catalog.ear gets deployed to the server and the /seam-catalog context gets registered.

Seam05
Figure 5: Deploying seam-catalog.ear to the server

The catalogs schema gets exported to MySQL database table.

Seam06
Figure 6: Exporting the catalogs schema

Running the JBoss Seam Project

In this section, we shall run the Seam application. Invoke the URL http://localhost:8080/seam-catalog/index.xhtml. The URL gets redirected to catalog.seam because of the redirect specified in index.xhtml. The Faces Servlet gets invoked as the servlet mapping URL pattern in web.xml is specified as *.seam. The input form for a catalog entry gets displayed.

Seam07
Figure 7: Creating a new catalog entry

Add input field values for a catalog entry and click Create Catalog.

Seam08
Figure 8: Creating a catalog

A catalog entry gets created and the catalogCreated.xhtml gets redirected to and rendered in the browser. A Seam conversation gets started.

Seam09
Figure 9: Starting a Seam conversion

Run a SELECT query on the catalogs table to output the new catalog entry.

Seam10
Figure 10: Running a SELECT query

Conclusion

In this three-section tutorial, we developed a JBoss Seam project in Eclipse IDE. We added an EJB 3.0 entity bean Seam component for database persistence and a JSF 2.0 user interface for a view template. We discussed how to make an entity or a stateful session bean a Seam component, how to inject a component into another Seam component, and how to configure a JNDI for the session bean. We ran the Seam application to create a catalog entry in MySQL database.


Tags: Java, MySQL, JDBC, JSF, Maven, JAR, JBoss, EJB, JBoss Seam, Web descriptor, Package Explorer, compile, package, Maven EAR, WAR, EAR, Faces Servlet




Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel