Getting Started with Struts2 without XML Code in Eclipse
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:
- https://www.journaldev.com/2146/struts-2-hello-world-example-with-annotations-and-without-struts-xml-file
- https://stackoverflow.com/questions/31415300/how-to-add-struts2-to-a-web-application-without-web-xml
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:
|
|
This class is used to replace web.xml
.
Write Your Code
Write your action with annotations like this:
|
|
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.