Presentation:

My name is Houcine Gasmi, I’m a Software Engineering of National Institute of applied sience and technology in Tunisia (INSAT).

I will show you an amazing world of IT development in two technologies :

  • Android
  • J2EE (especially Spring)
Advertisements

Spring Roo Startup & First application

hello everybody, i will show you how to start development with spring roo.

what is that Spring Roo?

Spring Roo is a next-generation rapid application development tool for Java developers. With Roo you can easily build full Java applications in minutes. It differs from other productivity tools by focusing on:

  • Rapid results
  • 100% develop in Java
  • Easy-to-use
  • Fast and simple to remove
  • Totally compromise free
  • Active, helpful community
  • Comprehensive technology integrations
  • Extension points via Roo add-ons
  • SpringSource-certified architecture

reference from here.

lets start by downloading spring roo now from here.

next, we have to use Maven to test, run, build … your application until now before using an IDE like STS (Spring tool Suite) or IntelliJ IDE.

After downloading we will decompress the zip file of Spring Roo in any location of your choice.

open your Terminal if you use Linux as an operating system (i use now Ubuntu 12.04 as a Linux distribution), you can open it by pressing (CTRL+ALT+T). And write this command line:

# ln -s <Location of your Spring roo directory>/bin/roo.sh /usr/bin/roo

now you can use the command “roo” in anytime you like.

Now let’s create our first application Spring Roo.

create into your work-space a directory named SpringRooFirstApplication by typing this command line:

$ mkdir SpringRooFirstApplication

Now we will move into this directory:

$ cd SpringRooFirstApplication/

Let’s use roo command line to open Spring Roo shell to create our spring roo application and execute some command line to set up our database and our web component.

$ roo

Now create our first application by tapping that:

roo> project –topLevelPackage tn.gasmi.firstapplication –projectName SpringRooFirstApplication

Spring roo create files like :

  • pom.xml: show here
  • log4j.properties: setup Log4j framework to log the activity of our application.
  •  applicationContext.xml: declare spring beans

Now we have create a simple Maven/Spring application after taping one command line into spring roo shell.

this work will not show you the true power of spring roo, but now the true strength of spring roo will appear clearly.
To set up a simple JPA application you can do the job between 6 and 24 hours. But with Spring Roo this job will be very simple and we can setUp JPA into our application, create entities and create repository to expose CRUDs between 15 min and 1 hour it depends on the complexity of the project.

Let’s setup our jpa component into our application by tapping this command line into spring roo shell:
roo>  jpa setup –provider ECLIPSELINK –database HYPERSONIC_PERSISTENT

Spring roo create/update files like :

  • database.properties: properties of database like host name, user name, password, database name, …
  • applicationContext.xml: xml file to declare beans into our application like datasource, transactionManager, entityManagerFactory, setup AspectJ and the use of annotations into spring.
  • persistence.xml: jpa properties

Now let’s create a table named Student and add some fields like first name, last name, email, address, phone number; by tapping this command line into spring roo shell:

roo>  entity jpa –class ~.model.Student –testAutomatically

what it means this ~:  this is a shortcut to the top level package like “tn.gasmi.firstapplication” in our project.

this  command line create and entity of jpa with adding some Annotations like:

  • @RooJavaBean
  • @RooToString
  • @RooJpaActiveRecord

this annotarion help spring roo do know that he should create AspectJ files like:

  • Student_Roo_JavaBean.aj : to declare getters and setters of fields into Student java class.
  • Student_Roo_Configurable.aj : for configuration
  • Student_Roo_Jpa_ActiveRecord.aj : into this aspect spring roo declare the entity manager and CRUDs to use it from Student directly.
  • Student_Roo_ToString.aj : declare the toString method.
  • Student_Roo_Jpa_Entity.aj : declare the id, version, and name of the table into database.

All this aspects are generated automatically and you can use the methods in these aspects directly from Student java class.

what it means this –testAutomatically: this will ask spring roo to create a junit test for Student entity, that mean that you can test all CRUDs of Student by using mvn test command line or run a junit test from your IDE.

Spring Roo create some files into src/test/java like:

  • StudentIntegrationTest.java: java class for Junit Test.
  • StudentIntegrationTest_Roo_Configurable.aj : configuration of the junit test.
  • StudentIntegrationTest_Roo_IntegrationTest.aj : CRUDs Tests like: testCountStudent(), testFindStudent(), testFlush() , testMergeUpdate() …
  • StudentDataOnDemand.java, StudentDataOnDemand_Roo_DataOnDemand.aj, StudentDataOnDemand_Roo_Configurable.aj : create fake entities of Student to test with it.

Let’s add some field to Student entity by tapping commands:

~.model.Student  roo> field string –fieldName firstname –sizeMin 1 –sizeMax 30 –notNull –column FIRST_NAME

~.model.Student  roo> field string –fieldName lastname –sizeMin 1 –sizeMax 30 –notNull –column LAST_NAME

~.model.Student roo> field email template –fieldName email

~.model.Student roo> field string –fieldName address –column ADDRESS –sizeMax 100

~.model.Student roo> field string –fieldName phonenumber –sizeMin 6 –sizeMax 12 –regexp “^0[1-9]([-. ]?[0-9]{2}){4}$”

Now, you can test your application by using mvn test or run a junit test with your IDE.

but i will show you how to test directly your CRUDs so you can create, update, delete, and list Students from the web.

Let’s setup the web component into our projetc by tapping this:

roo> web mvc setup

roo> web mvc all –package ~.web

the first command set up Spring MVC by creating views and configuration file “webmvc-config.xml”.

the second command create views and controllers for All entity into the package ~.web.

Now lets run our project but how?

Spring Roo setup two web container; jetty and tomcat so with maven command you have only to tap into Linux shell:

  • mvn jetty:run
  • mvn tomcat:run

and we will show the result into the web browser with this url: http://localhost:8080/SpringRooFirstApplication/

So you show that:

now you show that Spring roo have done all work to create a complete application architecture and use Spring MVC like a Presentation Layer.

That’s all for our first tutorial Spring roo and see you in an other tutorial.

Simple Example : Click on button

Explain: The application places a button to occupy the screen. When clicked the button’s text shows current time.

Create new Android Project And use this parameters :

  1. Project name : MyFirstUIAndroid
  2. SDK: Android 2.3 (because it is the most used until today), Note: you can use emulator for Android 4.0.3 to test your application if you need that.
  3. Package Name: tn.insat.houcine
  4. Create Aclivity: ButtonActivity
  5. Minimum SDK: 10

Now edit the XML File named main.xml (location : under res/layout in your project) to add a button into your layout :

 <?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
    android:layout_width=”match_parent”
android:layout_height=”fill_parent”
android:orientation=”vertical” android:gravity=”center”><Button
android:id=”@+id/button1″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”MyFirstButton” />
</LinearLayout>
Because we want to reference this button from our Java code, we need to give it an identifier via the android:id attribute.

  • android:text indicates the initial text to be displayed on the button face (in this case,an empty string).
  • android:layout_width and android:layout_height tell Android to have the button’s width and height fill the “parent“ container, in this case the entire screen or wrap the content that the button need.
Best Practices:
you can change the button name with a link to an attribute into an xml file (located under res/values) that contains only a Strings,Colors, Lists,… because it will centralize the changes of text, title or color into a single place and facilitates the internationalization of your application later.
Now, change the java Class (Named: ButtonActivity) to load the button and add a Click Event to allow the user to chow The date and time after clicking the button.
package tn.insat.houcine;

import java.util.Date;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;public class ButtonActivity extends Activity {
Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
updateTime();
}
});
}
private void updateTime() {
btn.setText(new Date().toString());
}}
Now to test this application right click on the project and select Run As then select Android Application.

User Interfaces Using XML Layouts

The View class represents the basic building block for user interface components.
View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.).
All of the views in a window are arranged in a single tree.
To arrange the views in one window we use different types of Layouts like :

  1. Linear Layout: A LinearLayout is a GroupView that will lay child View elements vertically or horizontally.
  2. Relative Layout: A RelativeLayout is a ViewGroup that allows you to layout child elements in positions relative to the parent or siblings elements.
  3. Table Layout: A TableLayout is a ViewGroup that will lay child View elements into rows and columns.

A brief sample of UI components:

Time / Date picker: 

A DatePicker is a widget that allows the user to select a month, day and year. And a TimePicker is a widget that allows the user to select an hour, minute and second.

Form Controls:
Includes a variety of typical form widgets, like: image buttons, text fields, checkboxes and radio buttons.
you can use other views
like: GalleryView, TabWidget, Spinner, AutoCompleteTextView, ListView, WebView, MapView …
To see all UI components see the palette of the Graphical Layout in the XML Layout located under res/layout (i will say more about the xml layout after that) like that:
What is an XML Layout?
An XML-based layout is a specification of the various UIcomponents (widgets) and the relationships to each other – and to
their containers – all written in XML format.

Android considers XML-based layouts to be resources, and as such layout files are stored in the res/layout directory inside your Android project.
The attributes of the XML elements are properties, describing how a widget should look or how a container should behave.
Example:
If a Button element has an attribute value of
          android:textStyle = “bold”
that means that the text appearing on the face of the button should be rendered in a boldface font style.

Installation and Hello World Android:

To develop in Android you have to Download tow thinks:

  1. Eclipse: to code in java and use the intellisense (Crtl +  space ) to facilitate your work.
  2. Android’s SDK that add  to your environment the emulator in witch you execute the application as if you have a mobile with Android as OS.

First, you have to download eclipse from the official web site, from here. And you have to chose witch version of eclipse for your needs.
For me i have chosen Eclipse IDE for Java EE Dévelopers For Linux. You can chose other version for an other OS.

Second, you have to download the SDK of Android from here.

Now, you have to open unzip the ziped file of eclipse and run the executable into folder named eclipse. And do not forget to unzip the Android SDK if you have download it as a zip or a tgz file.
After choosing your workspace you have to install the plugin of Android into eclipse:

  1. go to help -> install new Software
  2. press Add And enter as a Name ” Android ” and as Location                                                                    ” https://dl-ssl.google.com/android/eclipse/
  3. After same minutes, check all choices And press Next, And accept the licence.
  4. And finally, Click finish to let Eclipse download and install the plugin, After that, eclipse will ask you to restart it so accept that to restart eclipse.

Note: you can use MotoDev (As plugin Eclipse or an application installed like .exe in Windows ) to develop android easier. to know more about MotoDev and download it see here.
Now you are able to see this icons into your eclipse like that:

Now Create new Android project : press File -> New -> Android Project
After that  use this parameters :

  1. Project name : HelloWorldAndroid
  2. SDK: Android 2.3 (because it is the most used until today), Note: you can use emulator for Android 4.0.3 to test your application if you need that.
  3. Package Name: tn.insat.houcine
  4. Create Aclivity: HelloWordActivity
  5. Minimum SDK: 10

Result:

And Android Project consists of:

  1. src : contains all java classes And Activities.
  2. gen : automatically generate
  3. res : contains all resources like layouts, values, images, …

That’s all for this tutorial you will see in other how to use layouts to create window in Android.