My Wiki!

Deltaspike tutorial: dev env

Maven

  • if ~/.m2/setting.xml does not work, remove it.
  • Download all archetype


mvn archetype:generate



Eclipse can't update archetypes

  • Go to Window→Preferences→Maven and enable checkbox named “Download repository index updates on startup”.
  • Delete eclipse.m2e settings
  [dang@localhost .m2]$ rm -rf ~/data/src/30_social_cloud_ws/01_jee_dev_ws/.metadata/.plugins/org.eclipse.m2e.core/nexus
  • OK and restart Eclipse.

Create Project

Archetype: remote → org.jboss.spec.archetypes:jboss-javaee6-webapp-archetype (An archetype that generates a starter Java EE 6 webapp project for JBoss AS 7.1 (by default) or EAP 6 (if the “enterprise” property is true))

Used by this example: http://www-beta.jboss.org/ticket-monster/Introduction/#_creating_a_new_java_ee_6_project_with_maven

Generated stuffs

The initial project includes the following Java packages:

.controller

contains the backing beans for #{newMember} and #{memberRegistration} in the JSF page index.xhtml

.data

contains a class which uses @Produces and @Named to return the list of members for index.xhtml

.model

contains the JPA entity class, a POJO annotated with @Entity, annotated with Bean Validation (JSR 303) constraints

.rest

contains the JAX-RS endpoints, POJOs annotated with @Path

.service

handles the registration transaction for new members

.util

contains Resources.java which sets up an alias for @PersistenceContext to be injectable via @Inject

Now, let’s explore the resources in the project.

main/resources/import.sql

contains insert statements that provides initial database data. This is particularly useful when hibernate.hbm2dll.auto=create-drop is set in persistence.xml. hibernate.hbm2dll.auto=create-drop causes the schema to be recreated each time the application is deployed.

main/resources/META-INF/persistence.xml

establishes that this project contains JPA entities and it identifies the datasource, which is deployed alongside the project. It also includes the hibernate.hbm2dll.auto property set to create-drop by default.

test/java/test

provides the .test package that contains MemberRegistrationTest.java, an Arquillian based test that runs both from within JBoss Developer Studio via Run As → JUnit Test and at the command line: + + mvn test –Parq-jbossas-remote + + Note that you will need to start the JBoss Enterprise Application Platform 6.2 server before running the test.

src/main/webapp

contains index.xhtml, the JSF-based user interface for the sample application.

In src/main/webapp/WEB-INF, you will find three key files:

beans.xml

is an empty file that indicates this is a CDI capable EE6 application

faces-config.xml

is an empty file that indicates this is a JSF capable EE6 application

ticket-monster-ds.xml

when deployed, creates a new datasource within the JBoss container

Add new Entity

There are several ways to add a new JPA entity to your project:

Starting from scratch

Right-click on the .model package and select New → Class. JPA entities are annotated POJOs so starting from a simple class is a common approach.

Reverse Engineering

Right-click on the "model" package and select New → JPA Entities from Tables. For more information on this technique see this video

Using Forge

to create a new entity for your project using a CLI (we will explore this in more detail below)

Reverse Engineering with Forge

JBoss Developer Studio or JBoss Tools.

Exercises: Adding entities and annotation stuffs.

Using JBoss Tools

Install from marketplace.

Create new Class with private attributes. Then generate getter/setter, right-click on editer windows, context menu select Source → Generate Getters and Setters.

Now, right-click on the editor, from the pop-up context menu select Source → Generate Hibernate/JPA Annotations.

Add the Bean Validation constraint annotations, such as @NotNull to the fields.

Deploy on server

Long story: https://docs.jboss.org/author/display/AS7/Securing+the+Management+Interfaces

[dang@localhost jbossAS]$ ./bin/add-user.sh 

What type of user do you wish to add? 
 a) Management User (mgmt-users.properties) 
 b) Application User (application-users.properties)
(a): a

Enter the details of the new user to add.
Realm (ManagementRealm) : 
Username : admin
Password : 
Re-enter Password : password
The username 'admin' is easy to guess
Are you sure you want to add user 'admin' yes/no? yes
About to add user 'admin' for realm 'ManagementRealm'
Is this correct yes/no? yes
Added user 'admin' to file '/home/dang/data/devfs/opt/jboss-as-7.1.1.Final/standalone/configuration/mgmt-users.properties'
Added user 'admin' to file '/home/dang/data/devfs/opt/jboss-as-7.1.1.Final/domain/configuration/mgmt-users.properties'

H2 console

In memory database

Copy h2console.war to server/standalone/deployments/

http://localhost:8080/h2console

Find the connection in WEB-INF/*-ds.xml

  jdbc:h2:mem:AuctionServiceWebapp
  
 <connection-url>jdbc:h2:mem:AuctionServiceWebapp;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1</connection-url>
        <driver>h2</driver>
        <security>
            <user-name>sa</user-name>
            <password>sa</password>
        </security>

Adding a JAX-RS RESTful web service

Right-click on the .rest package, select New → Class from the context menu, and enter AuctionService as the class name.

Add following code and imports.

package de.dailab.socialcloud.auctionservice.webapp.AuctionServiceWebapp.rest;

import java.util.List;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import de.dailab.socialcloud.auctionservice.webapp.AuctionServiceWebapp.model.Auction;

@Path("/auctions")
@RequestScoped
public class AuctionRESTService {
	@Inject
	private EntityManager em;
	
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public List<Auction> getAllAuctions() {
		final List<Auction> results =
				em.createQuery(
						"select a from Auction a order by a.name").getResultList();
		return results;
	}
}

Adding a jQuery Mobile client application

Tip: For more information on building HTML5 + REST applications with JBoss technologies, check out Aerogear.

First, using the Project Explorer, navigate to src/main/webapp, and right-click on webapp, and choose New HTML file.

On the Select HTML Template page of the New HTML File wizard, select New HTML File (5).

We shall now add the jQuery and jQuery Mobile JavaScript and CSS files to the HTML document. Luckily for us we can do this by clicking the JS/CSS widget in the palette.

We shall now proceed to setup the page layout. Click the page widget in the palette to do so. Ensure that the cursor is in the <body> element of the document when you do so.

To populate the page content, remove the paragraph element: <p>Page content goes here.</p> to start with a blank content section. Click the Listview widget in the palette to start populating the content section.

Select the inset checkbox to display the list as an inset list. Inset lists do not span the entire widget of the display. Set the ID as “listOfItems”. Retain the number of items in the list as three, and also their labels, but modify the URL values to #. Retain the default values for the other fields, and click Finish. This will create a listview widget with 3 item entries in the list. The jQuery Mobile page is now structurally complete.

Now the secret sauce to connecting your front-end to your back-end is simply observing the jQuery Mobile pageinit JavaScript event and including an invocation of the previously created Events JAX-RS service.

Insert the following block of code as the last item in the <head> element

	<script type="text/javascript">
		$(document).on("pageinit", "#page-1", function(event) {
			$.getJSON("rest/auctions", function(auctions) {
				var auctionsList = $("auctionsList");
				auctionsList.empty();
				$.each(auctions, function(index, auction) {
					auctionsList.append("<li><a href='#'>" + auction.name + "</a>");
					});
				auctionsList.listview("refresh");
			});
		});
	</script>

Resources


Navigation