“Hello, World” Web Application using Struts 2 in IntelliJ IDEA 8.0 M1
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
- Download and install the latest version of Sun JDK: Sun Java SE 6.
- Download and install Apache Tomcat web application server: Apache Tomcat 5.5.
- 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.

Creating a New Struts 2 Web Application Project in IntelliJ IDEA 8 M1
- 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.

- 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.

[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).

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.
- 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:
-
<filter>
-
<filter-name>struts2</filter-name>
-
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
-
</filter>
-
<filter-mapping>
-
<filter-name>struts2</filter-name>
-
<url-pattern>/*</url-pattern>
-
</filter-mapping>
You can add additional configuration elements to the file.
-
You can accept the default settings for everything else in the wizard. Click Finish.
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
- Right-click on the src folder, select New -> Class and enter hello.HelloWorld in the New Class dialog box.

Edit the file so that it contains the following code.
JAVA: - 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:
-
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
-
<%@ taglib prefix="s" uri="/struts-tags" %>
-
-
Struts 2 Message: <s:property value="message" default="Guest." />
-
-
<s:form method="GET" action="hello/HelloWorld.action">
-
Enter your name:<s:textfield name="userName" />
-
<s:submit value="Submit" />
-
</s:form>
-
</body>
-
</html>
-
- 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:
-
<!DOCTYPE struts PUBLIC
-
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
-
"http://struts.apache.org/dtds/struts-2.0.dtd">
-
-
<struts>
-
<package name="hw" extends="struts-default">
-
<action name="HelloWorld" class="hello.HelloWorld">
-
<result name="SUCCESS">/index.jsp</result>
-
</action>
-
</package>
-
</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.
-
- The HelloWorld web application project structure, when relevant folders are expanded, should now look like this:

(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
- 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.

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.
You should now see a warning displayed near the bottom of the window and the option to fix it.

Click the Fix button on the far right corner and then click OK.
- 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:


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:
- more details on Struts 2 support in IntelliJ IDEA 8.0.
- the full list of features introduced in IntelliJ IDEA 8.0 Milestone 1 and what is planned for the final release.
Related Posts:
Readers who viewed this page, also viewed:
- Creating “Hello World” Web Application Using the Grails Framework
- Six Popular IDEs For Developing Software in C/C++ on Windows Platform
- 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

Thanks for this nice introduction, I've linked your post on the plugin's documentation website (http://www.jetbrains.net/confluence/display/CONTEST/Struts2PluginFeaturesUserGuide)
QuoteComment by Yann Cébron — August 25, 2008 @ 12:18 pm
Yann, thanks for writing the best struts 2 plugin in the market; I find it difficult to search for any other struts 2 plugin that can come even close to yours. I am very glad that your plugin is being integrated with the core of IntelliJ IDEA 8.
Comment by tabrez — August 30, 2008 @ 1:52 pm
[...] use Struts 2 you can be happy to know that 8.0 release comes with a new Struts 2 plugin. There is a small tutorial on how to use it but it has some errors. First the Action class should extend ActionSupport hand [...]
QuotePingback by O Blog do Gustavo Felisberto » Struts 2 under IntelliJ IDEA 8.0M1 — October 3, 2008 @ 7:13 pm
First of all I am really impressed from stauts2, it is really superb framework and it really eliminated all struts 1 spaghetti mappings.
I have tow comments,
1. I could not work it out on my Vista notebook installation, but it does work on my Ubuntu machine.
some how the struts2 mapping is not working properly with last Intellij (855somthing).
2. the tutorial has naming mistake as the line
in index.jsp should be,
QuoteComment by moshe beeri — October 4, 2008 @ 9:21 pm