jboss developer studio 5.0 getting started guide en us

120
JBoss Developer Studio 5.0 Getting Started Guide Provides information on how the JBoss Developer Studio functions. Edition 5.0.1 JBoss Tools Team JBoss Developer Studio 5.0 Getting Started Guide 1

Upload: es7eban

Post on 18-Apr-2015

120 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: JBoss Developer Studio 5.0 Getting Started Guide en US

JBoss Developer Studio 5.0

Getting Started Guide

Provides information on how the JBoss Developer Studio functions.

Edition 5.0.1

JBoss Tools Team

JBoss Developer Studio 5.0 Getting Started Guide 1

Page 2: JBoss Developer Studio 5.0 Getting Started Guide en US

Legal NoticeCopyright © 2011 Red Hat.

The text of and illustrations in this document are licensed by Red Hat under a Creative CommonsAttribution–Share Alike 3.0 Unported license ("CC-BY-SA"). An explanation of CC-BY-SA is available athttp://creativecommons.org/licenses/by-sa/3.0/. In accordance with CC-BY-SA, if you distribute thisdocument or an adaptation of it, you must provide the URL for the original version.

Red Hat, as the licensor of this document, waives the right to enforce, and agrees not to assert, Section4d of CC-BY-SA to the fullest extent permitted by applicable law.

Red Hat, Red Hat Enterprise Linux, the Shadowman logo, JBoss, MetaMatrix, Fedora, the Infinity Logo,and RHCE are trademarks of Red Hat, Inc., registered in the United States and other countries.

Linux® is the registered trademark of Linus Torvalds in the United States and other countries.

Java® is a registered trademark of Oracle and/or its affiliates.

XFS® is a trademark of Silicon Graphics International Corp. or its subsidiaries in the United Statesand/or other countries.

MySQL® is a registered trademark of MySQL AB in the United States, the European Union and othercountries.

All other trademarks are the property of their respective owners.

2 Legal Notice

Page 3: JBoss Developer Studio 5.0 Getting Started Guide en US

AbstractThe Getting Started Guide explains the JBoss Developer Studio.

JBoss Developer Studio 5.0 Getting Started Guide 3

Page 4: JBoss Developer Studio 5.0 Getting Started Guide en US

Table of ContentsPreface

1. Introduction to JBoss Developer Studio1.1. What JBoss Developer Studio is1.2. Key Features of JBoss Developer Studio and Requirements

2. System Requirements

3. Installing JBoss Developer Studio3.1. Configuring Your Java Environment

3.1.1. Installing and Configuring 32-bit Sun JDK 6 on Linux3.1.2. Installing and Configuring 32-bit Sun JDK 6 on Microsoft Windows

3.2. JBoss Developer Studio Installation3.2.1. Important information before installation3.2.2. Installation

3.3. Automating subsequent installations3.4. Upgrading3.5. Updating JBoss Developer Studio

4. First launch of the JBoss Developer Studio4.1. Welcome4.2. Usage Reporting

4.2.1. Collected usage information guide

5. JBoss Central5.1. Getting Started with JBoss Central5.2. Software installation and updates from within JBoss Central5.3. Project Example Installation

6. JBoss Perspective

7. Setting up a JBoss runtime and managing the server7.1. Adding and configuring a JBoss server runtime7.2. Starting JBoss Server7.3. Stopping the JBoss Server7.4. Server Container Preferences

8. Example of developing a Java EE6, HTML5, Maven application for mobile devices8.1. Purpose and Target Audience8.2. Creating a new Java EE 6 project with Maven8.3. Adding a new entity using Forge8.4. Reviewing persistence.xml and updating import.sql8.5. Optional: Adding a new entity as a POJO8.6. Deployment8.7. Adding a JAX-RS RESTful web service8.8. Adding a jQuery Mobile client application

9. Developing a simple JSP web application9.1. Setting Up the Project9.2. Creating JSP Page

9.2.1. Editing a JSP Page9.2.2. web.xml file9.2.3. Deploying the project

4 Table of Contents

Page 5: JBoss Developer Studio 5.0 Getting Started Guide en US

9.2.4. JSP Page Preview9.2.5. Launch JSP Project

10. Rapid Application Development of a JSF application10.1. Setting up the project10.2. Creating JSP Pages10.3. Creating Transition between two views10.4. Creating Resource File10.5. Creating a Java Bean10.6. Editing faces-config.xml File10.7. Editing the JSP View Files

10.7.1. Editing inputnumber.jsp page10.7.2. Editing success.jsp page

10.8. Creating index.jsp page10.9. Running the Application

11. Uninstalling the JBoss Developer Studio

12. FAQ12.1. What should I do if the Visual Page Editor does not start under Linux12.2. Visual Editor starts OK, but the Missing Natures dialog appears12.3. I have an existing Seam 1.2.1 project. Can I migrate or import the project into a JBossDeveloper Studio Seam project?12.4. I have an existing Struts or JSF project. Can I open the project in JBoss Developer Studio?12.5. Can I import a WAR file?12.6. Is it possible to increase the performance of Eclipse after installing your product?12.7. How can I add my own tag library to the JBoss Tools Palette?12.8. How to get Code Assist for Seam specific resources in an externally generated project?12.9. How to import an example Seam project from jboss-eap directory?12.10. Is a cross-platform project import possible for JBoss Developer Studio?

A. Getting Help and Giving FeedbackA.1. Do You Need Help?A.2. Give us Feedback

B. Revision History

JBoss Developer Studio 5.0 Getting Started Guide 5

Page 6: JBoss Developer Studio 5.0 Getting Started Guide en US

PrefaceThis manual uses several conventions to highlight certain words and phrases and draw attention tospecific pieces of information. To get more information on these conventions please refer to theDocument Conventions manual, which can be found on the Red Hat Documentation website underthe JBoss Developer Studio section.

6 Preface

Page 7: JBoss Developer Studio 5.0 Getting Started Guide en US

Chapter 1. Introduction to JBoss Developer Studio

1.1. What JBoss Developer Studio isJBoss Developer Studio is a set of Eclipse based development tools that have been pre-configured forJBoss Enterprise Middleware Platforms and Red Hat Enterprise Linux. These preconfigured tools savetime and offer significant value, improving productivity and reducing deployment times.

This guide covers the foundation of getting started with JBoss Developer Studio. You will learn how toinstall and configure the software necessary for your OS (currently Linux, Microsoft Windows or MacOSX), including detailed information on how to install, configure and use this toolset, as well as the JavaDevelopment Kit (JDK).

1.2. Key Features of JBoss Developer Studio and RequirementsThe table below lists the main features of JBoss Developer Studio:

JBoss Developer Studio 5.0 Getting Started Guide 7

Page 8: JBoss Developer Studio 5.0 Getting Started Guide en US

Table 1.1. Key Functionality of JBoss Developer StudioFeature Benefit

JBoss Central Your first interaction with the tooling suite, JBoss Central is at theheart ofthe JBoss tooling; providing the latest software updates,news, and project creation.

CDI Tooling Context Dependency Injection (CDI) tooling allows you to createassociated projects and add artifacts to existing projects. Validationand code completion is also included.

Visual Page Editor JBoss Developer Studio includes a Visual Page Editor for combinedvisual and source editing of Web pages. The Visual Page Editoreven renders AJAX-enabled RichFaces components.

AJAX Capabilities JBoss Developer Studio includes a Technology Preview of JBossRichFaces. RichFaces provides nearly 70 skinnable components,including calendar, tree, dropdown menu, drag-and-dropcomponents, and more. JBoss Developer Studio’s Visual PageEditor can render RichFaces components.

JBoss Tools Palette The JBoss Tools Palette contains a developer’s project taglibraries and enables inserting tags into a JSP file with one click.The Tools Palette supports custom and 3rd party tags.

JBoss jBPM Tools JBoss Developer Studio includes jBPM tooling for defining Seampage flows.

Spring Tools JBoss Developer Studio includes Spring IDE for developing Springapplications.

Struts Tools JBoss Developer Studio includes Struts tools for Struts 1.xapplications.

Optimized JBoss ApplicationServer adapter

JBoss Developer Studio’s advanced JBoss AS adapter includesfeatures like incremental deployment for fast development.

Red Hat Enterprise Linux andRHN Access

JBoss Developer Studio includes access to Red Hat EnterpriseLinux, Linux development tools, and Red Hat Network fordevelopment purposes.

Project Examples Tooling for easy installation of Examples.

JBossWeb Service JBoss WS is a web service framework developed as a part of theJBoss Application Server. It implements the JAX-WS specificationthat defines a programming model and run-time architecture forimplementing web services in Java.

Usage reporting The usage plug-in anonymously gathers information on how youuse the provided tools. This information is used to improve andshape the JBoss Developer Studio product to better cater to yourneeds. For more information refer to Section 4.2, “UsageReporting”.

JBoss Developer Studio requires JDK 6 or above. It is important that the architecture of the Java VirtualMachine (JVM) matches the particular JBoss Developer Studio build. For example, if you are using a 64-bit system and use the 64-bit JVM, you need the 64-bit JBoss Developer Studio.

For the 32-bit version of JBoss Developer Studio use the 32-bit JVM or just use -vm flag to tell JBossDeveloper Studio to use 32-bit java if the 64-bit one is found as the default.

8 Chapter 1. Introduction to JBoss Developer Studio

Page 9: JBoss Developer Studio 5.0 Getting Started Guide en US

Note:

On Linux systems with a 64-bit JVM, the visual editor may fail to start. In this case you will needthe libstdc++.so.5 library. See the FAQ for further details.

JBoss Developer Studio 5.0 Getting Started Guide 9

Page 10: JBoss Developer Studio 5.0 Getting Started Guide en US

Chapter 2. System RequirementsJBoss Developer Studio is certified to run on the following platforms:

Microsoft Windows 7 (i386 and x86_64) running 32-bit Sun JDK 6

Microsoft Windows 7 (x86_64) running 64-bit Sun JDK 6 (*)

Microsoft Windows XP (i386 and x86_64) running 32-bit Sun JDK 6

Microsoft Windows XP (x86_64) running 64-bit Sun JDK 6 (*)

MacOS X 10.7 running 32-bit Apple JDK 6

MacOS X 10.7 running 64-bit Apple JDK 6 (*)

MacOS X 10.6 running 32-bit Apple JDK 6

MacOS X 10.6 running 64-bit Apple JDK 6 (*)

Red Hat Enterprise Linux 6 i386 running 32-bit Sun JDK 6 and OpenJDK 6

Red Hat Enterprise Linux 6 x86_64 running 64-bit Sun JDK 6 and OpenJDK 6

Red Hat Enterprise Linux 5 i386 running 32-bit Sun JDK 6 and OpenJDK 6

Red Hat Enterprise Linux 5 x86_64 running 64-bit Sun JDK 6 and OpenJDK 6

(*) The visual component of the Visual Page Editor is not supported in these configurations.

The minimum requirements are:

1 GB RAM

450 MB of hard disk space

The recommended requirements are:

4 GB RAM

1 GB of hard disk space

10 Chapter 2. System Requirements

Page 11: JBoss Developer Studio 5.0 Getting Started Guide en US

Chapter 3. Installing JBoss Developer Studio

3.1. Configuring Your Java EnvironmentYou must have a working installation of JDK before you install JBoss Developer Studio. In this guide wewill show you how to install a 32-bit Sun JDK 6 on a Linux Platform and Microsoft Windows Platform.

3.1.1. Installing and Configuring 32-bit Sun JDK 6 on Linux

To install 32-bit Sun JDK 6 on Linux and configure it, you should follow the next steps:

Download the Sun JDK 6 from Sun's website. Click the Download JDK button for the "JDK SE 6Update <x>" (where "x" is the latest update number) option, select the Linux platform, agree tolicense agreement, and click the Continue button. You can then download the JDK installationpackage.

You may need to set the executable flag on the downloaded file before it can be launched by runningthe chmod +x command, like so:

chmod +x jdk-6u24-linux-i586-rpm.bin

Launch the installation package, and follow the prompts to install Java JDK 6.

If you don't want to use SysV service scripts you can install the "self-extracting file" for Linux insteadof choosing the "RPM in self-extracting" file. In that case you can skip the next step mentioned here.But it is recommended to use the SysV service scripts for production servers.

Download and install the appropriate -compat RPM from JPackage here. Please ensure you choosea matching version of the -compat package to the JDK you've installed.

Create an environment variable that points to the JDK installation directory and call it JAVA_HOME.Add $JAVA_HOME/bin to the system path to be able to run java from the command line. You can dothis by adding the following lines to the .bashrc file in your home directory.

#In this example /usr/java/jdk1.6.0_24 is the JDK installation directory.export JAVA_HOME=/usr/java/jdk1.6.0_24export PATH=$PATH:$JAVA_HOME/bin

Note:

If you have a JDK already installed and added in your system path, you should add $JAVA_HOME/bin before the old $PATH (not after it) so that the new version of JDK can befound first, i. e. export PATH=$JAVA_HOME/bin:$PATH This way, the machine will pick up thenew JVM first. You only need to run "alternative" as a safe guard for the right JVM.

Set this variable for your account doing the installation and also for the user account that will run theserver.

If you have more than one version of JVM installed on your machine, make sure you are using theJDK 1.6 installation as the default java and javac. You can do this using the alternatives system. Thealternatives system allows different versions of Java from different sources to co-exist on yoursystem.

JBoss Developer Studio 5.0 Getting Started Guide 11

Page 12: JBoss Developer Studio 5.0 Getting Started Guide en US

Note

If the JDK is not listed by the alternatives command, you can manually add it with the followingcommands (substitute the location of your JDK for /usr/java/jdk1.6.0_24 whereappropriate):

sudo /usr/sbin/alternatives --install "/usr/bin/java" "java" "/usr/java/jdk1.6.0_24/bin/java" sudo /usr/sbin/alternatives --install "/usr/bin/javac" "javac" "/usr/java/jdk1.6.0_24/bin/javac" sudo /usr/sbin/alternatives --install "/usr/lib/jvm/java-1.6.0" "java_sdk_1.6.0" "/usr/java/jdk1.6.0_24" 1

3.1.1.1. Select alternatives for java, javac and java_sdk_1.6.0

As a root user, type the following command at the shell prompt and you should see something likethis:

sudo /usr/sbin/alternatives --config java

There are 3 programs which provide 'java'.

Selection Command----------------------------------------------- 1 /usr/lib/jvm/jre-1.5.0-gcj/bin/java* 2 /usr/lib/jvm/jre-1.6.0-openjdk.x86_64/bin/java + 3 /usr/java/jdk1.6.0_24/bin/java

Enter to keep the current selection[+], or type selection number:

Make sure the Sun version [/usr/java/jdk1.6.0_24/bin/java in this case] is selected (marked with a '+' inthe output), or select it by entering its number as prompted.

Repeat the same for javac and java_sdk_1.6.0.

sudo /usr/sbin/alternatives --config javac

There are 2 programs which provide 'javac'.

Selection Command-----------------------------------------------* 1 /usr/lib/jvm/java-1.6.0-openjdk.x86_64/bin/javac + 2 /usr/java/jdk1.6.0_24/bin/javac

Enter to keep the current selection[+], or type selection number:

sudo /usr/sbin/alternatives --config java_sdk_1.6.0

There are 2 programs which provide 'java_sdk_1.6.0'.

Selection Command-----------------------------------------------* 1 /usr/lib/jvm/java-1.6.0-openjdk.x86_64 + 2 /usr/java/jdk1.6.0_24

Enter to keep the current selection[+], or type selection number:

12 Chapter 3. Installing JBoss Developer Studio

Page 13: JBoss Developer Studio 5.0 Getting Started Guide en US

You should verify that java, javac and java_sdk_1.6.0 all point to the same manufacturer and version.

Note:

You can always override this step by setting the JAVA_HOME environment variable as explainedin the previous step.

Make sure that the java executable is in your path and that you are using an appropriate version. Toverify your Java environment, type java -version at the shell prompt and you should seesomething like this:

java -versionjava version "1.6.0_24"Java(TM) SE Runtime Environment (build 1.6.0_24-b07)Java HotSpot(TM) Server VM (build 19.1-b02, mixed mode)

3.1.2. Installing and Configuring 32-bit Sun JDK 6 on Microsoft Windows

To install and configure 32-bit Sun JDK 6 on Microsoft Windows, follow these steps:

Download the Sun JDK 6 from Oracle's website. Choose "JDK 6 Update <x>" (where "x" is the latestupdate number) for download and then select your Windows Platform options to perform theinstallation.

Create an environment variable called JAVA_HOME that points to the JDK installation directory, forexample: C:\Program Files\Java\jdk1.6.0_24\

In order to run java from the command line, add the jre\bin directory to your path, for example:

C:\Program Files\Java\jdk1.6.0_24\jre\bin

To do this, open the Control Panel from the Start menu, switch to Classic View if necessary, openthe System Control Panel applet (System ), select the Advanced Tab, and click on the EnvironmentVariables button.

Now, when 32-bit Sun JDK 6 has been successfully installed, we can pass on to the next step.

3.2. JBoss Developer Studio Installation

3.2.1. Important information before installation

The Visual Page Editor tooling is only available in the 32-bit versions of the JBoss Developer Studiofor Windows and Mac OS X. You may still run your application servers in 64-bit virtual machines (ifneeded), to insure compatibility with the production environment.

If you are installing on a Linux distribution you must set the following limits in /etc/security/limits.conf upon installation completion and before running JBoss DeveloperStudio:

* soft nofile 9216* hard nofile 9216

These settings will ensure you do not encounter an error regarding maximum open files whenstarting JBoss Developer Studio.

A GUI interface is also necessary for JBoss Developer Studio to run correctly. Either Gnome or KDEare acceptable.

JBoss Developer Studio 5.0 Getting Started Guide 13

Page 14: JBoss Developer Studio 5.0 Getting Started Guide en US

are acceptable.

When installing JBoss Developer Studio on the 64-bit edition of MacOS X Snow Leopard or Lion, onlythe 64-bit edition of JBoss Developer Studio is supported. Though the 32-bit edition will install, manyfeatures will not work correctly due to Java 1.6 being specific to 64-bit applications on Leopard andJBoss Developer Studio requiring Java 1.6 for all features to function.

3.2.2. Installation

JBoss Developer Studio comes with a universal installer, allowing you to use the same file to install onLinux, Windows and Max OS X operating systems. The following steps show you how to install JBossDeveloper Studio:

You can download the universal installer from here the Red Hat Customer Portal.

Run the following command in a terminal window:

java -jar jbdevstudio-product-universal-5.0.0.GA.jar

Follow the instructions presented by the installation wizard:

Figure 3.1. JBoss Developer Studio Installation Wizard

Accept the license agreement.

14 Chapter 3. Installing JBoss Developer Studio

Page 15: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 3.2. Accepting the license agreement

Provide the installation path

Figure 3.3. Set installation path

Select the appropriate Java Virtual Machine

Tip:

By selecting Default Java VM you set the default Java VM of your system (to verify your Javaenvironment, type "java -version" in console).

Selecting the Specific Java VM option will allow you to provide the path to a specific Java VM installation.

JBoss Developer Studio 5.0 Getting Started Guide 15

Page 16: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 3.4 . Select Java VM

This step lets you select a location that will be used to search for locally available JBoss servers uponfirst running of JBoss Developer Studio:

Figure 3.5. Selecting a location to search for JBoss servers

To add a location click on the Add button. When the Add button is clicked you are prompted to selectthe folder from which to search for available JBoss Application Servers. Click the OK button to select thechosen location. This location and subsequent directories will be searched for any JBoss serverinstances when you launch JBoss Developer Studio.

Click the Next button to bring up a summary page where you can check your installation paths and seethe components that will be installed. If you'd like to change something, press the Previous button.Click the Next button to start installation.

16 Chapter 3. Installing JBoss Developer Studio

Page 17: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 3.6. Summary Information

Installation will then take place.

Figure 3.7. Installation

Once installation completes you will have the option to create shortcuts to JBoss Developer Studio.

JBoss Developer Studio 5.0 Getting Started Guide 17

Page 18: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 3.8. Shortcut creation

The final screen will declare that Installation has completed successfully. Click the Donebutton.

Figure 3.9. Completion

3.3. Automating subsequent installationsParameters for automated installation are defined in the InstallConfigRecord.xml file. Theseparameters include the path to the installation folder, the packages to be installed and the shortcuts tobe created.

Within the InstallConfigRecord.xml file is three parameters that can be modified for specificinstallation options:

18 Chapter 3. Installing JBoss Developer Studio

Page 19: JBoss Developer Studio 5.0 Getting Started Guide en US

AutomatedInstallation/com.jboss.jbds.installer.PathInputPanel/installpath: Installation folder

AutomatedInstallation/com.jboss.jbds.installer.JBossAsSelectPanel/installgroup: jbds

com.izforge.izpack.panels.ShortcutPanel: Shortcut menu for installation

Note

If you change the installation folder path, you must update thecom.izforge.izpack.panels.ShortcutPanel parameter to reflect the new path.

Once you have configured the InstallConfigRecord.xml file for your needs, the followingcommand can be run on the command line to begin installation:

java -jar /path/to/installer.jar /path/to/InstallConfigRecord.xml

Current limitations

The following limitations currently exist when using the automated method of installation:

Desktop shortcuts are not supported.

Creation of shoortcuts for all users on a system has not been tested.

Automated installation does not check if the specified installation path already exists. If the path doesexist, any files will be overwritten without notice.

The Java Virtual Manchine to be used with JBoss Developer Studio is automatically chosen andcannot be specified.

No verification exists to ensure the installer you are using is correct for your platform.

Warning

Automated installation does not check if the specified installation path already exists. If the pathdoes exist, any files will be overwritten without notice.

An example of an InstallConfigRecord.xml file is available below:

JBoss Developer Studio 5.0 Getting Started Guide 19

Page 20: JBoss Developer Studio 5.0 Getting Started Guide en US

<?xml version="1.0" encoding="UTF-8" standalone="no"?><AutomatedInstallation langpack="eng"> <com.jboss.jbds.installer.HTMLInfoPanelWithRootWarning id="introduction"/> <com.izforge.izpack.panels.HTMLLicencePanel id="licence"/> <com.jboss.jbds.installer.PathInputPanel id="target"> <installpath>/opt/local/JBDS5/GA</installpath> </com.jboss.jbds.installer.PathInputPanel> <com.jboss.jbds.installer.JREPathPanel id="jre"/> <com.jboss.jbds.installer.JBossAsSelectPanel id="as"> <installgroup>jbds</installgroup> </com.jboss.jbds.installer.JBossAsSelectPanel> <com.jboss.jbds.installer.UpdatePacksPanel id="updatepacks"/> <com.jboss.jbds.installer.DiskSpaceCheckPanel id="diskspacecheck"/> <com.izforge.izpack.panels.SummaryPanel id="summary"/> <com.izforge.izpack.panels.InstallPanel id="install"/> <com.jboss.jbds.installer.CreateLinkPanel id="createlink"> <jrelocation>/usr/lib/jvm/java-1.6.0-sun-1.6.0.30.x86_64/jre/bin/java</jrelocation> </com.jboss.jbds.installer.CreateLinkPanel> <com.izforge.izpack.panels.ShortcutPanel id="shortcut"/> <com.jboss.jbds.installer.ShortcutPanelPatch id="shortcutpatch"/> <com.izforge.izpack.panels.SimpleFinishPanel id="finish"/></AutomatedInstallation>

3.4. UpgradingBecause JBoss Developer Studio 4.0x and 5.0 use different versions of Eclipse, it is not possible todirectly upgrade from an older version to JBoss Developer Studio 5.0.

Note

It is possible to run JBoss Developer Studio 4.0x and 5.0 side by side, as long as they have beeninstalled into separate directories.

3.5. Updating JBoss Developer StudioUpdates to JBoss Developer Studio can be installed by selecting Help → Check For Updates fromwithin Eclipse.

20 Chapter 3. Installing JBoss Developer Studio

Page 21: JBoss Developer Studio 5.0 Getting Started Guide en US

Chapter 4. First launch of the JBoss Developer Studio

4.1. WelcomeThe welcome page is the first page you see when you first launch JBoss Developer Studio.

Figure 4 .1. Welcome to JBoss Developer Studio

Click Get started with JBoss Central to begin using JBoss Developer Studio.

4.2. Usage ReportingThe JBoss Developer Studio now includes a usage plug-in that anonymously reports information back toJBoss. The plug-in is not enabled by default. To enable, click the Yes button.

Figure 4 .2. Usage plug-in pop-up

Once enabled, the plug-in will remain active until turned off. To turn the active plug-in off, navigate toWindow → Preferences → JBoss Tools → Usage Reporting.

The gathered data allows JBoss to see how the tools are being used and where they are being usedgeographically. Currently we are looking into the operating systems being used, screen resolution and

JBoss Developer Studio 5.0 Getting Started Guide 21

Page 22: JBoss Developer Studio 5.0 Getting Started Guide en US

how often the tooling environment is started. In the future geographic information will assist in focusingtranslation resources to areas where the developer environment is most used.

The plug-in uses Google Analytics to track and report data by acting as if you were visiting the sitehttp://jboss.org/tools/usage/. To view the type of information being collected, refer to Section 4.2.1,“Collected usage information guide”.

To view the source code of the usage plug-in visithttp://anonsvn.jboss.org/repos/jbosstools/trunk/usage/.

4 .2.1. Collected usage information guide

Below you will find an outline of the information that is reported and the Goolge Analytics fields that areused to gather this information.

Version

The Content field has been modified to report the installed JBoss Developer Studio version.Sample returned values include: jbdevstudio-linux-gtk-x86_64-4.0.0.v201009301221R-H20-Beta1.jar and jbdevstudio-linux-gtk-3.0.2.v201009161622R-H138-GA.jar.

Installed components

The Keyword field has been modified to report the installed JBoss Developer Studiocomponents. Sample returned values include: JBoss AS, Drools, Teiid and ModeShape.

Visitor type

The Visitor type field reports if the current user is new or returning.

Language

The Language field reports the localized language the product is being used in. Samplereturned values include: en-US, de-DE and fr-FR.

Location fields

The location fields report the geographical location where the product is being used based onthe continent, country and city. Sample returned values include: Europe (continent), Germany(country) and Munich (city).

Eclipse interface and version

The Browser field has been modified to report the Eclipse interface and version being used.Sample returned values include: JBoss Developer Studio: 5.0.0 and JBoss Developer Studio:5.0.1.

Operating System

The Operating System field reports the Operating System and its version that the productis running on (with Linux distribution version reporting conducted through the User DefinedValue field). Sample returned values include: Linux, Macintosh 10.6, Macintosh 10.7 andWindows 7.

22 Chapter 4. First launch of the JBoss Developer Studio

Page 23: JBoss Developer Studio 5.0 Getting Started Guide en US

Linux distribution version

The User Defined Value field reports the distribution and version of Linux, if one is beingused as the Operating System. Sample returned values include: Red Hat Enterprise Linux 6 andFedora 16

Screen colors

The Screen colors field reports the color depth being used. Sample returned valuesinclude: 32-bit and 24-bit.

Screen resolution

The Screen resolution field reports the resolution being used. Sample returned valuesinclude: 2048x1536 and 1920x1080.

Java version

The Flash version field has been modified to report the Java version used. Samplereturned values include: 1.6.0_20.

Connection speed

The Connection speed field reports the type of internet connection being used. Samplereturned values include: T1, Cable and DSL.

JBoss Central Enabled

The JBoss Central Enabled field reports whether JBoss Central is set to be seen uponstartup or not. Returned value is either true or false.

JBoss Developer Studio 5.0 Getting Started Guide 23

Page 24: JBoss Developer Studio 5.0 Getting Started Guide en US

Chapter 5. JBoss CentralWhen viewing the workbench for the first time you will be greated with JBoss Central. JBoss Centralassists you in getting started and keeps you up to date with JBoss technologies.

Figure 5.1. JBoss Central

5.1. Getting Started with JBoss CentralThe Getting Started tab of JBoss Central is the first tab you will see, and is setup as a functionalstarting point for developers.

At the top of the tab is a search box. By clicking the down arrow to the left you can select to perform asearch on either the Red Hat Customer Portal or the JBoss Community.

Figure 5.2. Search Options

Performing a search will launch a web browser as a new tab, displaying the results page. You caninteract with the browser as you would any other web browser.

24 Chapter 5. JBoss Central

Page 25: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 5.3. Search Results

From the Create Projects section you can create a Dynamic Web Project, OpenShiftExpress Application, Java EE Web Project, Java EE Project, HTML5 Project, SpringMVC Project, RichFaces Project, GWT Web Project, or any one of many ProjectExamples. To access a complete list of projects you can create, click on the window icon at the top-

right of the Create Projects section .

Note

All wizards generate Maven based projects, except for the Dynamic Web Project wizard.

Figure 5.4 . Creating a Project

GWT (Google Web Toolkit) Web Project creation

To create a GWT web project, the latest m2e-wtp and Google plug-ins have to be installed. If these plug-ins have not been previously installed, the first page of the GWT web project wizard will provide you withthe ability to install the plug-ins.

JBoss Developer Studio 5.0 Getting Started Guide 25

Page 26: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 5.5. GWT Web Project creation: plug-in page

Under the Project Examples section you can expand and view JBoss Quickstarts. Eachquickstart is an example to assist first time users.

Figure 5.6. Project Examples

For an in-depth look into the installation of project examples go to Section 5.3, “Project ExampleInstallation”.

You can also download other examples and install and set runtime preferences through the Project

Examples section by using the five buttons at the top-right. The first button launches a NewProject Examples wizard. Here you can search and download project examples to assist you with

26 Chapter 5. JBoss Central

Page 27: JBoss Developer Studio 5.0 Getting Started Guide en US

getting started.

Figure 5.7. New Project Examples wizard

By clicking on the second button you will be taken directly to the JBoss Tools RuntimeDetection dialog within Preferences.

JBoss Developer Studio 5.0 Getting Started Guide 27

Page 28: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 5.8. JBoss Tools Runtime Detection

The third button takes you directly to the Server Runtime Environments preferences dialog.

28 Chapter 5. JBoss Central

Page 29: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 5.9. Server Runtime Environments preferences

The final button refreshes the Project Examples list.

The Documentation section of JBoss Central contains links to help materials such as referenceguides, user forums and documentation concerning new features.

Figure 5.10. Documentation links

If you do not wish to see JBoss Central at every startup, you can deselect the checkbox Show onStartup in the Settings section.

JBoss Developer Studio 5.0 Getting Started Guide 29

Page 30: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 5.11. Sett ings section

The News and Blogs sections are updated automatically, bringing you the latest information concerningJBoss technologies.

Figure 5.12. News and Blogs sections

Note

After using JBoss Central once, the current content will be available offline. This includes projectexample archives and descriptors, maven artifacts, and news and blog feeds.

5.2. Software installation and updates from within JBoss CentralThe Software/Update tab at the bottom of JBoss Central switches to a screen where you can installand update your software.

30 Chapter 5. JBoss Central

Page 31: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 5.13. Software/Update tab

Warning

Some software available through this tab is third-party community software that Red Hat has nocontrol over. Installing third-party software may impact the level of support that Red Hat canprovide.

To install or update software, select the checkbox beside one or more components.

Figure 5.14 . Software/Update tab

Once you have selected all the components to update or install, click the button at thebottom left of the tab window. A screen will then appear offering you the option of deselecting a part ofthe component if you wish.

JBoss Developer Studio 5.0 Getting Started Guide 31

Page 32: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 5.15. Component selection

You will then be asked to confirm the selection of components to install. To accept the selection pressthe Next button. You can change the components by pressing the Back button.

Figure 5.16. Component verification

After pressing the Next button you will need to accept the terms of the license agreementfor the associated software being installed. To do this select the corresponding radio button.

Click Finish to begin installation.

32 Chapter 5. JBoss Central

Page 33: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 5.17. Licenses review

As the installation process takes place you can choose to have further details displayed or run theinstallation in the background.

Figure 5.18. Licenses review

After the software has been downloaded, you will be asked to verify the installation of the components ifthe contents are unsigned. Install unsigned third-party components with discretion.

JBoss Developer Studio 5.0 Getting Started Guide 33

Page 34: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 5.19. Installing unsigned content

Once installation is complete, restart JBoss Developer Studio by pressing the Restart Now button thatappears.

Figure 5.20.

5.3. Project Example InstallationTo install an example from within JBoss Central, navigate to the Project Examples section and selectan example from the list.

Figure 5.21. Project Examples

This section will guide you through the installation of the Helloworld example.

Click on the Helloworld item in the Quickstarts list.

An installation wizard for the example will open and automatically search for required components onyour system. If a particular component is not found (a plug-in or a required version of the JBoss server),the component will be automatically downloaded for you during example installation.

34 Chapter 5. JBoss Central

Page 35: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 5.22. Project Examples

Click the Next button.

You will be asked to define the install location for the example. Your curent workspace will be selected bydefault.

JBoss Developer Studio 5.0 Getting Started Guide 35

Page 36: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 5.23. Project Examples

Click the Finish button to begin installation of the example.

Once the example has been installed it will appear in the list of projects within your ProjectExplorer.

36 Chapter 5. JBoss Central

Page 37: JBoss Developer Studio 5.0 Getting Started Guide en US

Chapter 6. JBoss PerspectiveThe JBoss perspective is designed to incorporate the views most often used by developers usingJBoss, while also changing standard menu items to reflect what a JBoss devleoper would want andneed.

Figure 6.1. JBoss Perspective

The JBoss perspective views include: Project Explorer, Outline, Palette, Properties andServers.

Certain menus also see a change in available items. These menus include File → New, Window →Show View and the context menu for a project.

A selection of the menu items for File → New can be seen in Figure 6.2, “JBoss Perspective: File →New”.

Figure 6.2. JBoss Perspective: File → New

A selection of the menu items for Window → Show View can be seen in Figure 6.3, “JBoss Perspective:Window → Show View ”.

JBoss Developer Studio 5.0 Getting Started Guide 37

Page 38: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 6.3. JBoss Perspective: Window → Show View

A selection of the menu items for the context menu of a project can be seen in Figure 6.4, “JBossPerspective: Context Menu → New ”.

38 Chapter 6. JBoss Perspective

Page 39: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 6.4 . JBoss Perspective: Context Menu → New

JBoss Developer Studio 5.0 Getting Started Guide 39

Page 40: JBoss Developer Studio 5.0 Getting Started Guide en US

Chapter 7. Setting up a JBoss runtime and managing theserverAlthough JBoss Developer Studio works closely with JBoss Enterprise Application Platform 6 we do notultimately tie you to any particular server for deployment. There are some servers that Studio supportsdirectly (via the bundled Eclipse WTP plug-ins). In this chapter we discuss how to manage a JBossserver.

Note

This chapter assumes you have a JBoss application server installed on your system. If you donot, consult the installation instructions that accompanied your server.

7.1. Adding and configuring a JBoss server runtimeSelect the Servers view by navigating to Window → Show View → Other → Server → Servers.

Click the new server wizard text in the Servers view or if this is not your first server, right-clickanywhere in the view and select New → Server.

Select the server option that matches your installed server.

Figure 7.1. Selecting Server Name and Server Type

To setup the new runtime, click the Next button.

In the next dialog verify the specified information and set local or remote server information.

40 Chapter 7. Setting up a JBoss runtime and managing the server

Page 41: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 7.2. JBoss Runtime Summary

Lastly, a screen will appear that will allow you yo modify projects that are to be configured for theserver. Click the Finish button.

JBoss Developer Studio 5.0 Getting Started Guide 41

Page 42: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 7.3. Configuring Projects

A new JBoss Server should now appear in the Servers view.

Figure 7.4 . New JBoss Server

7.2. Starting JBoss ServerStarting JBoss Server is quite simple. JBoss Developer Studio allows you to control its behavior with thehelp of a special toolbar, where you could start it in a regular or debug mode, stop it or restart it.

To launch the server click the green-with-white-arrow icon in the Servers view or right click servername in this view and click the Start button. If this view is not open, select Window → Show View→ Other → Server → Servers

Figure 7.5. Starting from Icon

42 Chapter 7. Setting up a JBoss runtime and managing the server

Page 43: JBoss Developer Studio 5.0 Getting Started Guide en US

While launching, server output is written to the Console view:

Figure 7.6. Console Output

When the server is started you should see Started in the square brackets right next its name in theServers view.

Figure 7.7. Server is Started

7.3. Stopping the JBoss ServerTo stop the server, click the Stop button icon in Servers or right click the server name and press Stop.

Figure 7.8. Stopping Server

When the server is stopped you will see Stopped in the square brackets next to its name.

7.4. Server Container PreferencesYou can control how JBoss Developer Studio interacts with server containers in the Server editor.Double-click the server to open it in the editor.

JBoss Developer Studio 5.0 Getting Started Guide 43

Page 44: JBoss Developer Studio 5.0 Getting Started Guide en US

Here you can specify some common settings: host name, server name, runtime as well as settingsrelated to publishing, timeouts and server ports.

44 Chapter 7. Setting up a JBoss runtime and managing the server

Page 45: JBoss Developer Studio 5.0 Getting Started Guide en US

Chapter 8. Example of developing a Java EE6, HTML5, Mavenapplication for mobile devices

8.1. Purpose and Target AudienceWe illustrate here how to bring together the best features of Java EE 6, HTML5 and JBoss to create arich, mobile-optimized and dynamic application https://github.com/jboss/ticket-monster.

Through this tutorial, you will:

Create a Java EE 6 project via a Maven archetype

Leverage Maven to Eclipse infrastructure (m2e) and m2e-wtp

Create a JPA entity using Forge

Utilize Hibernate Tools

Generate a database schema

Deploy an application to a local JBoss Server

Add a JAX-RS endpoint to an application

Add a jQuery Mobile client

Test the application for mobile compatibility with BrowserSim

Figure 8.1. JBoss Developer Studio 5 with Mobile BrowserSim

8.2. Creating a new Java EE 6 project with MavenSwitch to the JBoss Perspective and click on the JBoss logo in the toolbar to launch JBoss Central, if it

JBoss Developer Studio 5.0 Getting Started Guide 45

Page 46: JBoss Developer Studio 5.0 Getting Started Guide en US

is not already viewable.

Select Java EE Web Project from JBoss Central, under Create Projects. This archetype provides aMaven-based project that can be built outside of the IDE, in continuous integration solutions like Hudsonand Jenkins.

Figure 8.2. JBoss Central

Once Java EE Web Project has been selected you will be prompted with a dialog box. If you are in abrand new workspace, the application server will not be configured and the checkbox on the serverruntime row will not be checked.

46 Chapter 8. Example of developing a Java EE6, HTML5, Maven application for mobile devices

Page 47: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.3. New Project Wizard

If you have not downloaded an application server, you can download and install a community server byclicking on the Download and Install button. To add a JBoss server you have downloaded, click on theInstall button.

Selecting Install will launch the JBoss Runtime Detection section of Preferences.

JBoss Developer Studio 5.0 Getting Started Guide 47

Page 48: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.4 . JBoss Tools Runtime Detection

Select the Add button and select the main directory of your unzipped JBoss server.

Figure 8.5. Runtime Open Dialog

48 Chapter 8. Example of developing a Java EE6, HTML5, Maven application for mobile devices

Page 49: JBoss Developer Studio 5.0 Getting Started Guide en US

Select Open and runtime detection will begin.

Figure 8.6. Searching for runtimes window

Select OK.

Once runtime detection is complete, you will see the server added to the Paths list.

JBoss Developer Studio 5.0 Getting Started Guide 49

Page 50: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.7. JBoss Tools Runtime Detection Completed

Select OK to close the Preferences Dialog

Returning to the New Project Example dialog, it will now have the JBoss application server checkboxselected.

50 Chapter 8. Example of developing a Java EE6, HTML5, Maven application for mobile devices

Page 51: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.8. JBoss application server found

Select Next

JBoss Developer Studio 5.0 Getting Started Guide 51

Page 52: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.9. New Project Wizard Step 2

The default Project name is jboss-javaee6-webapp and the Target Runtime will be empty. If youdecide to target a specific Enterprise Application Platform, you will first need to configure the enterpriseMaven repositories. Targetting a runtime and configuring Maven repositories is beyond the scope of thisguide, however the JBoss Web Framework Kit Maven Repository User Guide available throughhttp://docs.redhat.com/docs/en-US/ would assist in your Maven repository installation.

Select Finish and the project will be generated, and imported into your workspace.

52 Chapter 8. Example of developing a Java EE6, HTML5, Maven application for mobile devices

Page 53: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.10. New Project Wizard Step 3

Select Finish

8.3. Adding a new entity using ForgeRight-click on the org.jboss.tools.examples.model package in the Project Explorer and select Show InForge Console .

Figure 8.11. Show In Forge Console

You will be prompted with a dialog indicating Forge Not Running; select Yes

JBoss Developer Studio 5.0 Getting Started Guide 53

Page 54: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.12. Show Forge Not Running

If you are not prompted you can start Forge using the green arrow in the Forge Console tab.

Figure 8.13. Forge Console

Forge allows you to interactively type commands that generate code and update the IDE. You cancomplete commands via the Tab key.

The overall commands are as follows:

entity --named Event --package org.jboss.tools.examples.model

field string --named name

validation setup

constraint NotNull --onProperty name

constraint Size --onProperty name --min 5 --max 50 --message "Must be > 5 and < 50"

field string --named description

constraint Size --onProperty description --min 20 --max 1000 --message "Must be > 20 and < 1000"

field boolean --named major

field string --named picture

At the line [jboss-jbossee6-webapp] model $ type in en and hit the tab key on your keyboard. entitywill fill in. Hit tab again and entity --named will appear. Type in Event and add a space, Forge cannot anticipate the name of your new entity. Hit tab again and select --package. Now, hit tab 5 times tofill in org.jboss.tools.examples, and since there are multiple entries underneath examples, Forge

54 Chapter 8. Example of developing a Java EE6, HTML5, Maven application for mobile devices

Page 55: JBoss Developer Studio 5.0 Getting Started Guide en US

will display those options. Type in m and hit tab to select model.

Press the Enter key to watch the command execute.

Figure 8.14 . Forge new entity

The Event entity will be generated and placed into the model package. The entity will then be opened inthe editor.

Figure 8.15. Event Entity

Note

The "@Entity public class" line is generated on the same physical line as "importjava.lang.Override".

At the Forge prompt, you will notice that you have been switched into Event.java

Type ls, providing a list of the fields and methods.

JBoss Developer Studio 5.0 Getting Started Guide 55

Page 56: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.16. Forge ls

Now that the base Event entity has been created, add the fields and their JSR 303 Bean Validationconstraints.

This next step involves adding a name property for the Event entity.

Type fie and hit tab to fill in field, if you hit tab again, Forge will list out the possible field types. Typein s and hit tab, Forge will respond with string. Hit tab again to get --named and type in name. Theresulting command should be field string --named name and then press the Enter key.

This will add a private String name member variable and the appropriate get/set methods. You shouldalso notice that the toString method is tweaked to include name as well.

Figure 8.17. @Column name

The remaining Forge commands are:

validation setup

constraint NotNull --onProperty name

constraint Size --onProperty name --min 5 --max 50 --message "Must be > 5 and < 50"

field string --named description

constraint Size --onProperty description --min 20 --max 1000 --message "Must be > 20 and < 1000"

field boolean --named major

field string --named picture

Use the Outline view to see the results of Forge on the Event.java JPA entity. The view is on the right-side when using the JBoss Perspective.

56 Chapter 8. Example of developing a Java EE6, HTML5, Maven application for mobile devices

Page 57: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.18. Outline View

8.4. Reviewing persistence.xml and updating import.sqlBy default, with the way the jboss-javaee6-webapp project is configured, the entity classes becomethe database schema. persistence.xml. Open up the persistence.xml file and review its settings. It isstill under src/main/resources/META-INF. The two key settings are the <jta-data-source> andhibernate.hbm2ddl.auto. The datasource setting must map to one already established in thesrc\main\webapp\jboss-javaee6-webapp–ds.xml descriptor file. The hibernate.hbm2ddl.auto=create-dropproperty indicates that missing tables & columns will be created or updated inside the database schemaitself. Open up the import.sql file, this file contains INSERT statements that will inject sample data intoyour initial database structure. Add the following insert statements:

insert into Event (id, name, description, major, picture, version) values (1,'Shane''s Sock Puppets','This critically acclaimed masterpiece...',true,'http://dl.dropbox.com/u/65660684/640px-Carnival_Puppets.jpg',1);

JBoss Developer Studio 5.0 Getting Started Guide 57

Page 58: JBoss Developer Studio 5.0 Getting Started Guide en US

insert into Event (id, name, description, major, picture, version) values (2,'Rock concert of the decade','Get ready to rock...',true,'http://dl.dropbox.com/u/65660684/640px-Weir%2C_Bob_(2007)_2.jpg',1);

8.5. Optional: Adding a new entity as a POJORight-click on the org.jboss.tools.examples.model package and select New → Class. Enter theclass name as Venue.

Add the private variables representing the columns in the Venue table.

package org.jboss.tools.examples.model;

public class Venue { private Long id; private String name; private String description; private int capacity;}

Right-click on the editor, and from the context menu, select Source → Generate Getters andSetters.

58 Chapter 8. Example of developing a Java EE6, HTML5, Maven application for mobile devices

Page 59: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.19. Generate Getters and Setters Menu

This will create getX and setX methods for all private variables, making them accessible by the entityclass.

Figure 8.20. Generate Getters and Setters Dialog

Select All and OK

JBoss Developer Studio 5.0 Getting Started Guide 59

Page 60: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.21. Venue.java with gets/sets

Right-click on the editor and from the context menu select Source → Generate Hibernate/JPAAnnotations.

If you have not yet saved Venue.java, you will prompted to do so.

60 Chapter 8. Example of developing a Java EE6, HTML5, Maven application for mobile devices

Page 61: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.22. Save Modified Resources

The Hibernate: add JPA annotations wizard will launch. Ensure that Venue is the class you areworking on.

JBoss Developer Studio 5.0 Getting Started Guide 61

Page 62: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.23. Hibernate: add JPA annotations

Select Next. The next step in the wizard will provide a sampling of the refactored sources. The changesbeing made to your Venue class will be detailed.

62 Chapter 8. Example of developing a Java EE6, HTML5, Maven application for mobile devices

Page 63: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.24 . Hibernate: add JPA annotations Step 2

Click Finish

You can now enter the Bean Validation annotations ( for example, @NotNull) for each variable.

8.6. DeploymentEnsure that you have installed the h2console.war file into the deployments directory. The WAR filecan be copied from the quickstarts directory. You can find the enterprise quickstarts in the CustomerPortal, or the unsupported community quickstarts can be downloaded fromhttp://www.jboss.org/jbossas/downloads/.

JBoss Developer Studio 5.0 Getting Started Guide 63

Page 64: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.25. Quickstarts Directory Layout

Drag and drop the h2console.war file into deployments. Notice the .deployed files, if an erroroccurs you should see a .failed file instead.

Figure 8.26. h2console.war in deployments

To deploy the project, right click on the project name in the Project Explorer and from the contextmenu select Run As → Run on Server. If needed, this will start the application server instance, compile,build and push the application into the JBoss/standalone/deployments directory.

64 Chapter 8. Example of developing a Java EE6, HTML5, Maven application for mobile devices

Page 65: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.27. Run As → Run on Server

By deploying the project in this way, the internal web browser will also be launched with the appropriateURL for the application.

JBoss Developer Studio 5.0 Getting Started Guide 65

Page 66: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.28. Web browser interaction

With a web browser, navigate to http://localhost:8080/h2console

66 Chapter 8. Example of developing a Java EE6, HTML5, Maven application for mobile devices

Page 67: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.29. h2console in browser

Enter jdbc:h2:mem:jboss-javaee6-webapp in as the JDBC URL. This file is found in src\main\webapp\WEB-INF\

Set the password to sa and click Connect.

Your Event has now been dynamically added into the H2 schema.

To verify that your new EE 6 JPA entity called Event has been added to the system, deployedsuccessfully and created the supporting RDBMS schema, use the SQL statement: select * from event and press the Run button. The data you previously entered in the import.sql file will bedisplayed.

JBoss Developer Studio 5.0 Getting Started Guide 67

Page 68: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.30. h2console Select * from Event

8.7. Adding a JAX-RS RESTful web serviceYou will now create a POJO with the JAX-RS annotations that will become an endpoint, providing accessto the Event data.

Right-click on the org.jboss.tools.examples.rest package, and from the context menu selectNew → Class. Input EventService as the class name.

68 Chapter 8. Example of developing a Java EE6, HTML5, Maven application for mobile devices

Page 69: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.31. New Class EventService

Click Finish.

The new EventService.java file will now be opened in the Java editor.

Copy the following code into the EventService.java file:

JBoss Developer Studio 5.0 Getting Started Guide 69

Page 70: JBoss Developer Studio 5.0 Getting Started Guide en US

package org.jboss.tools.examples.rest;

@Path("/events")@RequestScopedpublic class EventService { @Inject private EntityManager em;

@GET @Produces(MediaType.APPLICATION_JSON) public List<Event> getAllEvents() { final List<Event> results = em.createQuery( "select e from Event e order by e.name").getResultList(); return results; }}

This code returns all Events for the JAX-RS endpoint.

Figure 8.32. EventService after Copy and Paste

70 Chapter 8. Example of developing a Java EE6, HTML5, Maven application for mobile devices

Page 71: JBoss Developer Studio 5.0 Getting Started Guide en US

Still in the Java editor, right-click and from the context menu select Source → Organize Imports.

Figure 8.33. Source → Organize → Imports

Some of the class names are not unique. You will be prompted with any decisions for an intended class.Select the following:

javax.ws.rs.core.MediaType

org.jboss.tools.examples.model.Event

javax.ws.rs.Produces

java.util.List

java.inject.Inject

java.enterprise.context.RequestScoped

The following screenshots illustrate how you handle these decisions. The description of eachscreenshot indicates the name of the class you should select.

JBoss Developer Studio 5.0 Getting Started Guide 71

Page 72: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.34 . javax.ws.rs.core.MediaType

Figure 8.35. org.jboss.tools.examples.Event

Figure 8.36. javax.ws.rs.Produces

72 Chapter 8. Example of developing a Java EE6, HTML5, Maven application for mobile devices

Page 73: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.37. java.util.List

Figure 8.38. javax.inject.Inject

JBoss Developer Studio 5.0 Getting Started Guide 73

Page 74: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.39. javax.enterprise.context.RequestScoped

You should now see the following code contained within your EventService.java file:

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 org.jboss.tools.examples.model.Event;

Save the changes you have made to the EventService.java and you will see it listed under the JAX-RS REST Web Services section of the project, in the Project Explorer.

74 Chapter 8. Example of developing a Java EE6, HTML5, Maven application for mobile devices

Page 75: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.4 0. Project Explorer JAX-RS Services

You have successfully configured your JAX-RS endpoint.

You can deploy your jboss-javaee6-webapp project to your local application server via Run As →Run on Server or right-click on the project in the Servers tab and select Full Publish.

Figure 8.4 1. Full Publish

Using a web browser navigate to the following URL:http://localhost:8080/jboss-javaee6-webapp/rest/events. You will see the results of the query, formatted as JavaScript Object Notation(JSON) returned into the browser.

JBoss Developer Studio 5.0 Getting Started Guide 75

Page 76: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.4 2. JSON Response

Note

The rest prefix is setup in a file called JaxRsActivator.java and it contains a small bit ofcode that configures the application for JAX-RS endpoints.

8.8. Adding a jQuery Mobile client applicationYou will now add a HTML5, jQuery based client application, optimized for a mobile web experience.

In the Project Explorer, navigate to src/main/webapp, right-click on webapp to bring up the contextmenu and select New → HTML File .

Figure 8.4 3. New HTML File

The New HTML File Wizard will launch. Enter the file name as mobile.html.

76 Chapter 8. Example of developing a Java EE6, HTML5, Maven application for mobile devices

Page 77: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.4 4 . New HTML File src/main/webapp

Click Next

On the Select HTML Template page, select HTML5 jQuery Mobile Page .

JBoss Developer Studio 5.0 Getting Started Guide 77

Page 78: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.4 5. Select HTML5 jQuery Mobile Template

Click Finish

Using a web browser navigate to http://localhost:8080/jboss-javaee6-webapp/mobile.html.

Note

HTML files are deployed automatically, however if you find it missing, use >Full Publish or RunAs → Run on Server, as demonstrated in previous steps.

You will see a dialog box stating Ready to Go.

78 Chapter 8. Example of developing a Java EE6, HTML5, Maven application for mobile devices

Page 79: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.4 6. jQuery Mobile Template

To connect your front-end to your back-end, edit the pageinit JavaScript event and include an invocationof the previously created JAX-RS service, Events.

Insert the following block of code directly below the alert()

$.getJSON("rest/events", function(events) { // console.log("returned are " + results); var listOfEvents = $("#listOfItems"); listOfEvents.empty(); $.each(events, function(index, event) { // console.log(event.name); listOfEvents.append("<li><a href='#'>" + event.name + "</a>"); }); listOfEvents.listview("refresh"); });

Notes to assist understanding the above JavaScript code:

Uses $.getJSON("rest/events") to interact with EventService.java

// console.log has been commented out as it causes problems in Internet Explorer.

References listOfItems, declared in the HTML using an id attribute

.empty is executed on the list, removing the items: One, Two and Three

A Found event is appended to the unordered list (UL) in the HTML

listOfItems is refreshed

Refresh your browser to see the results.

JBoss Developer Studio 5.0 Getting Started Guide 79

Page 80: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.4 7. jQuery Mobile REST Results

From within the JBoss Perspective, click on the icon representing a mobile phone to launchBrowserSim .

Figure 8.4 8. Mobile BrowserSim icon

BrowserSim allows you to see how your project will be viewed on a range of mobile devices.

80 Chapter 8. Example of developing a Java EE6, HTML5, Maven application for mobile devices

Page 81: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.4 9. Mobile BrowserSim

BrowserSim has a Devices menu. From this menu you can change the device type, orientation andbrowser dimensions.

Figure 8.50. Mobile BrowserSim Devices Menu

JBoss Developer Studio 5.0 Getting Started Guide 81

Page 82: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.51. Mobile BrowserSim on Windows 7

You can also add your own custom device and browser types.

Figure 8.52. Mobile BrowserSim Custom Devices Window

You can browse any website using BrowserSim , providing you have an Internet connection. By doingso you can view the source code of other mobile sites by navigating to File → View Page Source .

82 Chapter 8. Example of developing a Java EE6, HTML5, Maven application for mobile devices

Page 83: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 8.53. Mobile BrowserSim View Source

JBoss Developer Studio 5.0 Getting Started Guide 83

Page 84: JBoss Developer Studio 5.0 Getting Started Guide en US

Chapter 9. Developing a simple JSP web applicationIn this chapter you'll find out how to create a simple JSP application using JBoss Developer Studio. Theapplication will show a classic "Hello World!" on the page.

We'll assume that you have already launched JBoss Developer Studio and also that the WebDevelopment perspective is the current perspective. If not, make it active by selecting Window → OpenPerspective → Web Development from the menu bar or by selecting Window → OpenPerspective → Other... from the menu bar and then selecting Web Development from the SelectPerspective dialog box.

9.1. Setting Up the ProjectWe are going to start by creating a Dynamic Web Project with a minimal structure, i.e. with just requiredfacets. Thus this section will perform you all necessary steps on how to do this.

Go to the menu bar and select File → New → Other...Select Web → Dynamic Web Project in the New Project dialog box

Click the Next button

Enter "jspHello" as a project name

Then select Minimal Configuration from the list of possible configurations and click the Finishbutton.

84 Chapter 9. Developing a simple JSP web application

Page 85: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 9.1. Create New Web Project

The jspHello node should appear in the upper-left Package Explorer view.

Figure 9.2. New Web Project

9.2. Creating JSP PageThis section covers all the points how to create, edit and then preview JSP page.

In our simple application we need to create only one JSP page which displays a "Hello World!" message.

Right click the WebContent folder and select New → JSP.

Type hello.jsp for a file name and click the Next button.

In the next window you can choose a template for your JSP page and see its preview.

Select New JSP File (xhtml) template and click the Finish button.

JBoss Developer Studio 5.0 Getting Started Guide 85

Page 86: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 9.3. Create JSP Page

Our hello.jsp page will now appear in the Project Explorer view.

9.2.1. Edit ing a JSP Page

Let's now make a little change so that a JSP page displays "Hello World!" message.

Insert this line inside the <body> </body> tag:

<% System.out.println("Hello World!"); %>

Notice that content assist functionality is always available when you are typing:

86 Chapter 9. Developing a simple JSP web application

Page 87: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 9.4 . Content Assist in JSP Page

After changes made your hello.jsp page should look like this:

Figure 9.5. Hello.jsp Page Source

This line will actually output "Hello World!" message in the Console. To make the message displayed inthe Browser, just replace this line with the simple Hello World!.

9.2.2. web.xml file

When you are creating web project the wizard creates the web.xml file for you automatically. Theweb.xml file editor provided by JBoss Developer Studio is available in two modes: Tree and Source.

JBoss Developer Studio 5.0 Getting Started Guide 87

Page 88: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 9.6. Web.xml in Design and Source Mode

Both modes are fully synchronized. Let's add a mapping to our hello.jsp page in the web.xml file.

Switch to the Source tab.

Add the next code into <welcome-file-list> :

<welcome-file>hello.jsp</welcome-file>

If you go back to Tree tab you will see that the changes made in the Source tab are automaticallyreflected.

Actually you do not really need to do any configurations right now.

9.2.3. Deploying the project

Writing ant scripts and managing the packaging process can be quite a complicated and time consumingtask for even the most trivial web applications. However, JBoss Developer Studio relieves you of thisburden. All you need is to start your JBoss Server and launch your application in your favorite browser.

You can also create a JAR archive with JBoss Developer Studio's Archive Tools and export it to any webserver.

9.2.3.1. JAR Config

Project archives managing is available through the Project Archives view.

Select Window → Show view → Other → JBoss Tools → Project archives from the menu bar

Select a project in Package Explorer you want to be archived

In the Project Archives view you will see the that the project is now listed:

88 Chapter 9. Developing a simple JSP web application

Page 89: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 9.7. Project Archives

Right click on the project and select the JAR type of archive.

Figure 9.8. Project Archives

In the New JAR dialog you can see automatically selected default values.

Figure 9.9. New JAR Archive

Click the Finish button. The .JAR file will appear in Package Explorer and also in Project Archivesview as structure tree:

JBoss Developer Studio 5.0 Getting Started Guide 89

Page 90: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 9.10. Archive is Created

Figure 9.11. Archive in Package Explorer

Using the Project Archives view you can rebuild the archive:

Figure 9.12. Configure Archive

9.2.3.2. Auto redeploy

When you create a web application and register it on the JBoss Server as it is automatically deployedinto the /deploy directory of the server. JBoss Developer Studio's auto-redeploy feature ensures youdo not need to restart the server. Any changes made in the application in exploded format will trigger aredeployment on the server.

You can also use the "Finger touch" button for a quick restart of the project without restarting the server:

90 Chapter 9. Developing a simple JSP web application

Page 91: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 9.13. Finger Touch button

The "Finger" touches descriptors dependent on project (i.e. web.xml for WAR, application.xml for EAR,jboss-esb.xml in ESB projects).

9.2.4 . JSP Page Preview

JBoss Developer Studio comes with JSP design-time preview features. When designing JSP pages youcan easily preview how they will look during runtime. You can even attach your stylesheet to the Preview.

Make a little change to hello.jsp page, e.g. put this code snippet:

<%= new java.util.Date() %>

Click the Save button.

Switch to Preview page by clicking the Preview tab at the bottom of the page. You will see how thepage will look at runtime.

9.2.5. Launch JSP Project

Now launch the project onto a JBoss server:

Start a JBoss Server from the Servers view by clicking the Start the server icon ( ) .

Click the Run icon or right click your project folder and select Run As → Run on Server. If youhaven't made any changes in the web.xml file or cleared it out you can launch the application by

right clicking the hello.jsp page and selecting Run on the Server( ).

You should see the next page in a Browser :

Figure 9.14 . Running Project

You have learnt how to organize a Dynamic Web Project with a minimal configuration, add new elementsto it (in our case it is a JSP page), deploy, and run it on a JBoss Server from within JBoss DeveloperStudio.

JBoss Developer Studio 5.0 Getting Started Guide 91

Page 92: JBoss Developer Studio 5.0 Getting Started Guide en US

Chapter 10. Rapid Application Development of a JSFapplicationIn this chapter you will learn how to create a JSF application being based on the Rapid ApplicationDevelopment (RAD) philosophy. We will create the familiar Guess Number application. The game isplayed according to the following rules. You are asked to guess a number between 0 and 100. If theguess is correct, a success page is displayed with a link to play again. If the guess is incorrect, amessage is printed notifying that a smaller or a larger number should be entered and the gamecontinues.

You will now learn how to create such an application from scratch, along the way demonstrating thepowerful features included in JBoss Developer Studio such as project templating, Visual Page Editor,code completion and others. You will design the JSF application and then run the application from insideJBoss Developer Studio using a JBoss server.

10.1. Setting up the projectFirst, you should create a JSF 1.2 project using an integrated JBoss Developer Studio's new projectwizard and predefined templates. Follow the next steps:

In the Web Projects view (if it is not open select Window → Show View → Others → JBoss ToolsWeb → Web Projects) click Create New JSF Project button.

Figure 10.1. Create New JSF Project

Enter GuessNumber as a project name, in JSF Environment drop down list choose JSF 1.2

Leave everything else as it is and click the Finish button

Our project will appear in the Project Explorer and Web Projects views. As you can see JBoss DeveloperStudio has created the entire skeleton for the project with all required libraries, faces-config.xml fileand web.xml file.

92 Chapter 10. Rapid Application Development of a JSF application

Page 93: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 10.2. New JSF Project

As the project has been set up, new JSP pages should now be created.

JBoss Developer Studio 5.0 Getting Started Guide 93

Page 94: JBoss Developer Studio 5.0 Getting Started Guide en US

10.2. Creating JSP PagesHere, we are going to add two pages to our application. The first page is called inputnumber.jsp. Itprompts you to enter a number. If the guess is incorrect, the same page will be redisplayed with amessage indicating whether a smaller or a larger number should be tried. The second page is called success.jsp. This page will be shown after you guess the number correctly. From this page you alsohave the option to play the game again.

Steps for adding two pages to your application:

First a folder called pages needs to be created under the WebContent folder. To do this right clickon the WebContent folder in the Package Explorer view and select New → Folder. Set the FolderName to pages and click the Finish button.

Figure 10.3. Create pages folder

Open the faces-config.xml file.

Right click anywhere on the diagram mode

From the context menu select New View

94 Chapter 10. Rapid Application Development of a JSF application

Page 95: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 10.4 . Create New View

Type pages/inputnumber as the value for the From View ID fieldLeave everything else as is and click the Finish button

In the same way create another JSF view. Type pages/success as the value for From View IDSelect File → Save

On the diagram you will see two created views.

Figure 10.5. New Views

10.3. Creating Transition between two viewsThen, we should create connection between JSP pages.

In the diagram, select the Create New Connection icon third from the top along the upper leftside of the diagram to get an arrow cursor with a two-pronged plug at the arrow's bottom

JBoss Developer Studio 5.0 Getting Started Guide 95

Page 96: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 10.6. Create Connection

Click on the pages/inputnumber page icon and then click on the pages/success page icon

A transition should appear between the two icons of views.

Figure 10.7. Created Connection

Select File → Save from the menu bar

10.4. Creating Resource FileA resource file is a file with a .properties extension for collecting text messages in one central place.JBoss Developer Studio allows you to create quickly a resource file. The messages stored in a resourcefile can be displayed to you on a Web page during application execution.

With resource file you don't hard code anything into the JSP pages. It also makes it easier to translateyour application to other languages. All you have to do is to translate all your messages to the otherlanguage and save them in a new properties file with a name that ends with the appropriate ISO-639language code.

It is a good idea to keep your resources inside the JavaSource folder, where you keep your .java files.

96 Chapter 10. Rapid Application Development of a JSF application

Page 97: JBoss Developer Studio 5.0 Getting Started Guide en US

Every time you build the project, all .properties files will then be copied to the classes folder by default.

Right click the JavaSource folder and select New → FolderEnter game as the Folder name and click the Finish button

Your resource file and java bean will be stored in this folder.

Right click on the game folder and select New → Properties FileType messages as the value for "name" attribute and click the Finish button

JBoss Developer Studio will automatically open messages.properties file for editing.

Figure 10.8. Messages.properties File

Click the Add button for adding new attribute to your resource file

Enter how_to_play for the "name" and Please pick a number between 0 and 100. for the value

Click the Finish button

Add the following properties using the same process:

makeguess_button=Make Guesstrayagain_button=Play Again?success_text=How cool.. You have guessed the number, {0} is correct! tryagain_smaller=Oops..incorrect guess. Please try a smaller number.tryagain_bigger=Oops..incorrect guess. Please try a bigger number.

Select File → Save from the menu bar

Your .properties file should now look like follows:

JBoss Developer Studio 5.0 Getting Started Guide 97

Page 98: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 10.9. Properties are Added

The Up and Down buttons allow you to move the attributes in the list. To delete the attribute, select itand press the Delete button.

If you want to change a value or a name of your attribute, select it and then click the Edit button.

If the .properties file is rather big and there are a lot of entries in it, you can use filtering and regularexpressions narrow down the list. The Filter and Regular Expressions Search is implemented by anexpandable panel, closed by default:

When "Expression" is not selected (as by default), filter is case insensitive. When "Expression" isselected, filter uses regular expressions which are case sensitive

Figure 10.10. Filter and Regular Expressions Search Panel

Enter the characters that should be searched for in the entries to the 'name' or 'value' input fieldsaccordingly. The filtered results will be displayed in the table below:

98 Chapter 10. Rapid Application Development of a JSF application

Page 99: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 10.11. Filter results

When using regular expressions please note, that regular expression syntax does not use "*" for anycharacters and "?" for any one character. It's necessary to use "." for any one character and ".*" for anycharacters. Symbols "*" and "?" are used to show that the preceding token is not required, for example,"a.a" matches "aba" but not "aa", while "a.?a" or a.*a" matches both; besides "a.*a" matches "abcda".

To find the exact match, use sequences \A and \z in expression. For example, expression "\Adate\z"matches only string "date"; expression "\Adate" matches "date" and "dateline", expression "date\z"matches "date" and "Begin date", and expression "date" matches all of them.

10.5. Creating a Java BeanIn this section you'll learn how to create a Java bean that will hold business logic of our application.

Right click the game folder

Select New → ClassType NumberBean for bean name

A java bean is created.

Declare the variable of your entered number:

Integer userNumber;

JBoss Developer Studio allows for quick generation of getters and setters for java bean.

Right click the NumberBean.java file in the Package Explorer view

Select Source → Generate Getters and Setters...Check userNumber box and click the OK button

JBoss Developer Studio 5.0 Getting Started Guide 99

Page 100: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 10.12. Generate Getters and Setters

Add the declaration of the second variable

int randomNumber;

.. other bean methods:

100 Chapter 10. Rapid Application Development of a JSF application

Page 101: JBoss Developer Studio 5.0 Getting Started Guide en US

public NumberBean () { randomNumber = (int)(Math.random()*100); System.out.println ( "Random number: "+randomNumber); } public String playagain () { FacesContext context = FacesContext.getCurrentInstance(); HttpSession session = (HttpSession) context.getExternalContext().getSession(false); session.invalidate(); return "playagain"; } public String checkGuess () { // if guessed, return 'success' for navigation if ( userNumber.intValue() == randomNumber ) { return "success"; }else { FacesContext context = FacesContext.getCurrentInstance(); ResourceBundle bundle = ResourceBundle.getBundle("game.messages", context.getViewRoot().getLocale()); String msg = ""; // if number bigger, get appropriate message if ( userNumber.intValue() > randomNumber ) msg = bundle.getString("tryagain_smaller"); else // if number smaller, get appropriate message msg = bundle.getString("tryagain_bigger"); // add message to be displayed on the page via <h:messages> tag context.addMessage (null, new FacesMessage(msg)); // return 'tryagain' for navigation return "tryagain"; } }

And the import declarations:

import javax.faces.context.FacesContext;import javax.servlet.http.HttpSession;import javax.faces.application.FacesMessage;import java.util.ResourceBundle;

The Java Bean contains the following code:

JBoss Developer Studio 5.0 Getting Started Guide 101

Page 102: JBoss Developer Studio 5.0 Getting Started Guide en US

package game;

import javax.faces.context.FacesContext;import javax.servlet.http.HttpSession;import javax.faces.application.FacesMessage;import java.util.ResourceBundle;

public class NumberBean { Integer userNumber; int randomNumber; // random number generated by application public Integer getUserNumber () { return userNumber; } public void setUserNumber (Integer value) { this.userNumber = value; } // constructor, generates random number public NumberBean () { randomNumber = (int)(Math.random()*100); System.out.println ( "Random number: " + randomNumber); } public String playagain () { FacesContext context = FacesContext.getCurrentInstance(); HttpSession session = (HttpSession) context.getExternalContext().getSession(false); session.invalidate(); return "playagain"; } // check if user guessed the number public String checkGuess () { // if guessed, return 'success' for navigation if ( userNumber.intValue() == randomNumber ) { return "success"; } // incorrect guess else { // get a reference to properties file to retrieve messages FacesContext context = FacesContext.getCurrentInstance(); ResourceBundle bundle = ResourceBundle.getBundle("game.messages", context.getViewRoot().getLocale()); String msg = ""; // if number is bigger, get appropriate message if ( userNumber.intValue() > randomNumber ) msg = bundle.getString("tryagain_smaller"); else // if number smaller, get appropriate message msg = bundle.getString("tryagain_bigger"); // add message to be displayed on the page via <h:messages> tag

102 Chapter 10. Rapid Application Development of a JSF application

Page 103: JBoss Developer Studio 5.0 Getting Started Guide en US

context.addMessage (null, new FacesMessage(msg)); // return 'tryagain' for navigation return "tryagain"; } }}

10.6. Editing faces-config.xml FileIn this section you will learn about the faces-config.xml file.

This file holds two navigation rules and defines the backing bean used.

Open the faces-config.xml file in a source mode

Here we will add one more navigation rule and a managed bean declaration, so that the content ofthe file looks like this:

<?xml version="1.0" encoding="UTF-8"?><faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xi="http://www.w3.org/2001/XInclude" 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-facesconfig_1_2_.xsd"> <navigation-rule> <from-view-id>*</from-view-id> <navigation-case> <from-outcome>playagain</from-outcome> <to-view-id>/pages/inputnumber.jsp</to-view-id> </navigation-case> </navigation-rule>

<navigation-rule> <from-view-id>/pages/inputnumber.jsp</from-view-id> <navigation-case> <from-outcome>success</from-outcome> <to-view-id>/pages/success.jsp</to-view-id> </navigation-case> </navigation-rule> <managed-bean> <managed-bean-name>NumberBean</managed-bean-name> <managed-bean-class>game.NumberBean</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean> </faces-config>

The first navigation rule states that from any page (* stands for any page) an outcome of playagain willtake you to the /pages/inputnumber.jsp file. Outcome values are returned from backing beanmethods in this example. The second navigation rule states that if you are at the page /pages/inputnumber.jsp, and the outcome is success, then navigate to the /pages/success.jsp page.

JBoss Developer Studio 5.0 Getting Started Guide 103

Page 104: JBoss Developer Studio 5.0 Getting Started Guide en US

10.7. Editing the JSP View FilesNow, we will continue editing the JSP files for our two "views" using the Visual Page Editor.

10.7.1. Edit ing inputnumber.jsp page

First, edit the inputnumber.jsp file.

On this page we will have an output text component displaying a message, a text field for user's numberentering and a button for input submission.

Open the inputnumber.jsp file by double-clicking on the /pages/inputnumber. jsp icon

The Visual Page Editor will open in a screen split between source code along the top and a WYSIWIGview along the bottom. You can see that some JSF code will have already been generated since wechose a template when creating the page.

At the beginning it's necessary to create a <h:form> component that will hold the other components.

Place the mouse cursor inside the <f:view></f:view> tag

Go to JBoss Tools Palette and expand JSF HTML folder by selecting it

Click on the <h:form> tag

104 Chapter 10. Rapid Application Development of a JSF application

Page 105: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 10.13. Insert h:form

In the Insert Tag dialog select the id field and click on the second column. A blinking cursor willappear in a input text field inviting to enter a value of id

JBoss Developer Studio 5.0 Getting Started Guide 105

Page 106: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 10.14 . Define Id of Form

Enter inputNumbers and click the Finish button

In source view you can see the declaration of a form.

Figure 10.15. Created Form

First let's declare the properties file in the inputnumber.jsp page using the loadBundle JSF tag.

Add this declaration on the top of a page, right after the first two lines:

106 Chapter 10. Rapid Application Development of a JSF application

Page 107: JBoss Developer Studio 5.0 Getting Started Guide en US

<f:loadBundle basename="game.messages" var="msg"/>

As always JBoss Developer Studio provides code assist:

Figure 10.16. Code Assist

Switch to Visual tab, where it is possible to work with the editor through a WYSIWYG interface

Click the outputText item from the JSF HTML group in the JBoss Tools Palette view, drag thecursor over to the editor, and drop it inside the blue box in the editor

Select the second column in the value row.

Click the ... button next to the value field

JBoss Developer Studio will display a list of possible values:

Figure 10.17. Choose Value

Expand Resource Bundles → msgSelect the how_to_play value and click the OK button. Then click the Finish button.

JBoss Developer Studio 5.0 Getting Started Guide 107

Page 108: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 10.18. Selecting Value

The text will appear on the page:

Figure 10.19. Created OutputText Component

Switch to Source mode and insert a <br/> tag after the <h:outputText> component to make anew line

Click the Save button

On the Palette click on inputText, drag the cursor over to the editor, and drop it inside the editor afterthe text

Select the value row and click in the second column

Click the ... button next to the value field

Expand Managed Beans → NumberBeanSelect userNumber value and click the OK button

Select the Advanced tab

Select the id row and click in the second column

Type userNumber in the text field

Select the required row and click in the second column

Click ... button next to the value field

Expand Enumeration and select true as a value

108 Chapter 10. Rapid Application Development of a JSF application

Page 109: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 10.20. Add "required" Attribute

Click the OK button, then click the Finish button

Go to Source mode

Add the validation attribute to <f:validateLongRange> for user input validation

<h:inputText id="userNumber" value="#{NumberBean.userNumber}" required="true"> <f:validateLongRange minimum="0" maximum="100"/></h:inputText>

Click the Save button

Again select Visual mode

On the Palette, click on commandButton, drag the cursor over to the editor, and drop it inside theeditor after the inputText component.

In the editing dialog select the value row and click on the second column

Click the ... button next to the value field

Expand Resource Bundles → msg and select makeguess_button as a value

Click the OK button

Select the action row and click in the second column

Type #{NumberBean.checkGuess} in the text field

Click the Finish button

In Source mode add <br/> tags between the <outputText>, <inputText> and <commandButton> components to place them on different lines

inputnumber.jsp page should look like this:

JBoss Developer Studio 5.0 Getting Started Guide 109

Page 110: JBoss Developer Studio 5.0 Getting Started Guide en US

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%><%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%><f:loadBundle basename="game.messages" var="msg"/><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <f:view> <h:form id="inputNumbers"> <h:outputText value="#{msg.how_to_play}"/> <br/> <h:messages style="color: blue" /> <br/> <h:inputText id="userNumber" required="true" value="#{NumberBean.userNumber}"> <f:validateLongRange minimum="0" maximum="100" /> </h:inputText> <br/> <br/> <h:commandButton action="#{NumberBean.checkGuess}" value="#{msg.makeguess_button}"/> </h:form> </f:view> </body></html>

10.7.2. Edit ing success.jsp page

We now edit the success.jsp page in the same way as we just edited the inputnumber.jsp file.The code for the success.jsp page should look like the following:

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %><%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %><f:loadBundle basename="game.messages" var="msg"/>

<html> <head> <title></title> </head> <body> <f:view> <h:form id="result"> <h:outputFormat value="#{msg.success_text}"> <f:param value="#{NumberBean.userNumber}" /> </h:outputFormat> <br /> <br /> <h:commandButton value="#{msg.trayagain_button}" action="#{NumberBean.playagain}" /> </h:form> </f:view> </body></html>

Again you can use code assist provided by JBoss Developer Studio when editing jsp page:

110 Chapter 10. Rapid Application Development of a JSF application

Page 111: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 10.21. Code Assist for <f:param>

The success.jsp page is shown if you correctly guessed the number. The <h:outputFormat> tagwill get the value of success_text from the properties file. The {0} in success_text will besubstituted for by the value of the value attribute within the <f:param> tag during runtime.

In the final result you have a button which allows you to replay the game. The action value referencesa backing bean method. In this case, the method only terminates the current session so that when youare shown the first page, the input text box is clear and a new random number is generated.

Switch to Preview mode to see how this page will look in a browser:

Figure 10.22. Success.jsp in Preview Mode

10.8. Creating index.jsp pageNow we need to create the index.jsp page.

JBoss Developer Studio 5.0 Getting Started Guide 111

Page 112: JBoss Developer Studio 5.0 Getting Started Guide en US

The index.jsp page is the entry point of our application. It's just forwarding to the inputnumber.jsp page.

Right click the WebContent folder and select New → JSP FileEnter index for name field and click the Next button.

Untick the Use JSP Template check box and click the Finish button.

Edit the source of the file so it looks like the following:

<!doctype html public "-//w3c//dtd html 4.0 transitional//en"><html> <body> <jsp:forward page="/pages/inputnumber.jsf" /> </body></html>

Note the .jsf extension of a page. It means that we trigger the JSF controller servlet to handle the pageaccording the servlet mapping in the faces-config.xml file.

10.9. Running the ApplicationFinally, we have all the pieces needed to run the application.

Start up JBoss server by clicking on the Start icon in the Servers view. (If the JBoss Server isalready running, stop it by clicking on the red icon and then start it again. After the messages in theConsole tabbed view stop scrolling, JBoss is available)

Right-click on the project and select Run As → Run on ServerPlay with the application by entering correct as well as incorrect values

Figure 10.23. You are Asked to Enter a Number Between 0 and 100

112 Chapter 10. Rapid Application Development of a JSF application

Page 113: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 10.24 . Your Input is Validated and an Error Message is Displayed if Invalid Input wasEntered

Figure 10.25. After You Enter a Guess, the Application Tells You Whether a Smaller or aLarger Number Should be Tried

JBoss Developer Studio 5.0 Getting Started Guide 113

Page 114: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 10.26. Your Guess is Correct

114 Chapter 10. Rapid Application Development of a JSF application

Page 115: JBoss Developer Studio 5.0 Getting Started Guide en US

Chapter 11. Uninstalling the JBoss Developer StudioEnsure JBoss Developer Studio is not running

Run the Uninstaller

JBoss Developer Studio 5.0 Getting Started Guide 115

Page 116: JBoss Developer Studio 5.0 Getting Started Guide en US

Chapter 12. FAQRefer to the following FAQ to get the answers on the most "popular" questions concerning JBossDeveloper Studio.

12.1. What should I do if the Visual Page Editor does not start underLinuxLinux users may need to do the following to get the Visual Page Editor to work correctly on theirmachines.

1. On Red Hat based Linux distributions install the libXp.i386 package

2. Type

ln -s libstdc++.so.5.0.7 libstdc++.so.5

3. and/or use

yum install libXp

4. Open the JBoss Developer Studio perspective. If you see the Help view open, close it and restartJBoss Developer Studio

5. If it doesn't help and you use Fedora with Eclipse Version: 3.4.1, the issue can be producedbecause the libswt-xulrunner-gtk-3449.so file doesn't present in eclipse-swt-3.4.1-5.fc10.x86_64.rpm/eclipse/plugins/org.eclipse.swt.gtk.linux.x86_64_3.4.1.v3449c.jar. To add this file to eclipse you should:

Decompress eclipse/plugins/org.eclipse.swt.gtk.linux.x86_3.4.1.v3449c.jar form eclipse-SDK-3.4.1-linux-gtk-x86_64.tar.gz

Copy libswt-xulrunner-gtk-3449.so file to your Fedora Eclipse location.

Open the file jbdevstudio.ini, which can be found in your Fedora Eclipse location and addthe following line:

-Dswt.library.path=/usr/lib/eclipse

,where /usr/lib/eclipse is the path to your eclipse folder.

6. If none of these work, do the following:

Clear the JBoss Developer Studio log file,<workspace>\.metadata\.log

Start JBoss Developer Studio with the -debug option:

jbdevstudio -debug

Post the JBoss Developer Studio log file(<workspace>\.metadata\.log) on the forums.

12.2. Visual Editor starts OK, but the Missing Natures dialog appears

116 Chapter 12. FAQ

Page 117: JBoss Developer Studio 5.0 Getting Started Guide en US

Figure 12.1. Missing Nature

Some functionality of Visual Editor may not work if a project doesn't have org.jboss.tools.jsf.jsfnature ororg.jboss.tools.jst.web.kb.kbnature in .project configuration. To fix this problem and turn off the messagebox execute next steps:

1. Right mouse button click on a project in Package Explorer.

2. Select Configure → Add JSF Capabilit ies from the context menu.

3. Configure your project using Add JSF Capabilities wizard and press Finish.

If you are sure that your project does not need JSF capabilities, just disable this message box bychecking Do not show this dialog again! checkbox.

12.3. I have an existing Seam 1.2.1 project. Can I migrate or importthe project into a JBoss Developer Studio Seam project?Use the following steps to manually transfer an existing Seam 1.2.1 project into a new JBoss DeveloperStudio Seam project:

Create a Seam Web project to get the JBoss tools structure

Then from your Seam 1.2.1 seam-gen project start doing the following:

Copy src to src

Copy view to Web content

Copy resources individual files to where they are in the seam web project etc.

12.4. I have an existing Struts or JSF project. Can I open the projectin JBoss Developer Studio?Yes. From main menu select File → File → Import → Other → JSF Project (or Struts Project) andfollow wizards steps.

12.5. Can I import a WAR file?Yes. Select File → Import → Web → WAR file then follow importing steps.

12.6. Is it possible to increase the performance of Eclipse afterinstalling your product?JBoss Developer Studio configures eclipse via the jbdevstudio.ini file to allocate extra memory, but

JBoss Developer Studio 5.0 Getting Started Guide 117

Page 118: JBoss Developer Studio 5.0 Getting Started Guide en US

if you for some reason need more memory then by default, you can manually make adjustments in thisfile. For example:

-vmargs -Xms128m -Xmx512m -XX:MaxPermSize=128m

12.7. How can I add my own tag library to the JBoss Tools Palette?See the section on Adding Tag Libraries in the Visual Web Tools Guide.

12.8. How to get Code Assist for Seam specific resources in anexternally generated project?To get Code Assist for Seam specific resources in an externally generated project, you should enableSeam features in Project Preferences. Right click an imported project and navigate Properties → SeamSettings. Check Seam support box to enable all available Seam Settings.

12.9. How to import an example Seam project from jboss-eapdirectory?To import an example Seam project from jboss-eap into your working directory, you should perform thefollowing steps:

Select New → Other → Java Project from Existing BuildfilePoint to the build.xml file of any chosen project by clicking the Browse button

Click the Finish button to open the project

As these seam examples are non WTP projects, next you should enable Seam support for them. To dothat, right click the project and select Properties → Seam Settings.

12.10. Is a cross-platform project import possible for JBossDeveloper Studio?Yes. You can easily import created in Linux JSF, Struts or Seam project to Windows and vice versa.

To do the transferring JSF, Struts or Seam project, select Menu → Import → General → ExistingProjects into Workspace .

118 Chapter 12. FAQ

Page 119: JBoss Developer Studio 5.0 Getting Started Guide en US

Getting Help and Giving Feedback

A.1. Do You Need Help?

If you experience difficulty with a procedure described in this documentation, visit the Red Hat CustomerPortal at http://access.redhat.com. Through the customer portal, you can:

search or browse through a knowledgebase of technical support articles about Red Hat products.

submit a support case to Red Hat Global Support Services (GSS).

access other product documentation.

Red Hat also hosts a large number of electronic mailing lists for discussion of Red Hat software andtechnology. You can find a list of publicly available mailing lists at https://www.redhat.com/mailman/listinfo.Click on the name of any mailing list to subscribe to that list or to access the list archives.

A.2. Give us Feedback

If you find a typographical error, or know how this guide can be improved, we would love to hear fromyou. Submit a report in Bugzilla against the product JBoss Developer Studio and the component Getting Started Guide. The following link will take you to a pre-filled bug report for this product:http://bugzilla.redhat.com/.

Fill out the following template in Bugzilla's Description field. Be as specific as possible whendescribing the issue; this will help ensure that we can fix it quickly.

Document URL:

Section Number and Name:

Describe the issue:

Suggestions for improvement:

Additional information:

Be sure to give us your name so that you can receive full credit for reporting the issue.

JBoss Developer Studio 5.0 Getting Started Guide 119

Page 120: JBoss Developer Studio 5.0 Getting Started Guide en US

Revision HistoryRevision 5.0.1-35 2012-07-18 Anthony Towns

Rebuild for Publican 3.0

Revision 1-0 Wed Nov 09 2011 Isaac RooskovInitial creation of book

120 Revision History