[jboss-user] [JBoss Tools] - Simple CDI Hello World with Eclipse and JBoss Tools

Max Andersen do-not-reply at jboss.com
Tue Jan 4 05:06:47 EST 2011


Max Andersen [http://community.jboss.org/people/max.andersen%40jboss.com] modified the blog post:

"Simple CDI Hello World with Eclipse and JBoss Tools"

To view the blog post, visit: http://community.jboss.org/community/tools/blog/2010/12/10/simple-cdi-hello-world-in-eclipse

--------------------------------------------------------------
With JBoss Tools 3.2 Beta 2 it is simple to start using CDI (Context Dependency Injection).

All you need is the following:

*  http://www.jboss.org/jbossas/downloads/ JBoss AS 6 or another Eclipse supported CDI container such as Glassfish
*  http://community.jboss.org/community/tools/blog/2010/12/09/jboss-developer-studio-beta-2-is-ready JBoss Developer Studio 4 Beta 2 or another Eclipse with  http://community.jboss.org/community/tools/blog/2010/12/09/jboss-tools-32-beta-2 JBoss Tools 3.2 Beta 2 installed
* 5 minutes to watch or read the following blog

Here it is as a video walk through:

Media Description: Link: http://www.vimeo.com/moogaloop.swf?clip_id=17669512&server=www.vimeo.com&show_title=1&show_byline=1&show_portrait=0&color=&fullscreen=1



...and here are the steps in Screenshot form:

h2. Step 1: Install/Configure the Server runtime
The simplest way to setup JBoss runtimes is to use the "JBoss Runtimes" preference page which are available
via the Preferences or by simply pressing Ctrl+3 and type in "JBoss runtimes".

 http://community.jboss.org/servlet/JiveServlet/showImage/38-3430-10704/hellocdi_ctrl3runtimes.png  http://community.jboss.org/servlet/JiveServlet/downloadImage/38-3430-10704/385-144/hellocdi_ctrl3runtimes.png 
When pressing enter you should see the following Preferences page:

 http://community.jboss.org/servlet/JiveServlet/showImage/38-3430-10705/hellocdi_preferencepageruntimes.png  http://community.jboss.org/servlet/JiveServlet/downloadImage/38-3430-10705/450-230/hellocdi_preferencepageruntimes.png 
Click the "Search..." button and point it to a directory with your JBoss runtimes; in this case I got a few.

 http://community.jboss.org/servlet/JiveServlet/showImage/38-3430-10706/hellocdi_selectruntimefolder.png  http://community.jboss.org/servlet/JiveServlet/downloadImage/38-3430-10706/450-335/hellocdi_selectruntimefolder.png 
When you have selected the directory JBoss Tools will scan for runtimes it knows about and show
you what it found:

 http://community.jboss.org/servlet/JiveServlet/showImage/38-3430-10707/hellocdi_foundruntimes.png  http://community.jboss.org/servlet/JiveServlet/downloadImage/38-3430-10707/450-165/hellocdi_foundruntimes.png 
In this dialog you can then remove the checkmarks for runtimes you do not want configured or simply press OK and JBoss Tools will make all selected runtimes available from within the IDE.

For the Server parts it is now availabe in the server view:

 http://community.jboss.org/servlet/JiveServlet/showImage/38-3430-10708/cdihelloworld_servers.png  http://community.jboss.org/servlet/JiveServlet/downloadImage/38-3430-10708/265-105/cdihelloworld_servers.png 
>From here you can start the server (in this case JBoss 6) by clicking the Debug or Run icon. If you start via Debug icon the server will wait at any breakpoints you have set in your code and allow you to inspect the current state.

In any case when the server starts up it sends the output of the console to the Console View:

 http://community.jboss.org/servlet/JiveServlet/showImage/38-3430-10709/helloworld_cdi_console.png  http://community.jboss.org/servlet/JiveServlet/downloadImage/38-3430-10709/450-187/helloworld_cdi_console.png 
h2. Step 2: Create the Project
Now use the File > New > Dynamic Web Project or simply press Ctrl+N and find the "Dynamic Web Project" wizard

 http://community.jboss.org/servlet/JiveServlet/showImage/38-3430-10710/dynamic_web_project.png  http://community.jboss.org/servlet/JiveServlet/downloadImage/38-3430-10710/450-332/dynamic_web_project.png 
On the Wizard page, type in a project name (i.e. "cditest") and Click the "Modify" button under Configuration.

 http://community.jboss.org/servlet/JiveServlet/showImage/38-3430-10711/webprojectwizard.png  http://community.jboss.org/servlet/JiveServlet/downloadImage/38-3430-10711/450-474/webprojectwizard.png 
The Modify Configuration is where you can enable/disable what Eclipse calls project facets.
In this case we simply want to enable the Context Dependency Injection facet.

 http://community.jboss.org/servlet/JiveServlet/showImage/38-3430-10712/cdifacet.png  http://community.jboss.org/servlet/JiveServlet/downloadImage/38-3430-10712/450-277/cdifacet.png 
Now Click Ok and press Finish on the Wizard and a basic WAR project will be created for you.
 http://community.jboss.org/servlet/JiveServlet/showImage/38-3430-10713/cdiproject.png  http://community.jboss.org/servlet/JiveServlet/downloadImage/38-3430-10713/229-190/cdiproject.png 
h2. Step 3: Create the code
The code we will create are the minimal for printing out Hello World via a servlet.

We will need to add the following files:

* WebContent/WEB-INF/beans.xml to enable CDI fully for the project (in next revision of JBoss Tools this file will be created for you)
* src/org/cditest/HelloWorld.java the bean for giving the "Hello World" greeting.
* src/org/cditest/HelloServlet.java the servlet for rendering the greeting in its response method.

To create the bean.xml simply use Ctrl+N and type in "XML File" and choose the WebContent/WEB-INF folder and give it the name: beans.xml

 http://community.jboss.org/servlet/JiveServlet/showImage/38-3430-10714/beans.png  http://community.jboss.org/servlet/JiveServlet/downloadImage/38-3430-10714/450-477/beans.png 
The default file is empty, but we need to add the following content to make it work:

<beans/>

that will suffice for this blog; remember the beans.xml file is simply just
a marker file to inform the CDI container that the War archive should be scanned for beans.

Next on comes the HelloWorld and Servlet class - for this I will just paste the simplest code
which you can paste into two .java files or use Eclipse's excellent wizards for it. If you want to see the full steps for this you can watch the video above.

HelloWorld.java:

package org.cditest;
 
public class HelloWorld {
    public String getGreeting() {
        return "Hello World!";
    }
}


HelloServlet.java:

package org.cditest;
 
import java.io.IOException;
import javax.inject.Inject;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
 
@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
    @Inject private HelloWorld hello;
       
    public HelloServlet() {
        super();
    }
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {
        response.getOutputStream().println("<html><h1>" + hello.getGreeting() + "</h1></html>");
    }
}


If you look at the code of the HelloServlet you can see it simply a class with a @WebServlet annotation to specify the context/url name, an @Inject annotated field called Hello and a doGet() method which prints out the value of hello.getGreeting().

In short, the simplest example of @Inject annotation on a servlet.

h2. Step 4: Run the Servlet

>From within Eclipse you can use the Run As... or Debug As... toolbar menu to run what is the current selection and the tools will try and guess what kind of "Run" that means. i.e. for an XHTML page named home.xhtml it might be to open up a browser at  http://localhost:8080/home.seam http://localhost:8080/home.seam or in this case for a Servlet to open up the browser at the @WebServlet specified location relative to the deployment name, i.e.  http://localhost:8080/cditest/HelloServlet http://localhost:8080/cditest/HelloServlet.

 http://community.jboss.org/servlet/JiveServlet/showImage/38-3430-10715/runonserver.png  http://community.jboss.org/servlet/JiveServlet/downloadImage/38-3430-10715/450-243/runonserver.png 
For new projects like this example Eclipse will ask you what Server you wish to run on:

 http://community.jboss.org/servlet/JiveServlet/showImage/38-3430-10716/selectserver.png  http://community.jboss.org/servlet/JiveServlet/downloadImage/38-3430-10716/450-477/selectserver.png 
In this case I got several servers available, so I will choose the JBoss 6 server and
choose "Always use this server when running this project" so I will not have to answer
this question again on future Run's.

When the server is selected JBoss Tools will deploy the application and show the relevant page
in a browser:

 http://community.jboss.org/servlet/JiveServlet/showImage/38-3430-10718/browserhelloworld.png  http://community.jboss.org/servlet/JiveServlet/downloadImage/38-3430-10718/419-121/browserhelloworld.png 

h2. Step 5: Have fun!
That is it - and there is of course more functionallity in the CDI tooling such as validations, quick fixes and code completions but I thought it would be good to just show the simplest application imaginable.

But with the steps described above you now know how to setup any JBoss runtime, create any kind of project and enable the Eclipse facets you want on them.

If you want to see how easy it is to debug your applications then watch the video above, it has a little more details.

Hope you enjoyed it  ;) 

p.s. the exact same steps can be used to run against Glassfish or other CDI enabled servers that has Eclipse WTP server adapters.
--------------------------------------------------------------

Comment by going to Community
[http://community.jboss.org/community/tools/blog/2010/12/10/simple-cdi-hello-world-in-eclipse]

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20110104/897740f1/attachment-0001.html 


More information about the jboss-user mailing list