Every Flavour Beans

“The time has come…to talk of many [technologies].” –Lewis Carroll(’The Walrus and the Carpenter’)
Development Tools. Web Frameworks. GNU/Linux. Nokia N800. Video Encoding.

October 5, 2008

Installing Sun Java SE 6, Maven 2 and Tomcat 5.5 on Fedora GNU/Linux

Filed under: Java, Struts 2 — tabrez @ 12:36 pm

For using most of the enterprise technologies based on Java, you generally need at least the following components installed and configured on your system:

  1. Sun Java SE (or EE) SDK
  2. A command line build tool like Ant or Maven
  3. A Java application server/container like Tomcat, Jetty, Glassfish, JBoss etc.

Based on which Java based technology/framework you want to use, you may need to install further dependencies. Even though you may not need all the three components mentioned above to be able to work with all Java technologies, I found myself installing them way too many times when compared to any other Java component that I wanted a place to document the installation process of these three components specifically, if only to refer to this post from other Java tutorials in future.

Installing OpenJDK/Sun Java SE 6, Apache Maven 2 and Tomcat 5 on Fedora GNU/Linux

Installing Java SE 6
You can’t install Sun’s JDK directly from the Fedora repositories, you have the following two choices:

  1. Use OpenJDK that comes installed by default in Fedora 9(if not, you can install it with a simple ‘yum install java-1.6.0-openjdk’ command; or first search for the exact package name using ‘yum search jdk’). If you want to go with Fedora’s OpenJDK then you don’t have to do anything else, except perhaps set JAVA_HOME environment variable to OpenJDK installation path in your profile file.
  2. Install Sun’s latest JDK by manually downloading the binaries from Sun’s website. If you are very specific about using Sun’s official JDK only, then you first have to un-install OpenJDK that is installed by default in Fedora 9. Then you need to download Sun’s JDK and run the installer. (Download the Java EE SDK if you want to install the enterprise edition.) Assuming you have downloaded Sun JDK binary and saved it in your home directory under a name like jdk-6u7-linux-i586.bin, run the following commands to complete the installation:
    # yum remove java-1.6.0-openjdk java-1.6.0-openjdk-plugin
    # yum install compat-libstdc++-33 compat-libstdc++-296
    # cd ~ & chmod +x jdk-6u7-linux-i586.bin
    # ./jdk-6u7-linux-i586.bin
    # java -version
    java version “1.5.0″

    As a final step, add the following line to your profile file(e.g. .bash_profile) to set the JAVA_HOME environment variable to the path where you have installed Sun JDK:

    export JAVA_HOME /opt/jre/jdk

    Make JAVA_HOME point to OpenJDK installation path if you are using OpenJDK.

Installing Apache Maven2
Apache Maven 2 can be installed using yum command:

# yum install maven2
# mvn ––version
Maven version 2.0.4
# yum remove maven2

Installing Maven 2 on Fedora 9 using the yum package manager gave me the old 2.0.4 version, so I removed it and decided to install Maven manually. To manually download and extract Maven 2 archive on Fedora 9, follow these instructions:

  1. Download the latest stable version of Apache Maven 2 archive from its download page.
  2. Extract it to your home directory and rename the directory to ‘maven2′
    # tar xjvf apache-maven-2.0.9-bin.tar.bz2
    # mv apache-maven-2.0.9-bin maven2
  3. Add the following line to your profile file(e.g. .bash_profile or /etc/profile) to set maven2 executables in system path:

    export PATH=$PATH:$HOME/maven2/bin

  4. Confirm if Maven 2 is installed and configured correctly:
    # source .bash_profile
    # mvn ––version
    Maven version 2.0.9
    Java version 1.5.0

Installing Tomcat 5 and Jetty
You can finally install tomcat and jetty servers using yum command:

# yum install tomcat5 jetty

The installation procedure on other GNU/Linux distributions should be on similar lines using their respective package managers and I will try to post the procedure for Ubuntu, Gentoo, openSuse and Mandriva distributions in the near future.

Testing the installation

To test the above installation, see my next post(to be published), “Hello, World” Java Web Application using Java SE 6 + Tomcat 5.5 + Maven 2.”


If you want to receive future posts by email, enter your email address here:

Related Posts:

  • Installing Sun Java SE 6, Apache Maven 2 and Tomcat 5.5 on Windows OS
  • Java on Gentoo
  • Installing C++ Boost on SuSE and Fedora
  • Setting Up Development Environment For Grails on GNU/Linux
  • Installing Grails in Ubuntu GNU/Linux Using Package Manager
  • “Hello, World” Web Application using Struts 2 in IntelliJ IDEA 8.0 M1
  • 7-Zip Compression Format Support on GNU/Linux Using p7zip

  • Readers who viewed this page, also viewed:


    August 22, 2008

    Installing Sun Java SE 6, Apache Maven 2 and Tomcat 5.5 on Windows OS

    Filed under: Java, Struts 2 — tabrez @ 8:49 pm

    For using most of the enterprise technologies based on Java, you generally need at least the following components installed and configured on your system:

    1. Java SE or EE SDK
    2. A command line build tool
    3. A Java application server/container like Tomcat, Glassfish, JBoss etc.

    Based on the Java based technology/framework you want to use, you may need to install further dependencies. Even though you may not need all the three components mentioned above to work with every Java technology, I found myself installing them way too many times when compared to any other Java component that I wanted a place to document the installation process of these three components specifically, if only to refer to this post from other Java tutorials.

    Below I explain how to install Java SE 6, Apache Maven 2 and Apache Tomcat 5.5 software.

    Installing Sun Java SE 6 on Microsoft Windows

    Download Java SE 6

    Download and install the latest version of Java SE(currently JDK 6 update 7). Just run the installer and follow the wizard. Note the installation directory where the wizard installs the Java package(default will be C:\Program Files\Java). You then need to set the two environment variables JAVA_HOME and PATH.

    Create Java Environment Variables

    Go to Control Panel -> System -> Advanced System Settings and click the “Environment Variables” button. Or Control Panel -> System, select the Advanced tab and click the “Environment Variables” button if you are using Windows XP.

    1. Click the New… button below the System variables pane and add a new environment variable called JAVA_HOME and set it to the path to your Java installation directory(e.g. C:\Programming Files\Java).

      Set Java environment variable JAVA_HOME in Windows Vista and XP

    2. Select the Path variable in System variables pane and add the path to Java bin directory to it.

      Set Java environment variable PATH in Windows Vista and XP

    Check Java Version and Confirm Install

    Let us confirm the correct configuration of Java installation from the command line. Close all the open dialog boxes and start a new command prompt(Start/All Programs -> Accessories -> Command Prompt). Run the following commands in it(doesn’t matter what your current directory is):

    $ echo %JAVA_HOME%
    C:\Program Files (x86)\Java\jdk1.6.0_06
    $ java -version
    C:\Program Files (x86)\Java\jdk1.6.0_06

    If you get an error saying that the ‘java’ command is not recognised or is not available, then you need to check the values the above environment variables were set to once again.

    For more details, refer to Sun’s instructions on how to install Java SE.

    Installing Apache Maven 2 on Microsoft Windows

    Download Maven 2

    Download the latest stable version of Maven from Apache Maven Project website. Extract it to a directory and rename the installation directory to maven. I assume Maven is now located in c:\maven.

    Download Apache Maven 2 package from its website.

    Extract the downloaded Apache Maven 2 compressed package to a directory.

    Create Maven 2 Environment Variables

    Create a new M2_HOME environment variable. Click the New… button(below System Variables pane) and enter the details as follows:

    Set Apache Maven 2 environment variable M2_HOME in Windows Vista and XP

    Similarly create another environment variable called M2 and set it to the value %M2_HOME%\bin

    Set Apache Maven 2 environment variable M2 in Windows Vista and XP

    Add %M2% to the Path variable.

    Add Apache Maven 2 bin directory to environment variable PATH in Windows Vista and XP

    Check Maven 2 Version and Confirm Install

    To check if Maven is properly configured or not, start a command prompt and check its version:

    $ echo %JAVA_HOME%
    C:\Program Files (x86)\Java\jdk1.6.0_06
    $ mvn –version
    Maven version: 2.0.9
    Java version: 1.6.0_06

    For more details, refer to instructions on Maven’s website on how to install Maven 2.

    Installing Apache Tomcat on Microsoft Windows

    Download Tomcat 5.5

    Download Apache Tomcat 5.5(or any other version of your choice) and extract the package in a directory(say C:\tomcat).

    Download Apache Tomcat Java application server 5.5 or 6.

    Create Tomcat Environment Variables

    Next we need to add the required environment variables.

    1. Edit the Path environment variable and add the path to Tomcat’s bin directory to it.

      Add path to Tomcat's bin directory to environment variable PATH in Windows Vista and XP.

    2. Create a new environment variable called CATALINA_HOME and set it to the directory where Tomcat is installed. I had extracted the Tomcat package to c:\tomcat directory in earlier step, so I would set CATALINA_HOME to c:\tomcat.

      Create CATALINA_HOME environment variable and set it to path to Tomcat's home directory in Windows Vista and XP.

    Check Tomcat Version and Confirm Install

    Verify Tomcat’s installation by running the following commands in the Windows command prompt:

    $ echo %JAVA_HOME%
    C:\Program Files (x86)\Java\jdk1.6.0_06
    $ echo %CATALINA_HOME%
    C:\tomcat
    $ version.bat

    You will need to start and stop the tomcat service from the command line manually(or tools from other Java frameworks may do that for you automatically). But if you want to the tomcat server to start automatically when the operating service, you can install it as a Windows service. For more details, refer to instructions on Tomcat website on how to run Tomcat as a Windows service. I am not including those instructions here because I don’t run and don’t recommend running it as a service; YMMV.


    If you want to receive future posts by email, enter your email address here:

    Related Posts:

  • Installing Sun Java SE 6, Maven 2 and Tomcat 5.5 on Fedora GNU/Linux
  • Java on Gentoo
  • “Hello, World” Web Application using Struts 2 in IntelliJ IDEA 8.0 M1
  • Installing C#, Mono(.NET) & MonoDevelop in Ubuntu Dapper Drake 6.06
  • “Hello, World” Web Application using Struts 2 in NetBeans IDE 6.1
  • Java 1.5 and Eclipse in Gentoo - Part II
  • Setting Up Development Environment For Grails on Windows Vista/XP

  • Readers who viewed this page, also viewed:


    August 20, 2008

    “Hello, World” Web Application using Struts 2 in IntelliJ IDEA 8.0 M1

    Filed under: Java, Struts 2 — tabrez @ 7:19 pm

    At last we have a Java IDE that ships with Struts 2 support built-in: no manual configuring the web applications to add Struts 2 support and no more hassles of finding and installing Struts 2 plugins.

    JetBrains released IntelliJ IDEA 8 Milestone release 1 a week ago and the milestone version includes support for Struts 2 out-of-the-box(no plugins need to be installed) so that we can test the functionality right away. Any loose ends should be tied up by the time the final version is released.

    Installing IntelliJ IDEA 8 M1 and Other Required Components

    1. Download and install the latest version of Sun JDK: Sun Java SE 6.
    2. Download and install Apache Tomcat web application server: Apache Tomcat 5.5.
    3. Download and install the 30-day trial version of the latest IntelliJ IDEA 8 version; currently Milestone 1 is available, but when you read this, probably another milestone, or beta, or even the final version may have been released, so make sure that you download the most recent release of IntelliJ IDEA 8. I show the screenshots from Windows Vista operating system in this post but it should work similarly for Mac and GNU/Linux too.

      Download 30-day trial version of JetBrains IntelliJ IDEA 8.0 Milestone 1 Java IDE with Struts 2 Support.

    Creating a New Struts 2 Web Application Project in IntelliJ IDEA 8 M1

    1. Run IntelliJ IDEA 8 M1 IDE and create a new project by going to File -> New Project... and following the wizard: Enter a name for the application(say "HelloWorld") when prompted and make sure you select Web Application and Struts 2 checkboxes on the technologies page.

      Select Web application support and Struts 2 framework support when creating a new project in IntelliJ IDEA 8.0 IDE.

    2. You can accept the default settings for everything else in the wizard. Click Finish.

    3. When you click Finish, IntelliJ IDEA will start downloading the required Struts 2 JAR library files to your project's lib directory, which may take a few minutes. IntelliJ IDEA may then parse all the files in the project to be able to help you later when you start editing/browsing the files.

      IntelliJ IDEA 8.0 IDE downloads the Struts 2 JAR libraries automatically when creating a new Struts 2 project.

      [The annoying thing is that IntelliJ downloads these Struts 2 library files for every Struts 2 application you create. Currently there is no option to reuse the previously downloaded library files. Hope they add it in the final version.]

      You should finally see the project structure created as shown below(expand the directories as needed).

      Struts 2 Web Application project structure in IntelliJ IDEA 8.0 IDE.

      As you can see, the required Struts 2 libraries are automatically downloaded and installed in the lib directory(and included in the build path) by the IDE.

    4. You can also see that the IntelliJ IDEA IDE has created and placed the struts.xml configuration file(containing basic template text) in the src directory. The generated web.xml file includes the following configuration elements needed by the Struts 2 framework so that you don't have to type or copy/paste it manually in every Struts 2 web application:
      XML:
      1. <filter>
      2.         <filter-name>struts2</filter-name>
      3.         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
      4.     </filter>
      5.     <filter-mapping>
      6.         <filter-name>struts2</filter-name>
      7.         <url-pattern>/*</url-pattern>
      8.     </filter-mapping>

      You can add additional configuration elements to the file.

    The basic structure needed for a Struts 2 web application project is now in place and we only ran the New Project wizard in Intellij IDEA so far! This is far cry from the experience with NetBeans or Eclipse IDEs even with the help of the plugins: see my previous posts “Hello, World” Web Application using Struts 2 in NetBeans IDE 6.1 and Struts 2 Plugin for NetBeans IDE - nbstruts2support for example. All we need to do now is to write our own application logic.

    Let us create an example action class in Java and a JSP result page to go with it.

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

    1. Right-click on the src folder, select New -> Class and enter hello.HelloWorld in the New Class dialog box.

      HelloWorld Struts 2 Java action class for Struts 2 web application in IntelliJ IDEA 8.0 IDE.

      Edit the file so that it contains the following code.

      JAVA:
      1. package hello;
      2.  
      3. public class HelloWorld {
      4.     private String userName;
      5.     public String getUserName() {
      6.         return userName;
      7.     }
      8.     public void setUserName(String userName) {
      9.         this.userName = userName;
      10.     }
      11.  
      12.     private String message;
      13.     public String getMessage() {
      14.         return message;
      15.     }
      16.  
      17.     public String execute() {
      18.         message = "Hello, " + userName + ".";
      19.         return "SUCCESS";
      20.     }
      21. }

    2. Let us access the message property set by HelloWorld action and display it in the index.jsp page. Edit index.jsp so that it looks like this:
      HTML:
      1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
      2. <%@ taglib prefix="s" uri="/struts-tags" %>
      3.  
      4.   <head><title>Simple jsp page</title></head>
      5.         Struts 2 Message: <s:property value="message" default="Guest." />
      6.  
      7.         <s:form method="GET" action="hello/HelloWorld.action">
      8.             Enter your name:<s:textfield name="userName" />
      9.             <s:submit value="Submit" />
      10.         </s:form>
      11.   </body>
      12. </html>

    3. We now need to configure the Java class(HelloWorld.java) as a Struts 2 Action and map it to the Struts 2 Result page(index.jsp) created in the above steps. Edit struts.xml file such that it has the following content:
      XML:
      1. <!DOCTYPE struts PUBLIC
      2.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
      3.     "http://struts.apache.org/dtds/struts-2.0.dtd">
      4.  
      5. <struts>
      6.     <package name="hw" extends="struts-default">
      7.         <action name="HelloWorld" class="hello.HelloWorld">
      8.             <result name="SUCCESS">/index.jsp</result>           
      9.         </action>
      10.     </package>
      11. </struts>

      (Ignore the error shown by IntelliJ IDEA in struts.xml file if it says it is not able to resolve struts-default package.)

      This maps the requests sent to the "/HelloWorld.action" url to HelloWorld.java Struts 2 action class, and after the successful execution of the request, it directs to the index.jsp result page.

    4. The HelloWorld web application project structure, when relevant folders are expanded, should now look like this:

      Hello World Struts 2 web application project structure in IntelliJ IDEA IDE.

      (Did you notice how struts.xml was automatically moved to our hello package? Nice, huh?)

    Create Run/Debug Configuration in IntelliJ IDEA and Run the Struts 2 Project

    1. Before we can run the application, we first need to create/edit the Run configuration for our project. Select the HelloWorld project in the left pane and go to Run -> Edit Configuration. If the left pane is empty, it means that we need to create a new configuration which is a one-time job; after creating it the first time, we can use the same configuration for all our future projects.

      Click the + button at the top of the window and select Tomcat -> Local (or other application server of your choice; I use Tomcat server as example in this post) from the pop-up menu.

      Configure Apache Tomcat application server in IntelliJ IDEA 8.0 IDE.

      In Run/Debug Configurations window, click the Configure button and in the popped-up Application Servers window click the [+ Add] button and select the directory where Apache Tomcat is installed on your system in Tomcat home and Tomcat base directory fields.

      Configure Run/Debug configuration for Struts 2 web application project in IntelliJ IDEA 8.0 IDE.

      You should now see a warning displayed near the bottom of the window and the option to fix it.

      Select Web project with Struts 2 Facet to deploy in IntelliJ IDEA 8.0 IDE.

      Click the Fix button on the far right corner and then click OK.

    2. We are now ready to run the application. Select the HelloWorld project in the left pane and go to Run -> Run.... You should see the run configuration window which allows you to make any further configuration changes before the application is run. (If you don't want to see the configuration window every time you run the application, uncheck the "Display settings before launching" checkbox.)

      Click the Run button to run the application. You can see the result in the following screenshots:

      Output of HelloWorld Struts 2 web project in IntelliJ IDEA 8.0 IDE.

      Output of HelloWorld Struts 2 web project in IntelliJ IDEA 8.0 IDE.

      Happy developing with Struts 2 in IntelliJ IDEA 8!

    Last Words

    This is a big moment for the Struts 2 users. It's a shame that after such a long period since the release of Struts 2 web framework, its support in all the popular IDEs is next to zero. The version of the NetBeans IDE that will be released next, version 6.5, talks nothing about the support for Struts 2 framework(you can see support for Struts 1.2.9 proudly displayed on NetBeans 6.5 Beta release page); all hopes are now pinned up on the nbstruts2support netbeans plugin I guess. I hope that IntelliJ IDEA's out-of-the-box Struts 2 support will encourage the teams/communities of other popular IDEs to follow the suit.

    Also see:

    1. more details on Struts 2 support in IntelliJ IDEA 8.0.
    2. the full list of features introduced in IntelliJ IDEA 8.0 Milestone 1 and what is planned for the final release.


    If you want to receive future posts by email, enter your email address here:

    Related Posts:

  • Struts 2 Plugin for NetBeans IDE - nbstruts2support
  • “Hello, World” Web Application using Struts 2 in NetBeans IDE 6.1
  • “Hello, World” Web Application in Ruby on Rails using Aptana Studio
  • Setting Up Rails Development Environment on Windows Vista/XP
  • Creating “Hello World” Web Application Using the Grails Framework
  • Simple Way to Update Ubuntu Edgy With Slow/No Internet Connection
  • Setting Up Development Environment For Grails on Windows Vista/XP

  • Readers who viewed this page, also viewed:


    August 4, 2008

    Struts 2 Plugin for NetBeans IDE - nbstruts2support

    Filed under: Java, Netbeans, Struts 2 — tabrez @ 10:09 pm

    As mentioned in my previous post about creating Struts 2 web applications in NetBeans IDE, there is a Struts 2 plugin available for the NetBeans IDE but it is still under development. You can try it out right away if you are interested; the name of the plugin is nbstruts2support.

    First read the summary of its current feature list(and what is planned for the future) and have a look at some screenshots. Then follow the installation instructions and try it on your system.

    Below I take you through the installation process of nbstruts2support Struts 2 plugin for NetBeans 6.1 IDE showing a few screenshots along the way.

    Installing nbstruts2support plugin in NetBeans IDE

    1. Go to the nbstruts2support download page and download the two nbm files and save them in a directory.
      Download nbstruts2support Struts 2 plugin for NetBeans IDE.
    2. Start the NetBeans IDE if not already running and go to Tools -> Plugins and select the Downloaded tab. Click the Add Plugins... button and select the two plugin files(.nbm files) downloaded in the earlier step.
      Install nbstruts2support nbm plugin files in NetBeans IDE.
      Install nbstruts2support nbm plugin files in NetBeans IDE.
    3. The Plugins dialog box should show you the details about the plugin.

      Install nbstruts2support nbm plugin files in NetBeans IDE.

      Go through the wizard clicking the button Next and accepting the license agreement and ignoring any errors about invalid signature.

      Install nbstruts2support nbm plugin files in NetBeans IDE.

      Restart the NetBeans IDE when prompted.
      Install nbstruts2support nbm plugin files in NetBeans IDE.

      The Struts 2 plugin is now installed in NetBeans.

    Using nbstruts2support in NetBeans IDE

    1. The plugin doesn't allow a Struts 2 project template to be created in NetBeans, which means that we need to manually create a basic Web Application and add Struts 2 libraries to it manually. We also need to create struts configuration files(struts.xml and struts.properties) and modify the web.xml file. If we do all this manually, then what does the plugin do?

      For one thing, the plugin helps in creating template files for Struts 2 Actions, Interceptors, Results etc.
      nbstruts2support Struts 2 plugin for NetBeans IDE allows easy creation of Struts 2 Action, Interceptor etc. template files.

      Secondly, it helps in auto-completion:
      nbstruts2support Struts 2 plugin for NetBeans IDE helps in auto-completion.
      More information on auto-completion.

      It is not yet completely bug free though:
      nbstruts2support Struts 2 plugin for NetBeans IDE helps in auto-completion but is not bug free.

      It also helps in easy navigation using hyperlinks. (See all the tabs at the top of the page: Analysis, Phase I, Phase II, Phase III.)
      nbstruts2support Struts 2 plugin for NetBeans IDE helps in hyperlinking.
      [image from nbstruts2support website]

    End Notes:
    The current state of Struts 2 support in the NetBeans IDE is hardly satisfying. Let us wait and see how much time it takes to complete at least the basic features in nbstruts2support. It is at least slightly better than creating and developing Struts 2 applications completely manually.

    The state is different if we look at other IDEs like Eclipse and IntelliJ IDEA. I will talk more about Struts 2 support for Eclipse IDE in my next post.


    If you want to receive future posts by email, enter your email address here:

    Related Posts:

  • “Hello, World” Web Application using Struts 2 in IntelliJ IDEA 8.0 M1
  • “Hello, World” Web Application using Struts 2 in NetBeans IDE 6.1
  • Setting Up Rails Development Environment using Aptana Studio
  • Wordpress Upgrading Experience With Automatic Upgrade Plugin
  • Wordpress Plugin and Theme Cheatsheets
  • Setting Up Rails Development Environment on Windows Vista/XP
  • Develop Ruby Applications Using Eclipse IDE

  • Readers who viewed this page, also viewed:


    July 30, 2008

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

    Filed under: Java, Netbeans, Struts 2 — tabrez @ 11:52 pm

    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:
      1. <?xml version="1.0" encoding="UTF-8"?>
      2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      3.     <filter>
      4.         <filter-name>Struts 2 filter</filter-name>
      5.         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
      6.     </filter>
      7.     <filter-mapping>
      8.         <filter-name>Struts 2 filter</filter-name>
      9.         <url-pattern>/*</url-pattern>
      10.     </filter-mapping>
      11.     <session-config>
      12.         <session-timeout>
      13.             30
      14.         </session-timeout>
      15.     </session-config>
      16. </web-app>

      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:
      1. package hello;
      2. import com.opensymphony.xwork2.ActionSupport;
      3.  
      4. public class HelloStruts2World extends ActionSupport {
      5.     private String userName;
      6.     public String getUserName() {
      7.         return userName;
      8.     }
      9.     public void setUserName(String userName) {
      10.         this.userName = userName;
      11.     }
      12.  
      13.     private String message;
      14.     public String getMessage() {
      15.         return message;
      16.     }
      17.  
      18.     @Override
      19.     public String execute() {
      20.         message = "Hello, " + userName + ".";
      21.         return SUCCESS;
      22.     }
      23. }

    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:
      1. <!DOCTYPE struts PUBLIC
      2.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
      3.     "http://struts.apache.org/dtds/struts-2.0.dtd">
      4.  
      5. <struts>
      6.     <package name="/" extends="struts-default">
      7.         <action name="HelloStruts2World" class="hello.HelloStruts2World">
      8.             <result name="success">/index.jsp</result>           
      9.         </action>
      10.     </package>
      11. </struts>

      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:
      1. <%@page contentType="text/html" pageEncoding="UTF-8"%>
      2. <%@taglib prefix="s" uri="/struts-tags" %>
      3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
      4.    "http://www.w3.org/TR/html4/loose.dtd">
      5.  
      6.     <head>
      7.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      8.         <title>Hello Struts 2 World Result Page</title>
      9.     </head>
      10.     <body>
      11.         <h2>Hello World!</h2>       
      12.         Struts 2 Message: <s:property value="message" default="Guest." />       
      13.        
      14.         <s:form method="GET" action="hello/HelloStruts2World.action">
      15.             Enter your name:<s:textfield name="userName" />
      16.             <s:submit value="Submit" />
      17.         </s:form>               
      18.     </body>
      19. </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.


    If you want to receive future posts by email, enter your email address here:

    Related Posts:

  • Struts 2 Plugin for NetBeans IDE - nbstruts2support
  • “Hello, World” Web Application using Struts 2 in IntelliJ IDEA 8.0 M1
  • “Hello, World” Web Application in Ruby on Rails using Aptana Studio
  • Setting Up Rails Development Environment on Windows Vista/XP
  • Creating “Hello World” Web Application Using the Grails Framework
  • Setting Up Development Environment For Grails on Windows Vista/XP
  • Setting Up Rails Development Environment using Aptana Studio

  • Readers who viewed this page, also viewed:


    Next Page »