Struts2 and Netbeans, configuring latest versions.

Configuring Netbeans to work with Struts2 framework

In this article I provide a workaround to let configure Netbeans Web Project, supporting Struts2 framework. Searching on the web, with Google and not only, I found an official plugin for Netbeans IDE, in developing status; but, sadly, last update was on 2008.

There is a blog , whose author provides an updated version of official plugin (it is modified for netbeans 6.7 and Struts 2.1.6) and this could be useful enough for my purpose…but maybe for curiosity or obduracy, I have created this workaround for installing and configuring a development environment, always updated to latest versions of both IDE and framework.

Very quick overview on Struts2 for those who already know Struts

Most revolutionary innovation provided by Struts2 is related, beyond a shadow of a doubt, with most important method in every Action: execute() method.
Unlike first version of Struts, in Struts2 this method is implemented without parameters to be passed;
and so anyone should ask: how to get the object which we need to work with?
With Struts, this is possible through Form beans, ActionForm. With Struts2, this becomes possible thanks to a specific pattern, known as inversion control or dependency injection.
I don’t pretend to dwell on a theoric explanation about this pattern, but for most curious of you, I warmly suggest to read  this interesting article, written by Martin Fowler.

Every way, this is the pattern which Spring framework is known for.

Struts2 complies to an advanced interpretation of MVC paradigm: pull-MVC. The purposes of this article are far away from an exhaustive text on differences between Struts and Struts2, you can find a lot of readings about this issue, with a quick Google search.
Anyway, some of this differences between two frameworks are fundamental, in my opinion.
MVC schema



Actions play a difference role, they pass from Controller to Model and View’s layers are now able to pull data directly from the actions, there is no more need of an extra object of model layer. The framework is based on 5 main components:
Model
    – Action;
Controller    
    – Interceptors;
    – DispatcherFilter;
View
    – Result type;
    – Result view (representation of results);
    – Valuestack\OGNL (modules that make possible integration between other components).
Who works with Struts knows very weel that one of the main configuration file used by framework is web.xml. References to several modules that builds the framework are defined inside that.
Let’s go with practical part.

Creation of a Web Project supporting Struts2, with Netbeans

The first step is to create e new empty WebProject:

File –> New –> Project 

From Categories choose Java Web and, on the left, Web Application. Then Next.

In the next view, give a name to the project, now select an Application Server and in the last page leave unchecked the Struts2 support . Then Finish.

At this point add Struts2 libraries to the project:
right click on Libraries and  Add Jar\Folder.
Now choose which libraries to include in the project, I personally suggest to begin including only necessary ones:
struts2-core.jar
xwork.jar
ognl.jar
freemarker.jar
commons-logging.jar
——————–
commons-io.jar
commons-fileupload.jar
imported libraries

It seems that adding all of those it is not a good idea, because they can generate conflicts. Last two libraries listed above became essential since version 2.1 of struts, I’ll investigate on that.

Creation of web.xml file.

Next step is writing web.xml file.

In particular, it is all handled through filters definition. It is necessary to add an entry for FilterDispatcher:That is the class that behaves like servlet filter, it initializes the framework and manages requets.
This file can be easily edited using the tab General, in this way:
Open it, select tab Filters –> Add Filters Element

In filter name field type: Struts2In filter class field, we should enter the FilterDispatcher class, who handles requests filtering. But the API of this class indicates:

Since Struts 2.1.3, use StrutsPrepareAndExecuteFilter instead or StrutsPrepareFilter and StrutsExecuteFilter if needing using the ActionContextCleanUp filter in addition to this one

Hence, since Struts 2.1.3, in filter class, inside web.xml, you should add:apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

The reason of this is still a mystery for me, until the official documentation on
versions subsequent to 2.1.3 will not be updated.

Inside Filter Mappingapply to URL: /*

Final xml will be something like this:


<web-app version="2.5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <filter>
 <filter-name>Struts2</filter-name>
 <filter-class>apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
 </filter>
 <filter-mapping>
 <filter-name>Struts2</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>
 <session-config>
 <session-timeout>
 30
 </session-timeout>
 </session-config>
 <welcome-file-list>
 <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>

Creation of struts.xml file.
Struts.xml file, in version 2 of Struts, replace struts-config.xml. Here it is the template, very basic, for every project.

Go in Source Package directory and create a brand new xml with a simple right click, then type:
</div>
<div style="margin-bottom: 0; margin-top: 0;">

<!DOCTYPE struts PUBLIC
 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 "http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>
 <package name="default" extends="struts-default">
 </package>
</struts>

I experienced several problems when deploy the application and I solved them creating a directory (manually) named classes inside the WEB-INF one (it can be done easily within Netbeans) and putting inside there the struts.xml file.

Now It is all ready to run you first application, It would be very interesting to delve into other configuring issues of Struts2, for example the properties files. But in this context, they can be left empty.

If you want to create your first Struts2 application, I refer you to documentation on the
official site
.

References:

An italian version of this article is available here:

Wellnet lab

Welcome, folks!

Welcome to my brand new blog!

You’ll probably say: why another blog on Java, developing, frameworks and others things like these?
My answer is: because I need it!
I love my job and I’ve been interested in Information Technology, Software Engineering, Unix-like Operating Systems and so on for a long time and now I work on these fascinating things, but..lately I really have short free time to upgrade my knowledge and it is frustrating!


Every time I say to myself: “I’ll study this new technology next weekend” or “Interesting! I’ll expand on that”…
And then..Nothing! I have never kept those promises, due to short time..but due to my laziness, too!


So.. with this blog I hope that I will be more motivated to study and I will not put off my good intentions.

I’m fighting my laziness. Please, help me to defeat it!