Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1199116
  • 博文数量: 89
  • 博客积分: 10546
  • 博客等级: 上将
  • 技术积分: 1510
  • 用 户 组: 普通用户
  • 注册时间: 2004-10-16 01:24
文章分类

全部博文(89)

文章存档

2012年(7)

2011年(4)

2010年(5)

2009年(52)

2008年(21)

分类: Java

2011-02-20 16:14:18

In this post, I will demonstrate how to create a JEE6 application step by step. Before you create the application, you must prepare the development environment.


1.Prerequisite

I assume you have installed the latest SUN/Oracle JDK 6, make sure the JAVA_HOME path variable is set properly and the JAVA_HOME/bin is added to the system path.


Install Eclipse

Download Eclipse 3.6 from . Choose Eclipse IDE for Java EE Developers which provides excellent features for JEE development. You should also install the M2Eclipse plugin when you are using maven to build your application.

M2Eclipse update site: .


I also suggest you install the Jboss Tools from jboss.org community, even you select Glassfish as deployment server. Jboss Tools provides some extra JEE development features which are not available in the standard Eclipse JEE bundle, such as improved Facelet based XHTML editor, CDI support etc.

Jboss Tools update site: .


Install Apache Maven

Download Apache Maven from . I select the 2.2.1 version for this tutorial. Unzip the archive to your disk, and set M2_HOME path variable to the uncompressed folder, and add M2_HOME/bin to your system path.

Execute “mvn -version” in your cmd console to verify Apache Maven has been installed properly.

Apache Maven 2.2.1 (r801777; 2009-08-07 03:16:01+0800)

Java version: 1.6.0_23

Java home: D:\jdk6\jre

Default locale: zh_CN, platform encoding: GBK

OS name: "windows 7" version: "6.1" arch: "x86" Family: "windows"


Install Glassfish

Download Glassfish v3 from http://glassfish.dev.java.net. Glassfish distribution is available in OS dependent installer and cross platform archive format, I select the cross platform archive, it can be used in all operation system, not only for Windows system. Unzip it to your disk.

Open Eclipse IDE, and open “Servers” view (Click Windows->Show View->Others, input “server” to filter results tree, and select “Servers” ).


Right click in the white space in the Servers view, select “New”->”Server” in the context menu and open “Define New Server” dialog.



If you can not find the Glassfish options in the list, I think you should install the Glassfish server adapters firstly. There are two approaches to accomplish this work.

  1. Click the “Download additional server adapters” link in the dialog to download the Glassfish server adapters.

  2. Install it from Oracle Enterprise Pack for Eclipse, the update site url is .

Click “Next” button and click “Browse” button to choose the Glassfish server folder.


Click “Finish” button to close the wizard dialog, you will see a Glassflass node in the Servers view.

2. Create application

Create a JEE6 web application with Maven

Execute the following command line in the system console.

mvn archetype:generate -DarchetypeRepository=

Input the number of webapp-javaee6 :

...

256: remote -> webapp-javaee6 (Archetype for a web application using Java EE 6.)

...

Choose a number: 101:

Here the number is 256, maybe it is a different number in your system.

Input the number and hint Enter key, it will provide prompt for the necessary attributes, answer one by one, and you will see the application is being generated by Maven.

Choose a number: 101: 256

Choose version:

1: 1.0

2: 1.0.1

3: 1.0.2

4: 1.1

5: 1.2

6: 1.3

Choose a number: 6:

Define value for property 'groupId': : jee6webtest

Define value for property 'artifactId': : jee6webtest

Define value for property 'version': 1.0-SNAPSHOT:

Define value for property 'package': jee6webtest:

Confirm properties configuration:

groupId: jee6webtest

artifactId: jee6webtest

version: 1.0-SNAPSHOT

package: jee6webtest

Y:

[INFO] ------------------------------------------------------------------

[INFO] BUILD SUCCESSFUL

[INFO] ------------------------------------------------------------------

[INFO] Total time: 14 minutes 36 seconds

[INFO] Finished at: Sat Feb 19 17:23:50 GMT+08:00 2011

[INFO] Final Memory: 11M/27M

[INFO] ------------------------------------------------------------------


If you are the first time to use Maven in your system, it will take several minutes to fetch essential dependencies from Apache Maven central repository.


Import codes into Eclipse workspace

Open Eclipse IDE, and import the application into Eclipse workspace. Click “File”->”Import...” in the Eclipse menu bar and open the Eclipse import project dialog. Select “Maven” in the tree list and expand the tree and select “Existing Maven Project”, click “Next” button.



In the next step in the wizard, click “Browse” button to select the project folder and click “Finish” to import the project codes into the Eclipse workspace.




When it is ready, switch Eclipse to “Java EE perspective”, it is friendly to develop a JEE application.

In this tutorial, I do not want to make thing complex, I will create a Session Bean and a JSF ManagedBean and JSF Facelet XHTML. I will improve the application in the further post.


Create a Session Bean

Open “New” in the File menu, and open New File dialog, select “EJB”->”Stateless Session Bean 3.x”, and click “Next”, input the class name “HelloBean”, click “Finish” button.


@Stateless

@LocalBean

public class HelloBean {


/**

* Default constructor.

*/

public HelloBean() {

// TODO Auto-generated constructor stub

}


public String sayHello(String from) {

return ("Say Hello to JEE6 from " + from + " at " + new Date());

}

}


There I added a sayHello method in this Session Bean. It is a none-interface local bean which is a new feature introduced in EJB 3.1


Create a JSF ManagedBean

Create a Class named “Helloworld”, added ManagedBean and RequestScoped annotations to the class.


@ManagedBean(name="helloworld")

@RequestScoped

public class HelloWorld {

@EJB

HelloBean bean;


public String sayHello() {

return bean.sayHello("hantsy");

}

}


In a JEE6 web application, JSF2.0 is ready for use, there is no need extra configuration for JSF2, you also does not need a web.xml for the web application.


Create a Facelet XHTML page

Create a XHTML page using Eclispe wizard.


DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns=""

xmlns:ui=""

xmlns:h=""

xmlns:f="">


JEE6 Web Appliation: #{helloworld.sayHello()}

html>


We have defined a ManagedBean named “helloworld”, here it can be called via expression language in the web pages.


3.Run on Glassfish Server

Now you can run the application on Glassfish application server. Right click the project node in the Project perspective. Select “Run as”-> “Run on Server”, select the configured Glassfish instance.

It will start the Glassfish server if the Glassfish is stopped.

Go to , you will get the result page like the following.




In this tutorial, you have learned how to create a simple maven based JEE6 web application with Eclipse, and how to run the application on the Glassfish application server.

阅读(2893) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~