Skip to content
This repository has been archived by the owner on Jul 3, 2023. It is now read-only.

Tutorial: a Java EE Web Profile application with nemo utils, part 2

vitorsouza edited this page Mar 27, 2016 · 8 revisions

In this step we create the AutoTrab project and set it up to use nemo-utils.

Database creation and set-up

We will use JPA (Java Persistence API), one of the Java EE standards, persisting our objects in a relational database stored in the MySQL server. We need, therefore, to:

  1. Create a database schema named autotrab;
  2. Create a database user named autotrab with password autotrab;
  3. Give user autotrab full permission for the schema autotrab.

To do that, use MySQL Workbench. Once you open it, connect to the server using the root user (the administrator) and you should see a screen similar to the figure below. If you see an error message at the bottom of the screen indicating that a connection to the server could not be established, click on Server > Startup/Shutdown and click the button to start the server.

MySQL Workbench after connecting to the server as the root user.

To create the database, click the Create a new schema button in the toolbar, indicated by the red arrow in the above figure. Fill in autotrab as Schema Name and select utf8 - default collation as Default Collation. Finally, click Apply and then Apply again to create the database schema.

Next, click on Users and Privileges at the left-hand side of the Workbench's window and the Administration - Users and Privileges tab should open (see figure below). Click on the Add Account button and fill in the autotrab user information like shown in the figure below (the password, which is hidden in the figure, should be autotrab as well):

Creating the autotrab user in MySQL Workbench.

Click Apply and then switch to the Schema Privileges section of the user details (see figure below). Click the Add Entry button, choose the Selected schema radio button and select AutoTrab at the list of schemas. Click OK to go back to the Schema Privileges screen, click the Select "ALL" button to give the autotrab user all the necessary permissions over the autotrab schema and, finally, click Apply. The schema privileges configuration should end up like shown in the figure below:

Setting up schema privileges for the autotrab user and the autotrab schema.

You can now close MySQL Workbench.

Datasource configuration in WildFly

While we could configure JPA to connect to the database we have just created in a configuration file in our project, creating a datasource for it in WildFly allows us to use JTA (Java Transaction API), another standard from Java EE, which provides us with automatic transaction start/commit/rollback.

To create a JTA datasource for AutoTrab in WildFly, open the file $WILDFLY_HOME/standalone/configuration/standalone.xml and look for the tag <subsystem xmlns="urn:jboss:domain:datasources:2.0">. Inside this tag, there is a <datasources> tag which holds the configuration for the java:jboss/datasources/ExampleDS datasource that WildFly comes with. Next to it, add a datasource for the autotrab database in MySQL:

<datasource jndi-name="java:jboss/datasources/AutoTrab" pool-name="AutoTrabPool" enabled="true" jta="true" use-java-context="true">
	<connection-url>jdbc:mysql://localhost:3306/autotrab</connection-url>
	<driver>mysql</driver>
	<security>
		<user-name>autotrab</user-name>
		<password>autotrab</password>
	</security>
</datasource>

March 27, 2016 update: newer versions of MySQL require that SSL is explicitly turned off for this set-up to work. If you're experiencing SSL troubles, try this connection URL instead: jdbc:mysql://localhost:3306/autotrab?useSSL=false. Thanks, Claudenir, for this information!

It's a good idea, at this point, to test if the MySQL driver and the data source were properly configured in WildFly. To do this, start the server from within Eclipse's Servers view and verify that the console outputs no errors or exceptions. Instead, you should see messages similar to these (... indicates other messages in between):

INFO  [org.jboss.modules] (main) JBoss Modules version 1.3.3.Final
...
INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-5) JBAS010417: Started Driver service with driver-name = mysql
...
INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-6) JBAS010400: Bound data source [java:jboss/datasources/AutoTrab]
...
INFO  [org.jboss.as] (Controller Boot Thread) JBAS015874: WildFly 8.1.0.Final "Kenny" started in 5629ms - Started 190 of 239 services (81 services are lazy, passive or on-demand)

Project creation and nemo-utils set-up in Eclipse

We are finally ready to move to Eclipse and start developing our application. The first step is to create the project under Eclipse and configured it to use nemo-utils. First, project creation, under the Java EE perspective:

  1. Click File > New > Dynamic Web Project;

  2. As Project name, use AutoTrab. Make sure WildFly 8.x is selected as Target runtime, then click Next twice;

  3. At the last step, check Generate web.xml deployment descriptor and click Finish.

Since nemo-utils is published in a Maven repository, we should setup AutoTrab as a Maven project. Here's what you do:

  1. Right-click the project and select Configure > Convert to Maven Project;

  2. Fill in br.ufes.inf.nemo in the Group Id field and autotrab in the Artifact Id field, then click Finish.

If your Maven installation and your Eclipse installation all agree on the Java version to use, everything should be error-free at this point. If Maven thinks for some reason your project should use a version of Java older than the one you'd like, then you might have to fix things. Under Eclipse's preferences, visit Java > Installed JREs and make sure there is a Java 7 (or 8) JRE installed. Then at Java > Installed JREs > Execution Environments make sure JavaSE-1.7 (or 1.8) points to the Java 7 (or 8) JRE. Finally, if you open the pom.xml file that Maven created in your project and switch to the source code view at the pom.xml tab, you should have the value 1.7 (or 1.8) for both <source> and <target> under the <configuration> for the maven-compiler-plugin artifact. Oh, by the way, you should study Maven once you have the time. :)

Now we set up nemo-utils as a dependency:

  1. Open the pom.xml file, switch to the source code view (the pom.xml tab) and add the following contents above the <build> tag:

    <repositories>
    	<repository>
    		<releases>
    			<enabled>true</enabled>
    			<updatePolicy>always</updatePolicy>
    			<checksumPolicy>fail</checksumPolicy>
    		</releases>
    		<id>br.ufes.inf.nemo</id>
    		<name>Nemo/Ufes Maven2 Repository</name>
    		<url>http://dev.nemo.inf.ufes.br/maven2/</url>
    		<layout>default</layout>
    	</repository>
    </repositories>
  2. Switch to the Dependencies tab and, at the Dependencies section, click the Add... button;

  3. At Group Id, fill in br.ufes.inf.nemo, at Artifact Id, fill in nemo-utils-jee-wp, at the Version field, fill in 1.1, then click OK;

  4. Save the pom.xml file;

  5. Finally, right-click your project, and select Maven > Update Project..., clicking OK at the window that shows up.

At this point, your project should be error-free, and if you open Java Resources > Libraries > Maven Dependencies you should see two JARs: nemo-utils-jee-wp-1.1.jar and primefaces-5.1.jar.

Testing the application

To make sure everything is in order before we actually start developing the application, deploy and test it:

  1. Double-click Deployment Descriptor: AutoTrab to open the web.xml configuration file and remove all <welcome-file> entries except the one with index.html as its contents;

  2. Right-click WebContent and select New > HTML File. Fill in index.html as File name and click Next;

  3. Select New HTML File (4.01 strict) from the list of Templates and click Finish;

  4. Write whatever you want at the title and the body of the page, just so we know it's working. Save the file;

  5. Open the Servers view, right-click the WildFly server and choose Add and Remove.... Move the AutoTrab project from the Available list to the Configured list and click Finish;

  6. Start the WildFly server and open http://localhost:8080/AutoTrab/ -- your index.html page should be shown.

Next: prepare the layout and create the home page