Contents
  1. 1. Example Project Download
  2. 2. Set up Normal JSP Project
  3. 3. Convert Project into Maven Project
  4. 4. Add Struts2 Dependencies
  5. 5. Add a Web Filter
  6. 6. Write Your Code

After JSP, I began to learn Struts2 in my class. Like before, I decide to write a tutorial about how to set up a basic struts2 project in Eclipse. I assume you already can set up a JSP project in Eclipse in the following steps.

Setting up Struts2 projects is a little bit harder than JSP one. I mainly learnt from these following 2 links:

Example Project Download

Here I modify the project in the first link a little to make this example project. You may download this first to see if it actually works.

Download Struts2-Start Project

Set up Normal JSP Project

Create a dynamic web project as my previous tutorial.

Convert Project into Maven Project

Right click the project and select “Configure - Convert to Maven Project”.

Add Struts2 Dependencies

Add two dependencies into your pom.xml: struts2-core and struts2-convention-plugin. Currently I am using version 2.5.16. If I guess correct, that convention plugin is used to replace struts2.xml.

Add a Web Filter

Add a web filter class:

1
2
3
4
5
6
7
8
9
10
package red.aoi.struts.filters;
import javax.servlet.annotation.WebFilter;
import org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter;
@WebFilter("/*")
public class StrutsFilter extends StrutsPrepareAndExecuteFilter {
}

This class is used to replace web.xml.

Write Your Code

Write your action with annotations like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package red.aoi.struts.actions;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Namespaces;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
/**
* An empty class for default Action implementation for:
*
* <action name="home">
* <result>/login.jsp</result>
* </action>
* HomeAction class will be automatically mapped for home.action
* Default page is login.jsp which will be served to client
* @author pankaj
*
*/
@Namespaces(value={@Namespace("/User"),@Namespace("/")})
@Result(location="/login.jsp")
@Actions(value={@Action("/"),@Action("/home")})
public class HomeAction extends ActionSupport {
}

Notice it seems that you have to put actions into a package named actions. Otherwise, the struts2 framework will not recognize it, even if you have already added annotations. I think this is that “convention” thing. If have time, search for it.

Contents
  1. 1. Example Project Download
  2. 2. Set up Normal JSP Project
  3. 3. Convert Project into Maven Project
  4. 4. Add Struts2 Dependencies
  5. 5. Add a Web Filter
  6. 6. Write Your Code