Quantcast
Channel: All systems GO » JBoss Seam
Viewing all articles
Browse latest Browse all 4

Create a Blog with JBoss Seam, Hibernate JPA and JSF then Deploy it to Tomcat in the Cloud – Part III

$
0
0

Seam Tomcat Deployment

In this part III of the tutorial, we’ll build the entity classes from the database we created in Part I and configure the Seam project we created in Part II so as to make it fully compatible with Apache Tomcat.

Creating The Model Layer

We’ll use Hibernate‘s reverse engineering tool (hbm2java) embedded in Seam-gen to create the entity classes from the database we created in part 1.

Seam Generate Entities

Figure 3.1 - Generating entities from database in JBoss Seam

Right-click the project blog in project explorer and select New > Seam Generate Entities.

Reverse Engineering from database

Figure 3.2 - Reverse Engineering from database

Select reverse engineer from database and then click ‘next‘ like shown in figure 3.2.

Reverse engineering from database (2)

Figure 3.3 - Reverse engineering from database (2)

To display the database schema in the first place, you may need to hit the refresh button first. Since we set everything up properly (did you, too? :) in Part II, we don’t need to tackle anything else – just click Finish.

Entity classes and the controller classes

Figure 3.4 - Seam-generated entity classes and controllers

And voilà! After some processing, our entities should show up in the project explorer along with some controller classes as well as the (JSF) xhtml files in the web content as shown in figure 3.4.

The classes with the suffix ‘Home’ such as CategoryHome are some sort of a JSF managed bean + JPA controller combo’s. In fact, these classes extend Seam’s EntityHome class which is pre-built with most of the methods for instantiating the entity object and performing CRUD operations using Seam’s built-in EntityManager. Likewise, the other classes in the session package with the suffix ‘List’ such as CategoryList extend Seam’s EntityQuery class which make it easy to perform certain basic persistence queries on entities without having to write so many lines of code to invoke the EntityManager, start transactions and run queries.

In the next section we’ll do some hacks and mods to the project and make it Tomcat-savvy, so that we can preview what we’ve done so far by running the application and see it in the browser for ourselves.

Configuring the Seam Project for Tomcat Deployment

There are a few articles, blog and forum posts all over the Internet which describe how to deploy a Seam application on Tomcat. But in my experience, none of them alone were accurate for deploying Seam 2.2 apps on Tomcat 6 (and perhaps 7, too) without crashing Tomcat at startup. Either one or more of the required jars were never mentioned, or components.xml modification was not correctly described.

In this part of the tutorial, I will explain step-by-step what exactly is necessary to be done and which versions of which libraries are exactly the ones to be included in the project so that Tomcat won’t crash as soon as started up – and neither your application.

The required library jar files (some of them are already be present in the project as we set up user defined JSF libraries in part II and some of them – and unfortunately not all of them come with recent versions of Seam):

  • antlr.jar
  • asm.jar
  • cglib.jar
  • commons-beanutils.jar
  • common-collections.jar
  • commons-digester.jar
  • commons-lang.jar
  • commons-logging.jar
  • dom4j.jar
  • hibernate-annotations.jar
  • hibernate-commons-annotations.jar
  • hibernate-entitymanager.jar
  • hibernate.jar
  • hibernate-validator.jar
  • javassist.jar
  • jboss-archive-browsing.jar
  • jboss-el.jar
  • jboss-seam-debug.jar
  • jboss-seam.jar
  • jboss-seam-ui.jar
  • jsf-api.jar
  • jsf-facelets.jar
  • jsf-impl.jar
  • jstl.jar
  • jta.jar
  • lucene-core.jar
  • persistence-api.jar
  • richfaces-api.jar
  • richfaces-impl.jar
  • richfaces-ui.jar
  • \test\hibernate-all.jar

You can download all of these necessary files in a zip file here. After downloading, decompress them in a folder on your computer and follow the steps in the next section to import them into your project. Please note that since some of them are already present in your project’s WEB-INF/lib, be sure to uncheck ‘overwrite existing files’ option.

Importing the .jar files into the Eclipse Project

Expand the WebContent folder and then the WEB-INF folder residing inside of it in Eclipse’s package explorer so that you reveal WEB-INF/lib. Then right-click lib, and select ‘Import…’ from the dropdown menu that pops up.

Importing Seam jars into WEB-INF/lib

Figure 3.5 - Importing Seam jars into WEB-INF/lib

Select ‘File System’ as the import source and click ‘Next’. Then select the folder in your file system (i.e. Windows explorer on the PC, or the Finder on the Mac)

Adding external JARs into your Eclipse Project

Figure 3.6 - Copying external JARs into your Eclipse Project from the file system

Refer to figure 3.6 and make sure you tick the checkbox next to the name of the folder containing the jar files to select them all at once (if you don’t want to individually select the checkboxes of the jar files yourself) then make sure of the following:

  • Into folder: blog/WebContent/WEB-INF/lib (importing the jars anywhere outside this path will fail you)
  • Overwrite existing resources without warning had better be unchecked. Why? Because if you try this tutorial with a version of Seam other than 2.2, you probably want to keep the original versions of the duplicate jars that shipped with Seam – for not risking anything to break up.
  • Create selected folders only is as good as checked. (It should be so by default anyway)

Click Finish to start importing the Seam libraries from the file system.

Dealing with duplicate libraries in the import process

Figure 3.7 - Dealing with duplicate Seam libraries in the import process

In case you’re asked what to do about overwriting some of the libraries already existing in your project, go with ‘No to All’. Because if you try this tutorial with a version of Seam other than 2.2, you probably want to keep the original versions of the duplicate jars that shipped with Seam – for not risking anything to break up.

Modifying the data source for Tomcat

Every Seam project created with Seam-Gen or JBoss Tools comes with a datasource definition in XML format called ‘blog-ds.xml’ residing in a folder called ‘resources’ in the project folder. This approach of ‘deploying the datasource to the server’ suits the JBoss AS very well, but in the case of Tomcat, the portable datasource definition file is useless and we need to define the datasource differently on Tomcat so as to have it supply us the database connection when running our application.

Therefore we will now create a file named context.xml in our WebContent/META-INF folder and create a new resource there. Fill the context.xml with the following definitions:

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

<Context docBase="blog" path="/blog" reloadable="true">

  <Resource auth="Container" driverClassName="com.mysql.jdbc.Driver"
  maxActive="20" maxIdle="10" maxWait="-1" name="jdbc/seamblog"
  password="12345"
  type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/seamblog"
  username="blogger" />

</Context>

Now we need to handle the other configuration files which use this connection. First of them is persistence.xml is which contains the definition of our JPA persistence unit. We need to change the jta-data-source value which is normally intended for a container-managed transaction such as on JBoss AS. Since we’re working for Tomcat deployment here, we need to change the transaction type to the local resource in persistence.xml.

Open persistence.xml (src/main/META-INF/persistence.xml) and modify the persistence unit so as to resemble the following:

<?xml version="1.0" encoding="UTF-8"?>
<persistence-unit name="seamblogPU" transaction-type="RESOURCE_LOCAL">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>java:comp/env/jdbc/seamblog</jta-data-source>
      <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
         <property name="hibernate.hbm2ddl.auto" value="validate"/>
         <property name="hibernate.show_sql" value="true"/>
         <property name="hibernate.format_sql" value="true"/>         
         <property name="hibernate.default_catalog" value="seamblog"/>
         <property name="jboss.entity.manager.factory.jndi.name" value="java:/blogEntityManagerFactory"/>
      </properties>
   </persistence-unit>

Here we defined the data-source as java:comp/env/jdbc/seamblog for Tomcat requires all connection JNDI names to be prefixed with java:comp/env.

Finally we will modify the components.xml which holds the definition of certain Seam components and resides in WEB-INF folder.

There are a few things to do in components.xml

First add an XML namespace definition for transaction:

xmlns:transaction="http://jboss.com/products/seam/transaction"

and its

 xsi:schemaLocation = http://jboss.com/products/seam/transaction http://jboss.com/products/seam/transaction-2.2.xsd

then add the following line somewhere:

<transaction:entity-transaction entity-manager="#{entityManager}"/>

Also the persistence-related definitions should be something like:

   <!-- If you are using Tomcat, you need the following to have your PU started -->
  <persistence:entity-manager-factory name="entityManagerFactory"
                     persistence-unit-name="seamblogPU"/>
   <!-- If you are using Tomcat, you need the following as well to have the entityManagerFactory used -->
  <persistence:managed-persistence-context name="entityManager" auto-create="true"
                         entity-manager-factory="#{entityManagerFactory}"/>

   <!-- If you are not using JBoss AS 5 or above, the following should be commented -->
<!--    <persistence:managed-persistence-context name="entityManager" auto-create="true" -->
<!--                            persistence-unit-jndi-name="java:/blogEntityManagerFactory"/> -->

Least but not last, open up your web.xml and add the following:

<resource-ref>
        <res-ref-name>jdbc/seamblog</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
 </resource-ref>

Running the Seam Blog Project for the first time

Now let’s run the project and see what we’ve done.

Right-click the project name and select Run As > Run on Server from the pop-up menu. Remember to choose Tomcat for target server.

If you’ve done everything correctly or at least as instructed in this tutorial, you should be welcomed with Seam’s cute welcome page thanks to its default template as shown in figure 3.7.

Seam Project Successfully Deployed

Figure 3.7 - The Successfully Deployed Seam Blog Project

You can customize the welcome page by modifying home.xhtml at the root of the web content folder.
Now let’s test our new app a little: Go to the menu bar of Seam Blog and click Browse data to pull down a menu from which I want you to select ‘Category List’. Here we will encounter a blank categories list. Click ‘Create Category’ and after a possible user authentication which you may pass just by entering ‘admin’ as the username and leaving the password blank (we haven’t programmed the user authentication yet), you should then encounter a form like the one in figure 3.8:

Creating a new category for the blog

Figure 3.8 - Creating a new category for the blog

I created my first category as ‘General’ and hit ‘Save’. Then I’ll repeat the step to create a few more categories such as ‘IT’, ‘Internet’, ‘Movies’ and ‘Music’.

Category list filled up

Figure 3.9 - Seam Blog category list filled up

Finally I’ll go to Users section, to create the blog’s first user: Me!

Creating blog users

Figure 3.10 - Creating blog's users

The user entry page signals us that the form needs our help with certain aspects such as converting the password field into a real password field hiding the entered characters – i.e. JSF-wise an <h:inputSecret /> and the level field seems to make no sense there and should be worked on.

You could also choose to experiment with the post entry form which I saved for last, but not as best, yet you will probably find out how peculiar it is to select a category and a user from a grid on a separate screen instead of a pop-up menu when entering a blog post – assuming of course you did not get a “duplicate Id for a component selectParent” error which arises due to a Seam-gen 2.2 bug when the generated page of a given entity has more than one other object association – in our case the Post entity has Many-to-One associations of the Category and User entities respectively.

Our project has now some basic CRUD functionality and runs on Tomcat. In Part IV of the tutorial, we will customize the code, correct the buggy code Seam-gen gave us, and add some of our own code, all in JSF.


Viewing all articles
Browse latest Browse all 4

Trending Articles