JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

Maven : Create Web Project in Eclipse

June 30, 2015 by Krishna Srinivasan Leave a Comment

This tutorial is a step-by-step guide for how to create web project in Eclipse using Maven. It is simple tutorial for the beginners.

It is a step-by-step tutorial for creating a simple Maven project in Eclipse IDE. In my earlier tutorials, I have written about Create Web Application Project With Maven and How to write custom plug-in for Maven. Here I will write how to create very simple web project using Eclipse.

Navigate to File -> New -> Maven Project

In the first step, if you select the “Create a simple project (skip archetype selection)”, it will skip the archetype selection dialog box and will create its own maven project. Archetypes are the predefined maven plug-ins to create a project. For example, if you want to create a web project, you would use the archetype “web” so that the folder structure will be created for web projects.

Maven Project In Eclipse

The below dialog box is for entering the “Group Id” and “Artifact Id” details.

Maven Project without Archtype

If you have not selected the “Create a simple project (skip archetype selection)” in the first dialog box, then you would be shown the below dialog box to select the archetype from the list. Here I am choosing the “web” archetype.

Select Archetype in Maven

maven-project-eclipse-4

The final folder structure would look like below. That’s it. Now you have maven project created in your eclipse.

Maven Folder Structure

Filed Under: Eclipse Tagged With: Eclipse

Spring Roo + Spring Data + MongoDB Repositories Integration

May 8, 2014 by Amr Mohammed Leave a Comment

Since Spring Data includes support for MongoDB repositories, we can use MongoDB as a persistence option when using Roo. We just won’t have the option of using the
active record style for the persistence layer, we can only use the repositories. Other than this difference, the process is very much the same as for a Spring ROO + Spring Data JPA solution.

In this tutorial you will learn how to use Spring Roo with Mongo Database. We have published in our previous tutorial on how to use Spring ROO with JPA. Spring Roo & JPA tutorial provided a common steps to install Spring Roo library, download Spring Roo plugin into your eclipse IDE and create the Spring Roo Project are already explained in our previous tutorial. Please refer that for more details.

I assume that readers who are reading this tutorial would have enough knowledge on working with Spring Data and MongoDB database. If not, please read our previous tutorial about Spring Data & MongoDB

1. Database Model

We’ve already learnt in our previous tutorial about creating a collection inside the MongoDB, so for completing this tutorial; Employee & Address collections should be created for that purpose.

Database Model

2. Setup Persistence Layer

Setting up the persistence layer steps have been already explained in Spring Roo + JPA. There is no need to repeat here. The next step is to setup the persistence layer through Spring Roo Shell.

The below snapshot should show you the Spring Roo Project Structure.

Spring Roo MongoDB Initial Project

After setting up the Mongo persistence layer by executing the command mongo setup command inside the Roo shell, the structure of the project folder looks like

Mongo Setup

Where the database.properties file contains the required information for the database that will be connected to the MongoDB. It’s possible for using the mongo setup –databaseName JavaBeat –host localhost command for creating an already configured file.

3. Create Entities

For creating the entities that will be used for persisting the collections inside MongoDB, you have to follow the below steps:

  • Create the Address entity by executing entity mongo –class ~roo.domain.Address inside the Roo Shell.
  • Add the addressCountry field by executing field string –fieldName addressCountry –notNull.
  • Add the addressCity field by executing field string –fieldName addressCity –notNull.
  • Create the Employee entity by executing entity mongo –class ~.roo.domain.Employee.
  • Add the employeeName filed by executing field string –fieldName employeeName –notNull.
  • Add the address field by executing field reference –fieldName address –type ~.roo.domain.Address –notNull.
  • One note here, is that the –activeRecord that already used in the Spring Roo & JPA has been eliminated, because of the Repository in the MongoDB implementation isn’t an optional. Unlike Spring Roo & JPA whose persisting operation could be done through using od standard EntityManagerFactory.

The project structure should be like

MongoDB Entities

4. Create Spring Data Repository

Spring Data Repository setup for MongoDB doesn’t differ from what’s happening in the Spring Roo & JPA. Here you just use a mongo keyword.

For creating Spring Data Repository, you have to execute the following commands.

  • For creating Employee Repository execute repository mongo –interface ~.roo.repository.EmployeeRepository –entity ~.roo.domain.Employee.
  • The string token ~ refers to initial package that you’ve provided at the first step, when you create the project as Roo Project.
  • For creating Address Repository execute repository mongo –interface ~.roo.repository.AddressRepository –entity ~.roo.domain.Address.

The project structure should look like this:

Mongo Repository

5. Create Web Layer

For creating a web layer with Spring Roo, you don’t need more than three additional commands. Those additional commands should be executed as their order shown below.
Executing of web mvc setup should setup the basic project structure for a Spring MVC/JSP application.

  • Executing of web mvc scaffold –class ~.roo.web.EmployeeController –backingType ~.roo.domain.Employee & web mvc scaffold –class ~.roo.web.AddressController –backingType ~.roo.domain.Address should create the controllers that used by Spring Roo for maintaining the CRUD functionality automatically.
  • Using of ~ notion is only for referring the package that you’ve created at the time of the project being created.
  • Add into your pom.xml the Spring-dao and Spring-tx dependencies. It’s possible for adding them through using of Spring Roo shell through using of dependency command.
  • Navigate into Project menu -> clean.
  • Right-Click on the project and choose Run As.
  • Select one of the installed Apache Tomcat.

Mongo Application View

Mongo Employees Mongo Addresses

6. Summary

Spring Data is an amazing framework, it provides the Spring Developer the ability to create a full Web Application from scratch. The application integrated with non relational database like MongoDB works much easier with Spring Data and Spring Roo. This tutorial focus on that subject, where an application has been created and integrated with the MongoDB using the Spring Roo tool. Hope you have enjoyed the tutorial. If you have any questions, please write it in the comments section.

Download Source Code

[wpdm_file id=88]

Filed Under: Spring Framework Tagged With: Eclipse, MongoDB, Spring Data, Spring ROO

Spring Roo + Spring Data JPA Repositories + Eclipse Integration

May 7, 2014 by Amr Mohammed Leave a Comment

Spring Roo is a rapid application development tool for Java developers. With Roo, you can easily build full Java applications in minutes. This tutorial isn’t intended for providing full coverage for Spring Roo, rather it should spot the focus to provide a good introduction for using Spring Data inside those applications that built with Spring Roo.

You knew that Spring Data provides a Repository concept for providing the full-fledged CRUD (Create, Read, Update & Delete) operations against whatever persistent stores you are use.

At this tutorial, you will be able to use the Spring Roo for creating a Web Application that uses the latest concept that you’ve learned about the Spring Data. Inside this tour, you just want to create your database models and Spring Roo will achieve everything you want inside your application, ranging from Web Layer into Persistence Layer.

  • Social Connection : Google+ | Twitter | FaceBook

1. Database Model

For simplifying the concept we introduced you the simple database model that you could have ever been applied; so it’s just an Employee & Address Tables, in that an Employee have a foreign key that used for referring the Address id. The below snapshots will clarify you the database models that we’re going to use inside this tutorial.

Spring Roo Address Table

Address Table

Employee Table

Employee Table

Employee Foreign Key

Employee Foreign Key

2. Spring Roo Library And Installation

To getting started using Spring Roo via command line or by integrating the Roo into your Eclipse IDE, you should install the Spring Roo into your machine and configure it. The installation and configuration process aren’t too complex. Follow the below steps for complete this installation.

  • Download the Spring Roo Zip file.
  • Unzip the downloaded file int your space. The supposed location that to be used at this tutorial is D:\SpringRoo\spring-roo-1.2.5.RELEASE.
  • Add the D:\SpringRoo\spring-roo-1.2.5.RELEASE\bin path into your path system properties. Such that addition should make the Spring Roo executable from your command line. For executing the Spring Roo from the Eclipse still an additional steps required.

Unzipped Spring Roo

3. Spring Roo Eclipse Plugin Installation

By default, if you’ve not installed the Spring Source Tool (SST), you shouldn’t be able to use the Spring Roo from the Eclipse IDE unless you install the Spring Roo plugin. This section will guide you step by step for install that plugin into your Eclipse IDE. The Eclipse IDE suggested here is Eclipse 4.3 Kepler.

By following the steps you will be ready for using the Spring Roo from you Eclipse IDE:

  • Determine the proper plugin version for your Eclipse IDE. Supposed Eclipse here is Eclipse Kepler 4.3 and for that we’ve been going to use SpringSource Update Site for Eclipse 4.3 link.
  • By copying the above link and paste it inside the install new software that you’ve got when you pressed on the Install New Software that located under help menu.

Spring Source Installation

  • Select Core / Spring IDE, Extensions / Spring IDE, Resources / Eclipse Integration Commons & Resources / Spring IDE and click next and install it after that.
  • Wait a while for the new plugins being installed and restart the Eclipse when the eclipse itself asked you to restart it.
  • After restarting the Eclipse IDE, you should notice a new Tool Tip Window that shown and titled with Spring Too Tip.

Spring Source ToolTip

  • Now, you are ready for getting started using the Spring Roo to develop a Web Application inside Eclipse IDE.

4. Create Spring Roo Project in Eclipse

For getting started using the Spring Roo, you have to create a Spring Roo project using the Eclipse IDE. Follow the below steps:

  • Open your Eclipse IDE.
  • Right-click on the Right Pane (Project Explorer) and select New -> Other.
  • From the Spring category, select Spring Roo Project & click next.
  • Name the project SpringData-Roo.
  • Type net.javabeat inside the Top Level package name.
  • Configure the Roo Installation by clicking on Configure Roo Installation.

Preferences Spring Roo

  • Select Roo Support and click Add.
  • From the opened dialog click browse and navigate into the Spring Roo parent directory. For this tutorial the parent directory is D:\SpringRoo\spring-roo-1.2.5.RELEASE and click Okay.

Configure Roo Installation

  • Click Okay until you returned back into Create Project Dialog.
  • From the Packaging Provider select the option Select a built-in provider.
  • From the packaging select WAR.

Create Roo Project

  • Click next and finish.
  • Wait a while for being Spring Roo initialized and the integrated Roo command line be available and ready for use.

Spring Roo Initialized

  • After you’ve seen the above screen, wait a while for being Roo Shell opened.

Spring Roo Shell

  • Spring Roo is installed and ready for use from your Eclipse IDE.

5. Setup Persistence Layer

For setting up the persistence layer you have to type the following command persistence setup –provider ECLIPSELINK –database MYSQL –databaseName javabeat –hostName 10.10.90.3 –userName root –password root.

By providing that instruction, Spring Roo will create the all persistence artifacts and include them inside your project.

6. Create Entities – Reverse Engineering

For creating the JPA entities, you’ve provided different ways for doing that. The simplest way is by Reversing the current tables that located inside your database by typing database reverse engineer –schema javabeat –package ~roo.domain –activeRecord false –disableVersionFields –disableGeneratedIdentifiers.

By executing that command inside your Eclipse IDE Roo Shell, you are about getting the entities from your Database by reversing the tables inside of it.

  • Be sure that you’ve provided the option –activeRecord false which means that we will have to provide the CRUD functionality using a Spring Data Repository.
  • In case you’ve faced any problem like missing of MySQL driver, you can add the MySQL driver library by typing osgi start –url file:///C:\mysql-connector-java-5.1.24-bin.jar into Roo Shell.

By finishing the above two steps, your project should look like the below snapshot.

Spring Roo Project Structure

  • All of those files related to the Persistence Configuration are added by executing the setup persistence layer Roo command.
  • Employee and Address entities are created by executing the reverse engineering Roo command.

7. Create Spring Data Repository

For creating Spring Data Repository, you have to execute the following commands.

  • For creating Employee Repository execute repository jpa –interface ~.roo.repository.EmployeeRepository –entity ~.roo.domain.Employee.
  • The string token ~ refers to initial package that you’ve provided at the first step, when you create the project as Roo Project.
  • For creating Address Repository execute repository jpa –interface ~.roo.repository.AddressRepository –entity ~.roo.domain.Address.

Repository Added

8. Create Web Layer using Spring Roo

For creating a web layer for for this application with Spring Roo, you don’t need more than three additional commands. Those additional commands should be executed as their order shown below.

  • Executing of web mvc setup should setup the basic project structure for a Spring MVC/JSP application.

Setup Basic Structure

  • Executing of web mvc scaffold –class ~.roo.web.EmployeeController –backingType ~.roo.domain.Employee & web mvc scaffold –class ~.roo.web.AddressController –backingType ~.roo.domain.Address should create the controllers that used by Spring Roo for maintaining the CRUD functionality automatically.

9. Spring Roo Example Application Demo

For executing the application as a whole, you have to put the build your application by navigating into Project menu and use click menu item. After your application is ready for use; just set the cursor into your project SpringData-Roo and select the Run As and select any Apache Tomcat Server installed.

The Apache Server that installed for this tutorial is v 7, and wait a while for being the Apache server run. Once the running phase has been done, the home page of your application should be displayed into your browser.

Spring Data Roo Home Page

By navigating into Create New Address you are able to create an address for being used later on.

Create Address

And by navigating into Create New Employee you have the chance to create a new Employee and associate it with already created Address.

Create Employee

Also, you have the ability to list all the employees and addresses that you have.

Employee List

Address List

10. Records Saved

The below snapshot shows you the records that persisted into database using the Spring Data Roo integration.

Data Persisted

11. Summary

Spring Data has transferred the development tasks into be much easier by integrating the latest newly development tool such as Spring Roo. Today, you can easily create your own Spring Data – JPA repository through one command, so no need to implement any interface or adding additional lines of codes for achieving what Spring Data had promised. If you have any questions, please write it in the comments section.

Download Source Code

[wpdm_file id=87]

Filed Under: Spring Framework Tagged With: Eclipse, Spring Data JPA, Spring ROO

JSF + Groovy Integration

April 10, 2014 by Amr Mohammed Leave a Comment

Groovy is another popular programming language for the Java Virtual Machine. Groovy is dynamically typed, and groovy code can be more concise than the equivalent Java because you can omit types of variables and parameters.

Almost any Java code is legal groovy, that’s make it simple and easier for getting started using it. Ultimately, using the Groovy within JSF applications isn’t more complex for one reason the using of groovy compiler to compile your groovy code does generate a .class file. In JSF, it doesn’t know, or care that you’re using groovy to generate them.

The JSF reference implementation  supports hot deployment of your Groovy code. When you change the groovy source files, the JSF implementation automatically recompile it and deploys the class files to the web application.

This tutorial you are going to integrate the Groovy with the JavaServer Faces using support of Eclipse IDE. This tutorial you will use the already created jsf maven project.

Tools Required For JSF + Groovy Integration

  • Eclipse IDE 4.3 (Kepler)
  • Tomcat Servlet Runner (7.0.35)
  • Groovy 2.0.x
  • JSF 2.0

1. Install Groovy Plugin for Eclipse

By using the Groovy download link you can choose the proper plugin for your Eclipse, but in our case you a have consider the link that provided for Kepler version. The steps required for installing the Groovy is as following:

  • Open Eclipse IDE
  • From Help menu choose Install New Software.
  • Inside Work with type the Groovy download link mentioned above.
  • From the fetched list, select Groovy-Eclipse.

Groovy Eclipse IDE - Download Groovy Plugin

2. Add Groovy Nature

If you’ve tried to create any type of Groovy files (groovy class, project, etc ..) you’re surely about getting complexity, cause your imported maven project doesn’t support Groovy naturally.

For adding Groovy you have to follow the below steps:

  • Right click on the created JSF project that imported into your eclipse IDE.
  • From Configure choose the option Convert To Groovy Project.
  • From Project menu select clean.
  • There is no specific perspective for Groovy, cause it’s considered as Java Project.

JSF 2 Groovy Add Nature

3. Groovy Library

Make sure that once you’ve added a groovy nature to your project,  the groovy library in it’s turn being added for it. Just look at your project structure and beside libraries such Maven Dependencies and JRE System Library you should see Groovy Libraries & Groovy DSL Support.

If you’ve not seen it, add it by the following steps:

  • Right click on your project.
  • From Grrovy Menu, select Add Groovy Library To Build Path.

But for those developers that focusing on using of maven you can consider the following dependency as mandatory.

Groovy Maven Dependency

[code lang=”xml”]
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.2.2</version>
</dependency>
[/code]

Either you are using the library added by the eclipse or use the library added by the maven, you are getting the right way and your application should running smoothly.

JSF 2 Groovy Add Library

JSF 2 Groovy Added Libraries

4. Groovy Class (Managed Bean)

To create a Groovy class that would be considering later as a managed bean, you have to follow the below steps:

  • Right click on the already created package and select New.
  • From the sub menu of new select Groovy Class.
  • Name your newly created Groovy class as IndexBean.
  • Staring to write a groovy class.

Your class should look like the below one:

IndexBean.groovy

[code lang=”java”]
package net.javabeat.jsf

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
class IndexBean implements GroovyObject{
private message = "Hello Mr. "; // Within groovy, you can omit the types
private userInput; // Within groovy, you can omit the types

public void setMessage(String message){
this.message = message;
}

public String getMessage(){
return this.message;
}

public void setUserInput(String userInput){
this.userInput = userInput;
}

public String getUserInput(){
return this.userInput;
}

public String navigate(){
return "anonymousView";
}
}
[/code]

  • Note using of properties without typos.
  • Note using of jsf2 @ManagedBean annotation
  • Note using of jsf2 @SessionScoped annotation
  • IndexBean groovy class defines two properties and one method named navigate that doesn’t consider any parameters and it will return a string.

JSF Groovy Adding Groovy Class

5. The View

index.xhtml

[code lang=”xml”]
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:view>
<h:form prependId="false">
<h1>JavaBeat JSF 2.2 Examples</h1>
<h2>JSF2 & Groovy</h2>
<br/>
<h:outputText value="#{indexBean.message}"></h:outputText>
<h:inputText value="#{indexBean.userInput}"></h:inputText>
<br/>
<h:commandButton value="Navigate Using Groovy Action" action="#{indexBean.navigate}"/>
</h:form>
</f:view>
</html>
[/code]

anonymousView.xhtml

[code lang=”xml”]
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:form prependId="false">
<h1>JavaBeat JSF 2.2 Examples</h1>
<h2>JSF2 & Groovy</h2>
<h3>Anonymous View</h3>
<h:outputText value="#{indexBean.message}"></h:outputText>
#{‘ ‘}
<h:outputText value="#{indexBean.userInput}"></h:outputText>
</h:form>
</html>
[/code]

6. Faces Configuration File

faces-config.xml

[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<application>
<resource-bundle>
<base-name>net.javabeat.jsf.application</base-name>
<var>msg</var>
</resource-bundle>
</application>
</faces-config>
[/code]

7. The Deployment Descriptor

web.xml

[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5" metadata-complete="true">
<context-param>
<description>State saving method: ‘client’ or ‘server’
(=default). See JSF Specification 2.5.2
</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
<context-param>
<param-name>javax.faces.application.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>
[/code]

8. JSF & Groovy Integration Demo

The below snapshots show you a running demonstration for a jsf application does use a Groovy programming language.

JSF 2 Groovy Example 1

JSF 2 Groovy Example 2

9. java.lang.NoClassDefFoundError: groovy/lang/GroovyObject

If you’ve followed the previous steps carefully, and your application doesn’t functional properly. Make sure that your Tomcat 7 has loaded the Groovy library in it’s classpath even your application has been compiled successfully. Also, you might be familiar with an error message that shown on the Eclipse console says

[code lang=”xml”]
SEVERE: Unable to load annotated class: net.javabeat.jsf.IndexBean, reason: java.lang.NoClassDefFoundError: groovy/lang/GroovyObject
[/code]

Even that your application has been running, but your groovy managed bean doesn’t initialized and try to navigate for seeing a big exception telling you that your managed bean is resolved to null.

Don’t worry, all things that you have to do is to assemble the groovy library into your server by following the below steps:

  • Right click on the project.
  • Choose properties, and point on Deployment Assembly.
  • Click Add button within the Web Deployment Assembly dialog.
  • Choose the Java Build Path Entries and click next.
  • Choose those libraries that mentioned for Groovy. and click finish.
  • Enjoy running your application.

JSF 2 Groovy Example 4

JSF 2 Groovy Example 5

JSF 2 Groovy Example 6

JSF 2 Groovy Example 7

[wpdm_file id=55]

Filed Under: JSF Tagged With: Eclipse, Groovy, JSF 2, JSF Integration

JSF 2 Installation & Configuration Using Eclipse

March 19, 2014 by Amr Mohammed Leave a Comment

JavaServer Faces (JSF) is a server side component framework for building Java Technology web-based application. JavaServer Faces (JSF) consists of two main parts:

  • The JavaServer Faces API for representing components, managing their states, handling events, handling server side validations, and achieving data conversion; defining pages navigation, supporting internationalization and accessibility and providing extensibility for all these features.
  • Tag libraries for adding components to the web pages and connecting those components into their corresponding server side objects.

This post should clarify how a JSF framework could be installed and configured by using Eclipse IDE support.

Also Read:

  • Introduction to JavaServer Faces
  • Request Processing Lifecycle phases in JSF
  • Navigation model in JSF

Tools Used

This post will use the following tools for achieving the required installation and configuration:

  • Eclipse Java EE IDE Kepler
  • JSF 2.2.

Create JavaServer Faces Project

Eclipse IDE doesn’t provide a specific project for creating a JavaServer Faces (JSF), so we need to create a general dynamic project and adding a JavaServer Faces (JSF) facet into it. The facets define a characteristics and requirements for Java EE projects and are used as a part of the runtime configuration, so adding a JSF facet into created project will help us installing the required JSF libraries and allow us to adhere the required view for dealing with the different parts of JSF application development.

The steps required for creating and configuring a JavaServer Faces project using Eclipse IDE are as follow:

  1. Create a new dynamic project.
  2. Right-Click on the created project -> select properties.
  3. While in a properties window, select project facets.
  4. Make sure that the version of your Dynamic Web Module is at least 2.5.
  5. Tick at the Java and select the version of 1.6 (it depends on your JDK that you are using)
  6. Tick at the JavaServer Faces and select the version of 2.2

By end of the previous steps, you should be informed about further configuration is required. Hover over the link and click it.

JSF Facet in Eclipse

Create JavaServer Faces Library

By clicking on the Further configuration required link, it’s the time for creating the JavaServer Faces library. To create library you have to follow the below steps:

  1. While a window of JSF Capabilities is opened, click on the download library.
  2. The JavaServer Faces library is being searched and you should at least see a JSF 2.2 (Mojarra 2.2.0) library. Mojarra is one of the implementations exist for JavaServer Faces (JSF 2).
  3. Tick the JSF 2.2 (Mojarra 2.2.0) library.
  4. Specify the library name that you would use it as a reference name for the installed JavaServer Faces library.
  5. Specify the Download destination that you would use a location for saving the binaries of the installed JavaServer Faces library.

You should get the same dialog as shown below

JSF Library Installed

  1. Click the next button for complete the installation.
  2. Tick the i accept the terms of this license.
  3. The JavaServer Faces library is being installed.
  4. Once the installation has been finished, you should see the JSF 2.2 (Mojarra 2.2.0) library is listed as a choice in the JSF Implementation Library.
  5. Tick the JSF 2.2 (Mojarra 2.2.0) listed library.
  6. Click Okay to close the dialog.
  7. Click Okay  to close the project facets dialog.

By ending the above steps, you are ready for using JavaServer Faces in your project.

Configure Apache Tomcat

If you’ve ever been configured the Apache Tomcat for executing a JSF samples, you’ve requested to install and configure it for next coming use. Follow the below steps for making Apache Tomcat for use.

  • From the dynamic project and underneath of Target Runtime -> Select New Runtime

Eclipse Tomcat Configurations

  1. Select Apache Tomcat v7.0 -> Click Next
  2. Type the name of the server that you’ve been installing.
  3. Navigate into the destination path of the Apache Tomcat 7.0 (Download the Apache Tomcat 7.0).
  4. Click on the server pane by Window -> show view -> Servers or Other -> Servers.
  5. You should find the following message (No Servers are available. Click this link to create a new server …)
  6. Choose Tomcat 7.0
  7. Let’s the remaining configuration without touch.
  8. Click next and finish.

Adding of Apache Server

  • The server is installed and is ready for use

Create JavaServer Faces Page

After creating, configuring and installing the all libraries that required to start using of JavaServer Faces, it’s time to create the first Hello World sample.

  • Right-Click the project’s WebContent.
  • Select New -> HTML File.
  • Name your file with an index.xhtml -> Click Next
  • Select New XHTML File (1.0 strict) -> Click Finish

The page that you will create it must be as below

[code lang=”xml”]

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:outputText value="Hello World !"></h:outputText>
</html>

[/code]

Run index.xhtml

To run the index.xhtml you should follow the below steps:

  • Righ-Click on the index.xhtml
  • Select Run As
  • Select the configured Apache Server that you’ve done before.
  • Click finish.
  • You should see the message Hello World !

Filed Under: JSF Tagged With: Eclipse, JSF 2

Create JSP Page in Eclipse

January 16, 2014 by Krishna Srinivasan Leave a Comment

  • Java EE Tutorials
  • JSP Tutorials
  • Recommended Books for Java Server Pages (JSP)

Simple JSP Example using Eclipse

Let us write a simple JSP code using Eclipse IDE and execute it using a web application server (Tomcat).

Tools Used

We are using the following tools to write and execute our JSP code:

  • Java 7
  • Eclipse IDE
  • Tomcat 7

Here we are assuming that the reader is well aware of the above mentioned tools and has knowledge of installation of these tools.

Once all the above mentioned tools are installed and configured, open the Eclipse IDE. Follow the below steps to create a simple JSP file using Eclipse.

Step 1: Create a Dynamic project as shown in the image below:

jsp_dynamicproject
Figure 1: Dynamic Project Creation

Step 2: Type the project name as “HelloWorldJSP” and click Next.

jsp_projectnameFigure 2: Name the project

Step 3: Now select the checkbox “Generate web.xml deployment descriptor” and click on Finish button.

jsp_createprojectFigure 3:Finish

Step 4: Once the project is created, you can see the project structure as in the screen below:

jsp_projectstructureFigure 4:Project Structure

Step 5: Now create a JSP file under the WebContent folder as shown below:

jsp_createjspfileFigure 5:Create JSP file

Step 6: Name the file as hellojavabeat.jsp and click on Finish button.

jsp_filenameFigure 6:Give JSP file name

Step 7: Now open the file hellojavabeat.jsp and paste the below contents in it:

[code lang=”html”]
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<body>
<% out.print("hello to Javabeat");%>
<p>Today’s date: <%= (new java.util.Date())%></p>

</body>
</html>
[/code]

Here we are printing a message “hello to Javabeat” and current time using the tags which are called scriptlets. More about scriptlet will be discussed in next chapter.

Step 8: Now to execute this JSP, we need to configure a web server (in our case it is Tomcat 7). Now select the file hellojavabeat.jsp and select Run As > Run on Server as shown below:

jsp_executejspfileFigure 7:Execute JSP file

Step 9: Choose the Tomcat version on your system. In our case it is “Tomcat v7.0 Server” and click Next.

jsp_configserver1Figure 8:Configure Tomcat

Step 10:Set the Tomcat path (installed path) and click Next.

jsp_settomcatpathFigure 9: Set Tomcat Path

Step 11: Click Finish

jsp_configserver2

Step 12: Now that you have configured web server with Eclipse, now run the JSP file by selecting Run As > Run on Server. The server will be started and output as shown in the screen below opens up:

jsp_output
Figure 10: Output

Previous Tutorial : JSP Architecture and Lifecycle   ||  Next Tutorial :  JSP Syntax

Filed Under: Java EE Tagged With: Eclipse, JSP Tutorials

How To Resolve “Resource Is Out Of Sync With The Filesystem” Error in Eclipse?

November 28, 2013 by Krishna Srinivasan Leave a Comment

The error in eclipse IDE Resource Is Out Of Sync With The File system happens many time when the resource is out of sync and may be updated by some other sources. We need refresh each time by default to load that files. There is settings in eclipse to permanently fix this problem. Look at the below window to resolve that error. Open the preference window in eclipse and check the option “Refresh using native hooks or pooling” or “Refresh Automatically”. To go to that option follow the path:

Window -> Preferences -> General -> Workspace

Refresh Eclipse IDE Files Sync

Eclipse is using poling mechanism to load the files. It might impact the performance for some users.

Filed Under: Eclipse Tagged With: Eclipse

How To Increase Heap Size in Eclipse?

November 26, 2013 by Krishna Srinivasan Leave a Comment

If you get the error java.lang.OutOfMemoryError: Java heap space while running Java applications, it means that your Java application environment don’t have the sufficient space to store the values needed for your application. This could be because of the following reasons:

  • By mistake, you have set the less memory for your Java environment (or)
  • Your epplication is huge and allocated memory is not enough for your application’s size

In the either case, increasing the size of the memory is the solution. You can update the VM arguments as below to fix this issue. You have to select the application and update the required value as -Xms512M -Xmx1024M. This would resolve the problem while that application is running.

eclipse-heap-size

By default, eclipse.ini file under the Eclipse folder has the default configuration for the heap size. Note that it is used only for the Eclipse IDE. When you build your project using ANt / Maven tools, the external JVM is used and the values used in the eclipse.ini will not be used. If you update the values also will not resolve the problem. The sample eclipse.ini file looks like this. launcher.XXMaxPermSize parameter has to be increased for the heap size for your eclipse environment.

[code]
-startup
plugins/org.eclipse.equinox.launcher_1.3.0.v20120522-1813.jar
–launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_1.1.200.v20120522-1813
-product
org.eclipse.epp.package.jee.product
–launcher.defaultAction
openFile
–launcher.XXMaxPermSize
256M
-showsplash
org.eclipse.platform
–launcher.XXMaxPermSize
256m
–launcher.defaultAction
openFile
-vmargs
-Dosgi.requiredJavaVersion=1.5
-Dhelp.lucene.tokenizer=standard
-Xms40m
-Xmx512m
[/code]

Filed Under: Eclipse Tagged With: Eclipse, heap

Convert Java Project to Web Project in Eclipse

November 26, 2013 by Krishna Srinivasan Leave a Comment

This tutorial is for adding a simple tips to the Eclipse developers. When you have a Java project already created in your eclipse, now you want to convert that same project to a Web project instead of creating a new project. We can do that easily using the property window for that specific project.

1. Open the property window

eclipse-java-to-web-project-1

2. Click convert to facet Form

eclipse-java-to-web-project-2

3. Select Dynamic Web Module from the list

eclipse-java-to-web-project-3

4. Click on the OK Button

Once you click the OK button, it will generate the required files for the web project and create the project structure.

eclipse-java-to-web-project-4

Filed Under: Eclipse Tagged With: Eclipse

How to Attach JDK source with Eclipse IDE?

November 11, 2013 by Krishna Srinivasan Leave a Comment

This tutorial explains about how to attach JDK source code to your working environment. When you want to look at particulat source code, we can use CTRL+Click on the class name to refer the source code. However, by default developer can not refer JDK’s core Java files. It is referenced as the class files and you will see only the class files and will not see source code. You can link the source code under the JDK installation folder to enable the linking of source code for all the Java core classes.

You can navigate to this path for enable this option : Windows -> Preference -> Java -> Installed JREs -> Select a JRE -> Click Edit -> Select rt.jar -> Click Source Attachment -> Click Finish. It is done.

eclipse-1

eclipse-2

Filed Under: Eclipse Tagged With: Eclipse

  • 1
  • 2
  • Next Page »

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved