This tutorial explains struts 2 with the bare minimum. You won't get lost in the unwanted files under the struts example applications.
You should be comfortable working with eclipse and WTP. I have used eclipse 3.2.2 myself. Any 3.1 or latter should work, although not tested. You will also need web Tools plugins from the curresponding Calisto release.
From eclipse select File -> New -> Project and from the list select “Dynamic Web Project” under the “Web” Group.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World !</title>
</head>
<body>
<h1> Struts 2 Hello World </h1>
</body>
</html>
Now if you select run “index.jsp” using “run on the server” option, you can see the JSP page we just created. If you see that, congratulations, we are ready to go to the struts2 tutorial.
At the minimum struts2 requires 4 jar files, and the configuration file “struts.xml”. We also need to make some modification to the project's build settings, so that eclipse will put the class files under “WebContent/classes”. If you know how to do that go ahead and do it. If you don't know, here is how it is done.
Now we need to add the struts 2 jar files to the Web app libray. Copy the following files from struts distribution to the “WebContent/WEB_INF/lib” folder.
I didn't mention the version number because it may change. But make sure you copy everything from the struts example application that comes with the struts distribution.
Now we need to add the struts 2 filter and filter mapping to the web.xml. Add the following line to your web.xml.
Create a files “struts2.xml” under 'HelloWorld/src' and add the following content.
This is the minimum required configuration for struts. Now if you restart tomcat, you should be able to see the same index.jsp page. Now add the following snippets between the package tags.
After restarting tomcat, try accessing '/HelloWorld/HelloWorld.action' , you can see the 'Hello world from struts2!” message on the screen. So your first action worlked, even though you didn't write any !
When a request comes to the container, it checks web.xml to decide how it should be dispatched. Here is the web.xml for our “Greeting” Application
On line numbers 3-6 , we are enabling struts2 filter. We map this filter to all urls on line numbers 7 through 10.ie all requests are passed through struts2 FilterDispatcher. The dispatcher looks in to the struts2.xml file for any matching mappings. Here is our struts2.xml.
line # 6-8 creates the HelloWorld Action (HelloWorld.action), that we used in the previous example. But we didn't give any class attribute to the action tag. if no class is defined for an action, struts assumes the action returns “SUCCESS”. We 'll discuss about the return values later. The Greeting action (Greet.action) is configured on line # 9 through 11. In this case we are giving a class attribute, meaning , we are going to create our own class for this action.
Now Let's try creating a struts2 Action class. Usually we sub class ActionSupport for creating our own Action class. Here is the code.
When struts finds “class” attribute for an action mapping, it create an instance of the class, and invokes it's execute method. In our action class we have one property apart from the execute method. In the execute method we are setting this property, to the greeting message. We can access this property from the jsp file using the tag library suplied by the struts2 package.
Note the value that we are returning from the execute method. it's the string "greet" !!!. On line number 10 of the struts2.xml we are configuring a result with the same name. that's what is telling struts to pick up the file "/greet.jsp" and merge it to the response.
Pretty Simple , Eh ?
Here is our "/greet.jsp".
action class