Khaled Rihane

Dev J2EE


Templating with JSF2.0 Facelets

Introduction

In JavaServer Faces (JSF) 2.0, Facelets is the default view declaration language (VDL) instead of JavaServer Pages (JSP). With Facelets, you don’t need to configure a view handler as you used to do in JSF 1.2. Facelets is a JSF-centric view technology.

Facelets is based on compositions. A composition defines a JSF UIComponents structure in a Facelets page. A Facelets application may consist of compositions defined in different Facelets pages and run as an application.

Facelets is a templating framework similar to Tiles. The advantage of Facelets over Tiles is that JSF UIComponents are pre-integrated with Facelets, and Facelets does not require a Facelets configuration file, unlike Tiles, which requires a Tiles configuration file. 

JSF Validators and Converters may be added to Facelets. Facelets provides a complete expression language (EL) and JavaServer Pages Standard Tag Library (JSTL) support. Templating, re-use, and ease of development are some of the advantages of using Facelets in a Web application.

In this article, we develop a Facelets Web application in Oracle Enterprise Pack for Eclipse 11g and deploy the application to Oracle WebLogic Server 11g. In the Facelets application, an input text UIComponent will be added to an input Facelets page. With JSF navigation, the input Facelets page is navigated to another Facelets page, which displays the JSF data table generated from the SQL query specified in the input Facelets page. We will use Oracle Database 11g Express Edition for the data source. Templating is demonstrated by including graphics for the header and the footer in the input and the output; the graphics have to be specified only once in the template.

Note: The source code for the Facelets Web application developed in this article can be downloaded here.

Overview of Facelets Technology

Before we develop the application, let’s review the Facelets technology. Facelets tags are used with the JSF Core and JSF HTML tag libraries to develop a JSF Facelets application. Some of the Facelets tags are discussed in Table 1.

Table 1. Facelets Tags

Facelets Tag

Description

Attributes

ui:composition

Defines a composition of UIComponents. A composition may consist of multiple UIComponents. Text not within ui:composition tags is not compiled in a Facelets page.

template: Optional attribute that specifies the template to use for adding UIComponents within a composition.

ui:define

Used in a Facelets page to add content with a template.

name: A required attribute; matches the name attribute in ui:insert.

ui:decorate

Similar to ui:composition except that text not within the ui:decorate tags is included in the Facelets page.

template: A required attribute that specifies the template to use for adding UIComponents in ui:decorate tags.

ui:component

Similar to ui:composition except adds a new UIComponent as the root component in the UIComponents structure.

id: If not specified, Facelets assigns an ID.

binding: Binds a UIComponent to a property of a JavaBean.

ui:fragment

A nontrimming tag for ui:component, similar to the ui:decorate tag for ui:composition.

id: If not specified, Facelets assigns an ID.

binding: Binds a UIComponent to a property of a JavaBean.

ui:include

Includes a Facelets page that defines a composition or a component in a Facelets page or the template page.

src: A required attribute that specifies the target Facelets page to include with an EL expression or a string literal.

ui:insert

Used in a template to define the layout of Facelets pages that use the template for defining UIComponents. A template client adds UIComponents to a Facelets page with corresponding ui:define tags. If a Facelets page specifies a template and it does not specify ui:define tags corresponding to ui:insert tags, the default ui:insert tags are used in the Facelets page.

name: An attribute that has a corresponding name attribute in a Facelets page’s ui:define tag for matching a template with a Facelets page.

ui:param

Specifies a variable when a Facelets page is included within a template or a Facelets page. 

name: Required attribute that specifies the variable name.

value: Required attribute that specifies the variable’s value as an EL expression or literal. 

Facelets pages are XHTML pages by default. A Facelets application consists of the following configuration and templating files.

  • A Facelet Template file, which is an XHTML page
  • Facelet Header and Facelet Footer XHTML pages
  • A JSF configuration file, faces-config.xml
  • Facelet Composition page, which is also an XHTML page

The template file defines the layout in Facelets and consists of <ui:insert/> tags to specify the structure of a Facelets composition that uses the template for defining UIComponents. 

The JSF configuration file is the only configuration file required; a Facelets configuration file is not required. A Facelets page is an XHTML page that includes tags in the Facelets namespace.

In the following sections, we create a Facelets application in Oracle Enterprise Pack for Eclipse 11g, Oracle WebLogic Server 11g, and Oracle Database 11g Express Edition.

Other Links:

http://www.mkyong.com/jsf2/jsf-2-templating-with-facelets-example/


05/09/2013
0 Poster un commentaire

TOMCAT CONFIGURATION IN ECLIPSE INDIGO

 

Without a tomacat plugin eclipse can not understand the tomcat. To configure it follow the below steps.

1.

Strat your eclipse

2. See in the toolbar there is no tomcat icon, because we dont have a plugin for it.

No icon in the top menu bar

3. Download the plugin here.

4. Unzip the file and paste to your eclipse\plugins directory.

paste unzip plugin folder here

5. Restart the eclipse now.

6. Now you can see the tomcat icon on the menu bar.

See the tomcat icons are appearing now

7. Now go to window->Preference . Find out Tomcat configuration from the left panel.

Go to preference

8.

Configure tomcat now

Choose your tomcat version and browse the tomcat home.

Press  OK to save all those  configuration.

Now you just configured your eclipse for tomcat.


28/04/2013
0 Poster un commentaire

Environnements de Développement Intégrés (EDI)

 

  • JBuilder , Borland JBuilder, qui est un environnement professionnel pour le développement de solutions Java, intègre tous les concepts d'ingénierie moderne, WebServices, XML, Travail collaboratif, plugin Mobilset pour écrire des applications pour terminaux mobiles, conception d'EJB, JSP/Servlet et Struts, Ant, etc.

  • NetBeans , NetBeans, crée à l'initiative de Sun Microsystems (Noyau de Forte4J/SunOne),  présente toutes les caractéristiques indispensables à un RAD Java de qualité. De licence OpenSource, NetBeans 4.1 permet de développer et déployer rapidement et gratuitement des applications graphiques Swing, des Applets, des JSP/Servlets, des architectures J2EE, dans un environnement fortement customisable. NetBeans possède un noyau robuste, et un système de plugins performant. Il intègre ainsi des outils comme AntJUnitJBossRefactoITCVS-SSHPoseidonUML , C, C++, XML, Jython, Tomcat, et propose un debugger puissant.

  • Sun One Studio , Anciennement connu sous le nom de Forte for Java, Sun ONE (Open Net Environnement) Studio s'appuie sur le noyau de NetBeans, projet initié par Sun. Sun ajoute cependant d'autres fonctionnalités, et permet notamment de coupler parfaitement l'IDE avec le serveur d'applications J2EE de Sun : Sun ONE Application Server Sun ONE Studio devient ainsi l'un des environnements de développement Java d'entreprise les plus complets, et totalement conforme aux spécifications J2EE. Les WebServices, JAXM, JAXP, JAXR, JAX-RPC, SOAP and WSDL, sont parfaitement assurés.

  • Sun Java Studio Creator ,Java Studio Creator (jscreator) est une plateforme de développement Java dédié J2EE et WebServices. Cet EDI fourni des outils visuels facilitant le développement Java grâce à une interface très ergonomique. Cette plateforme utilise une infrastructure à composants et fournis les outils permettant de réaliser des services web, d'accèder à des bases de données, etc...  Il permet au développeur d'apprendre, de développer, de tester et de déployer ses applications en se concentrant sur le code métier. Cet EDI se base les standards et assure de créer des applications portables.

  • WebSphere Studio Application Developer , WebSphere Studio est basé sur le projet OpenSource Eclipse. Outre les qualités indéniables de ce dernier, IBM apporte une valeur ajoutée à cet environnement. IBM WebSphere permet de créer facilement des WebServices avec WSDK mais aussi des projets basés sur des EJB et sur J2ME ( Applications pour terminaux mobiles). WASD accepte les interactions avec les SGBDR DB2InformixSybaseOracleSQL Server. Un environnement de test permet de controler la création et le déploiement de pages JSP, HTML

  • JDeveloper , Oracle JDeveloper est un EDI complet et gratuit permettant de modéliser et développer des applications Java pour les plateformes J2SE, J2EE ou J2ME. JDeveloper offre au développeur un excellent support orienté SGBDR ( Oracle9i ), le déploiement d'applications J2EE avec Oracle9iAS et WebLogic, la publication de procédures écrites en PL/SQL en WebServices, le debuggage de code PL/SQL, l'intégration de composants Java Server Faces (Oracle ADF Faces).


10/03/2013
0 Poster un commentaire

J2EE ou JEE ?

On a pendant longtemps parlé de la "plateforme J2EE", mais qui se souvient encore de l’origine de ce "2" ? C’est Sun qui a décidé de rebaptiser son langage "Java 2" à partir de la version 1.2 du Java Development Kit (JDK), nom qui est resté dans les versions suivantes. C’est ainsi que la plateforme entreprise de Java s’est au départ appelée "Java 2 Enterprise Edition". Mais pour des raisons marketing, Sun a décidé de rebaptiser cette plateforme "Java Enterprise Edition" (en supprimant le 2) à compter de la version 1.5... qui a elle-même été changée en "5" ! On parle donc de J2EE 1.4, mais de JEE 5.


10/03/2013
0 Poster un commentaire

Create Web Service

Create Web Service in Java Using Apache Axis2 and Eclipse

Web services are application components which communicate using open protocols. Using Web Services we can publish our application's functions to everyone. This tutorial provides step by step instructions to develop Web Services using Axis2 Web Services / SOAP / WSDL engine and Eclipse IDE. Let's start.

1. Setup the Development Environment


1.1. First you need to set up the development environment. Following things are needed if you want to create Web Services using Axis2 and Eclipse IDE. 

Some Eclipse versions have compatibility issues with Axis2. This tutorial is tested with Apache Axis2 1.5.2, Eclipse Helios and Apache Tomcat 6.

1) Apache Axis2 Binary Distribution - Download 
2) Apache Axis2 WAR Distribution - Download 
3) Apache Tomcat - Download 
4) Eclipse IDE – Download 
5) Java installed in your Computer – Download 

1.2. Then you have to set the environment variables for Java and Tomcat. There following variables should be added.
JAVA_HOME :- Set the value to jdk directory (e.g. C:\Program Files\Java\jdk1.6.0_21)
TOMCAT_HOME :- Set the value to top level directory of your Tomcat install (e.g. D:\programs\apache-tomcat-6.0.29)
PATH :- Set the value to bin directory of your jdk (e.g. C:\Program Files\Java\jdk1.6.0_21\bin)
1.3. Now you have to add runtime environment to eclipse. There go to Windows –-> Preferences and Select the Server --> Runtime Environments.


There select Apache Tomcat v6.0 and in the next window browse your Apache installation directory and click finish.


1.4. Then click on the Web Service –-> Axis2 Preferences and browse the top level directory of Apache Axis2 Binary Distribution.


2. Creating the Web Service Using Bottom-Up Approach


2.1 First create a new Dynamic Web Project (File --> New –-> Other…) and choose Web --> Dynamic Web Project.


2.2 Set Apache Tomcat as the Target Runtime and click Modify to install Axis2 Web Services project facet.


2.3 Select Axis2 Web Services


2.4 Click OK and then Next. There you can choose folders and click Finish when you are done.

3. Create Web Service Class


Now you can create a Java class that you would want to expose as a Web Service. I’m going to create new class called FirstWebService and create public method called addTwoNumbers which takes two integers as input and return the addition of them.

3.1 Right Click on MyFirstWebService in Project Explorer and select New –-> Class and give suitable package name and class name. I have given com.sencide as package name and FirstWebService as class name.

package com.sencide;
public class FirstWebService { 
 public int addTwoNumbers(int firstNumber, int secondNumber){
  return firstNumber + secondNumber;
 }
}
3.2 Then, select File --> New –-> Other and choose Web Service.


3.3 Select the FirstWebService class as service implementation and to make sure that the Configuration is setup correctly click on Server runtime.


3.4 There set the Web Service runtime as Axis2 (Default one is Axis) and click Ok.


3.5 Click Next and make sure Generate a default service.xml file is selected. 


3.6 Click Next and Start the Server and after server is started you can Finish if you do not want to publish the Web service to a test UDDI repository.


You can go to http://localhost:8888/MyFirstWebService/services/listServices to see your running service which is deployed by Axis2. You can see the WSDL by clicking the link FirstWebService.


We have to use Eclipse every time when we want to run the service if we do not create .aar (Axis Archive) file and deploy it to the server. So let’s see how we can create it.

4. Create .aar (Axis Archive) file and Deploying Service


4.1 In your eclipse workspace and go to MyFirstWebService folder and there you can find our web service inside services folder. Go to that directory using command prompt and give following command. It will create the FirstWebService.aar file there.
jar cvf FirstWebService.aar com META-INF



4.2 Then copy the axis2.war file you can find inside the Apache Axis2 WAR Distribution (You downloaded this at the first step) to the webapps directory of Apache Tomcat. Now stop the Apache Tomcat server which is running on Eclipse IDE. 


4.3 Using command prompt start the Apache Tomcat (Go to bin directory and run the file startup.bat). Now there will be new directory called axis2 inside the webapps directory. Now if you go to the http://localhost:8080/axis2/ you can see the home page of Axis2 Web Application.


4.4 Then click the link Administration and login using username : admin and password : axis2. There you can see upload service link on top left and there you can upload the created FirstWebService.aar file. This is equal to manually copping the FirstWebService.aar to webapps\axis2\WEB-INF\services directory.


4.5 Now when you list the services by going to http://localhost:8080/axis2/services/listServices you should be able to see our newly added service.


5. Creating a Web service client


5.1 Select File --> New --> Other… and choose Web Service Client


5.2 Set he newly created Axis2 Web service (http://localhost:8080/axis2/services/FirstWebService?wsdl) as the Service definition. Then configure the Server runtime as previously and click finish. 


5.3 This will generate two new classes called FirstWebServiceStub.java and FirstWebServiceCallbackHandler.java. Now we can create test class for client and use our web service. Create new class called TestClient.java and paste following code.
package com.sencide;

import java.rmi.RemoteException;
import com.sencide.FirstWebServiceStub.AddTwoNumbers;
import com.sencide.FirstWebServiceStub.AddTwoNumbersResponse;

public class TestClient {

 public static void main(String[] args) throws RemoteException {
  
  FirstWebServiceStub stub = new FirstWebServiceStub();
  AddTwoNumbers atn = new AddTwoNumbers();
  atn.setFirstNumber(5);
  atn.setSecondNumber(7);
  AddTwoNumbersResponse res = stub.addTwoNumbers(atn);
  System.out.println(res.get_return());
  
 }
}
Now you can run the above code as java application and you will get the output as 12 since we are adding 7 and 5. 
Download the Eclipse project of the above example (Password:sara)

25/01/2013
0 Poster un commentaire