Archive for July, 2008

“Hello, World” Web Application using Struts 2 in NetBeans IDE 6.1

This tutorial guides you through the process of creating and running a basic “Hello, World” Struts 2 web application using the NetBeans IDE 6.1.

NOTE: I recommend trying to follow the NetBeans tutorial on the Struts 2 website first. If you find any problems following it, then come back to my tutorial. I have tried to make this more beginner friendly by including a lot of screenshots.

Download/Install Java SE, NetBeans and Struts 2

  1. Download and Install the latest version of Sun JDK: Sun Java SE 6 Update 7.
  2. Download and Install the latest version of NetBeans IDE: NetBeans IDE 6.1.
  3. Download the Struts 2 “Full Distribution” package(~90MB) and extract it in a directory. You can also opt for the smaller “Essential Dependencies Only” package if you want to later incrementally add additional packages as and when the need arises.

    Download Struts 2 Full Distribution or Dependecy Only package.

  4. Run the NetBeans IDE and create a new Web Application project in it. Name it “HelloStruts2World” or something like that.

    Create a new web application project in NetBeans.

    Don’t select any frameworks, especially Struts 1.2.9, to be added to the project.

Add Struts 2 Libraries to Web Application project in NetBeans and configure web.xml file

  1. Add the Struts 2 library files to the web application project: Expand the web application project structure in the Project pane if not already expanded; right-click on the Libraries folder and click the Add JAR/Folder… menu item.

    Add Struts 2 JAR library files to NetBeans Web application project.

    In the Add JAR/Folder file chooser dialog box, browse to the extracted Struts 2 directory, go to lib directory in it and select all the JAR files that you want to add to the project. Hold down the Ctrl button to select multiple files.

    Add basic Struts 2 JAR library files to NetBeans Web Application project.

    The following JAR library files are essential for most Struts 2 projects(and for this tutorial) but you can select more if you know that you need them(you can add additional libraries later also):

    • commons-logging
    • ognl
    • struts2-core
    • xwork
    • freemarker

    Libraries folder in your project will now look like this:

    Struts 2 JAR files added to Libraries folder of web project in NetBeans.

  2. Add a filter element to web.xml deployment descriptor to let Struts 2 handle all the requests for your web application. Expand the Configuration Files folder and double-click on web.xml file, select the XML tab and copy paste the following code in it.

    [xml]



    Struts 2 filter
    org.apache.struts2.dispatcher.FilterDispatcher


    Struts 2 filter
    /*



    30



    [/xml]

    You can enter the same data in Filters tab also:

Create Struts 2 action Java class and JSP result page and configure struts.xml file

  1. We need one or more Java packages to store all the Java classes(Struts 2 actions) in it. Let’s call the first package “hello.” Right click Sources Package directory, select New and then select Java Class… menu item. Enter a name for the Java class(say HelloStruts2World”) and enter a package name(say hello) and click Finish.
  2. Edit the HelloStruts2World.java file so that it looks like this:
    [java]
    package hello;
    import com.opensymphony.xwork2.ActionSupport;

    public class HelloStruts2World extends ActionSupport {
    private String userName;
    public String getUserName() {
    return userName;
    }
    public void setUserName(String userName) {
    this.userName = userName;
    }

    private String message;
    public String getMessage() {
    return message;
    }

    @Override
    public String execute() {
    message = “Hello, ” + userName + “.”;
    return SUCCESS;
    }
    }
    [/java]

  3. We now need to configure the Java class created in the above step as a Struts 2 Action and map it to a result page. To do so, we need to create two Struts 2 configuration files – struts.xml and struts.properties – in the source package.
    1. Right-click on hello package in Source Packages folder and select New and then select XML document… and enter struts as the file name. This will create struts.xml file.
    2. Right-click on hello package in Source Packages folder and select New and then select Properties File… and enter struts as the file name. This will create struts.properties file.

    Edit struts.xml file such that it has the following content:

    [xml]
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">


    /index.jsp

    [/xml]

    This maps the requests sent to the “/HelloStruts2World.action” url to HelloStruts2World.java Struts 2 action and after the successful execution of the request, it gets directed to the index.jsp result page. Leave the struts.properties file empty for now.

  4. Let us access the message property set by HelloStruts2World action and display it in the index.jsp page. Edit index.jsp so that it looks like this:

    [html]
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@taglib prefix="s" uri="/struts-tags" %>
    "http://www.w3.org/TR/html4/loose.dtd">




    Hello World!

    Struts 2 Message:


    Enter your name:




    [/html]

Run the Struts 2 Web Application Project in NetBeans 6.1 IDE

  1. The “HelloStruts2World” web application project structure, when relevant folders expanded, should look like this:

    Hello World Struts 2 web application project structure in NetBeans IDE.

  2. Run the web application by going to Run -> Run Main Project and then access the following URL in a web browser: http://localhost:8080/HelloStruts2World/. You may have to modify this URL depending on what port the web server is running(“8080″) and what is the name of your web application(“HelloStruts2World”).

    Struts 2 Action redirects to JSP "SUCCESS" result page

Other Options:

This is the manual way to develop Struts 2 web applications in the NetBeans IDE but it is at least more beginner friendly than working from the command line using just the Maven build tool. There is a Struts 2 plugin called nbstruts available for NetBeans; its functionality is currently limited but it is under active development. Other popular alternative IDEs for developing web applications using Struts 2 are Eclipse and IntelliJ IDEA.

Witty Twitter + Darrel’s Identi.ca patch = Witty Identi.ca Client

Witty Identi.ca Client?

Finally found some time to play with Darrel Miller’s Identi.ca patch for Witty Twitter client. Good news: it works.

Witty Twitter client patched for Identi.ca.

If you want to try it too, this is what I did:

  1. Checked out the source code of Witty Twitter client using TortoiseSVN subversion client on Windows Vista.
  2. Downloaded Darrel Miller’s Identi.ca patch for Witty.
  3. Applied the patch using TortoiseSVN(no patch/cygwin tools for Windows needed!).
    (I got errors related to ITweet.cs in the patch file, so I edited it manually; http://ur1.ca/0tx. Use my modified patch file if you get similar errors.)
  4. Built the patched Witty source code in Microsoft Visual C# Express Edition 2008(with no configuration changes).
  5. Ran Witty(from “Release” directory), entered Identi.ca login details(alas, you are out of luck if you use only OpenID based login), and there I have all my contacts’ Identi.ca messages :)
  6. I can upload the patched source code/built binary files somewhere if anybody wants to play with it without going through the patching/building business.

So this is fun.

Additional information:
What is Witty: “Witty is a free, open source Twitter client for Windows Vista and XP powered by the Windows Presentation Foundation (WPF). ”

What is WPF: “Windows Presentation Foundation (WPF) provides developers with a unified programming model for building rich Windows smart client user experiences that incorporate UI, media, and documents.”

What is Identi.ca: “Identi.ca is a micro-blogging service based on the Free Software Laconica tool.”

Setting Up Rails Development Environment on Ubuntu GNU/Linux

This is an adaptation to Ubuntu GNU/Linux platform of my previous post which was meant for the Windows platform: Setting Up Rails Development Environment on Windows Vista/XP

I can think of two different ways in which you might want to set up a development environment for Ruby on Rails on a GNU/Linux machine. One is to download everything outside of the package manager of your distribution i.e. build everything from the source. I am going to cover the second, easier way: how to setup Rails on a Debian/Ubuntu machine using its package manager for the most part. I show below how to setup Ruby, RubyGems, Rails, Mongrel, MySQL, and Subversion as part of the development stack.

  1. Installing Ruby:

    If not already installed, install the build-essential package first:

    # sudo aptitude install build-essential

    Then install the Ruby and related packages:

    # sudo aptitude install ruby irb ri rdoc rake libopenssl-ruby libsqlite3-ruby ruby1.8-dev
  2. Installing Rubygems

    Though it is possible to install Rubygems using the Ubuntu package manager, not only will you get an old version of rubygems if you do so, it also may not be compatible with the latest version of rails. I strongly recommend you download the source code package of the latest version of rubygems and install it using its setup script:

    # cd $HOME
    # wget http://rubyforge.org/frs/download.php/38646/rubygems-1.2.0.tgz
    # tar xvzf rubygems-1.2.0.tgz && cd rubygems-1.2.0
    # sudo ruby setup.rb

    Debian purists may frown at installing packages all over the system directories outside of control of the package manager. See Debian’s position on Rubygems. [via - RailsOnUbuntu]
    Remember to download the latest version of rubygems available(currently 1.2.0). You can remove the downloaded tgz archive and the extracted directory after the installation is finished.

    #cd $HOME
    # rm -r rubygems-1.2.0.tgz rubygems-1.2.0

    If Ubuntu complains that the ‘gem’ command is not found, you may have to create a symbolic link like this:

    # sudo ln /usr/bin/gem1.8 /usr/bin/gem

    You can update the rubygems package by running the following command:

    # sudo gem update ––system
  3. Installing Rails

    The final step is to install the Rails gem package itself.

    # sudo gem install rails

    This will install the latest Rails version on your Ubuntu GNU/Linux. If you ever want to update Rails in the future, just run the following command:

    # sudo gem update rails

    You can update the other packages using Ubuntu’s package management tools(aptitude) since that is how you installed those packages in the first place.

The basic Rails development environment is now installed on your system and you can skip the rest of the post if you are happy with WEBrick as the web server, SQLite as the database server and your favourite text editor/IDE that may be already installed on your system. The following instructions cover installing an alternate web server called Mongrel; MySQL database server and its GUI tools; and Subversion.

  1. Installing Mongrel

    Installing the Rails gem also installs the WEBrick web server which is ideally suited for development purposes. Another much recommended web server for Rails development as well as production environment is the Mongrel web server. To install the Mongrel web server, run the following gem command:

    # sudo aptitude install mongrel

    After installing Mongrel, Rails automatically starts the Mongrel instead of WEBrick web server when you run the Rails applications in development mode. (Refer to Mongrel documentation to know more about runing Rails applications under Mongrel in production mode.)

  2. Installing MySQL

    This step covers the installation of MySQL database server and its GUI tools. Skip this step if you want to use some other database server. Run the following command to install the required mysql packages:

    # sudo aptitude install mysql-server mysql-admin mysql-query-browser

    One important step during the configuration process is to select a root password(you will need to enter it in the database.yml configuration file of your Rails application).

  3. Installing Subversion

    Finally, install your favourite revision control software – CVS, Subversion, git etc. You can install subversion in this way:

    # sudo aptitude install subversion

Now you can create/checkout Rails applications, edit the files using your favourite text editor/IDE and run it under Mongrel. In future, I will try to write about developing Rails applications using integrated development environments like Eclipse and NetBeans.

Installing Grails in Ubuntu GNU/Linux Using Package Manager

In my previous post I talked about setting up Grails development environment in GNU/Linux distributions by manually downloading the packages and configuring the environment variables. The main advantage of this method is that you can work with the latest versions of the Groovy and Grails packages available, and in fact, you can work with more than one version of these software at the same time: just point your environment variables to different locations. The procedure is also more or less independent of a particular GNU/Linux distribution.

An easier, alternative way to install Groovy and Grails packages is to use the package management system of your favourite GNU/Linux distribution. The trade-off is that you have to accept the version of the packages that are available in your distribution’s repositories(apt-get) or you may have to compromise with the stability of your system(dpkg). Below I am documenting the procedure to install Grails development environment using package manager tools in Ubuntu 8.04(Hardy Heron).

  1. Installing Sun JDK 6 in Ubuntu 8.04 Using apt-get/aptitude

    In Debian/Ubuntu based operating systems installing new packages is as simple as running aptitude command with the package name.

    # sudo aptitude install sun-java6-jdk

    You also need to set JAVA_HOME environment variable in your preferred profile file(/etc/profile, $HOME/.profile or /etc/environment):

    JAVA_HOME=/usr/lib/jvm/java-6-sun

    Refer to the first step of previous post in my Grails series if you need more help.

    1. Installing Groovy in Ubuntu 8.04 Using apt-get/aptitude

      To install Groovy and its dependency packages, run the following command in a command shell:

      # sudo aptitude install groovy

      You can also install Groovy from Synaptic Package Manager, simply search for package name called “groovy” in it. But have a look at the version number!

      Synaptic Package Manager shows an old version of Groovy package in Ubuntu 8.04 package repository

      See what I mean? The latest stable version of Groovy is 1.5.6 though I would recommend using the 1.6 beta 1.

    2. Installing Groovy in Ubuntu 8.04 Using GDebi

      If you don’t mind picking up a deb file from a third-party source, Groovy download page has Debian/Ubuntu packages for both 1.5.6 stable version and 1.6 beta 1 version.

      Groovy latest stable version for Debian/Ubuntu family of GNU/Linux distributions available as a deb package.

      When you download the Ubuntu deb binary from this page Ubuntu will automatically open it using GDebi and install it for you.

      Download the Groovy deb package from its download page and install it with GDebi application.

    3. Installing Groovy in Ubuntu 8.04 Using dpkg

      You can also save the deb file on your computer and install it from the command shell using the dpkg tool:

      # sudo dpkg -i <name-of-the-downloaded-deb-file>
    1. Installing Grails in Ubuntu 8.04 Using apt-get/aptitude

      Unfortunately there is no Grails package in Ubuntu official repositories yet. You can try any unofficial repositories for Grails available on the Internet but only if you trust them. An old repository meant for installing Grails 1.0 RC2 on Ubuntu Gutsy was posted on Ubuntu Forums by prach. Have a look at it just as an example, I don’t recommend actually using it. I don’t know of a similar repository for more recent versions of Grails. So better option will be to install the latest deb package of Grails using GDebi or dpkg tools; see the next step.

    2. Installing Grails in Ubuntu 8.04 Using GDebi

      You can follow the above procedure(Step 2(b)) used for Groovy to install Grails too, by downloading its deb package from Grails’s download page.

      Download the Groovy deb package from its download page and install it with GDebi application.

      Then choose to open the package with GDebi installer.

      Download the Grails deb package from its download page and install it with GDebi application.

    3. Installing Grails in Ubuntu 8.04 Using dpkg

      You can also save the deb file on your computer and install it from the command shell using the dpkg tool:

      # sudo dpkg -i <name-of-the-downloaded-deb-file>
  2. You can now go to the post where I show how to create a basic “Hello, World” Grails application to test your Grails configuration. (You can ignore the first step of that post.)

Summary:
To summarize, you can install Sun JDK, Groovy and Grails packages in Debian/Ubuntu family of distributions using simple commands, like this:

# sudo aptitude install sun-java6-jdk
# sudo echo ‘JAVA_HOME=/usr/lib/jvm/java-6-sun’ >> /etc/profile
# wget http://dist.codehaus.org/groovy/distributions/installers/deb/groovy-1.6-beta-1.deb
# sudo dpkg -i groovy-1.6-beta-1.deb
# wget http://ant-deb-task.googlecode.com/files/grails_1.0.3-1_all.deb
# sudo dpkg -i grails_1.0.3-1_all.deb

You can use similar commands/tools for other GNU/Linux distributions, like yum in Fedora, yast in openSUSE, emerge in Gentoo, etc. All the above download links point to the latest versions at the time of this writing, so remember to change them appropriately when updated versions are released.

Setting Up Development Environment For Grails on GNU/Linux

In couple of my previous posts, I have explained how to install Grails and required packages to get a Grails development environment on the Windows platform. Continuing the Grails series, the current post explains how to set up a development environment for Grails web application framework on a GNU/Linux distribution. An alternative easier but less flexible method is noted to at the bottom of the post.

To setup Grails development environment on your GNU/Linux box, you first need to install Java SDK and Groovy packages.

  1. Install Sun Java SDK 6 according to the instructions for your GNU/Linux distribution.

    1. For example, in Ubuntu you can install it like this:
      • Make sure that the multiverse respository is enabled. Go to System -> Adminstration -> Synaptic Package Manager.
      • In Synaptic Package Manager, go to Settings -> Repositories.
      • Enable the multiverse repository if not already enabled.
        Enable multiverse and universe repositories in Ubuntu GNU/Linux to see all installable packages.
      • Click the Close button. Click Reload button on the toolbar to reload the package list.
      • In Synaptic Package Manager, search for the “jdk” package(use Edit -> Search or Search button on the toolbar).
        Search for Sun JDK 6 package in Synaptic Package Manager in Ubuntu GNU/Linux.
        Scroll down the result list until you find Sun JDK 6 package, then right-click on it and select “Mark for installation.”
        Mark Sun JDK 6 package in Synaptic Package Manager for installation.
        Click the Apply button on the toolbar to install the Sun JDK package.

      You can also install it from the command line in one simple step:

      # sudo aptitude install sun-java6-jdk

      You can similarly use ‘emerge’ command in Gentoo, ‘yum’ command in Fedora GNU/Linux distributions. You can also simply download the compressed Sun JDK 6 binary archive for GNU/Linux, uncompress it in the home directory and set its bin directory in the PATH environment variables. For more information, read Sun’s JDK 6 installation notes for GNU/Linux(Yes, it’s ugly).

    2. To confirm that Java is installed and available in the system path, run the following commands:
      tabrez@tabrez-ubuntu-vm:~$ java -version ; javac -version
      java version “1.6.0_06″
      javac 1.6.0_06
    3. Create a new system environment variable in your profile file (e.g. $HOME/.profile or /etc/.profile or /etc/environment – whatever you prefer the most) called JAVA_HOME and set it to your Java SDK installation directory path.

      export JAVA_HOME=/usr/lib/jvm/java-6-sun/

      Run the source command to apply the profile changes to the environment immediately without needing a session or OS restart.

      # source $HOME/.profile
      # echo $JAVA_HOME
      /usr/lib/jvm/java-6-sun/
  2. Download and configure Groovy package.

    Download the stable version of Groovy package.
    Download the compressed archive package of Groovy from its download page to your home directory and uncompress it.

    #cd $HOME
    # wget http://dist.groovy.codehaus.org/distributions/groovy-binary-1.5.6.zip
    # unzip groovy-binary-1.0.2.zip
    # mv groovy-binary-1.0.2 grails

    (Remember to download the latest stable version available on the Groovy download page.)

    Create GROOVY_HOME environment variable in your profile file(just like you created the JAVA_HOME variable) and set it to the directory where you have uncompressed the Groovy package. Add Groovy’s bin directory to the PATH environment variable

    export GROOVY_HOME=/home/tabrez/groovy/
    export PATH=$PATH:$GROOVY_HOME/bin

    Check the version of Groovy.

    # groovy -v
    Groovy Version: 1.5.6 JVM: 10.0-b22
  3. Download and configure Grails package.

    Download the stable version of Grails package.
    Download the compressed archive package of Grails from its download page to your home directory and uncompress it.

    #cd ~
    # wget http://dist.codehaus.org/grails/grails-bin-1.0.3.zip
    # unzip grails-bin-1.0.3.zip
    # mv grails-bin-1.0.3 grails

    (Remember to download the latest stable version available on the Grails download page. You can also download the compressed tar version(.tar.gz) if you don’t have the unzip command installed on your distribution.)

    Create GRAILS_HOME environment variable and set it to the directory where you have uncompressed the Grails package(just like you created the JAVA_HOME variable). Add Grails’s bin directory to the PATH environment variable.

    export GRAILS_HOME=/home/tabrez/grails/
    export PATH=$PATH:$GROOVY_HOME/bin:$GRAILS_HOME/bin

    Make the grails command executable and check the version of Grails.

    # cd $HOME
    # chmod +x grails/bin/grails
    # grails -v
    Welcome to Grails 1.0.2 – http://grails.org/
  4. Testing the Grails Development Environment on GNU/Linux.

    Now go to the post that explains how to create a Grails “Hello, World” web application to test if your Grails development environment is properly setup. It is for the Windows platform but I think that you should be able to adapt it to other platforms too. If you get into any problems, check if all the environment variables are properly set.
    Make sure that you have setup the JAVA_HOME, GROOVY_HOME, GRAILS_HOME environment variables correctly to get working Grails development environment.
    You can also post the error messages that you are getting somewhere on the Internet and provide the link to it in the comment section below.

  5. Next: Alternative easy way to set up Grails development environment using package managers

    There is also an easy way to setup Grails development environment using the package managers of different GNU/Linux distributions. Like installing Groovy and Grails packages in Ubuntu using the apt-get command. This easy model has certain limitations though like support only for old versions of Java, Groovy and/or Grails. I will discuss more about it in the next post. Done.

Go to Top