This tutorial explains struts 2 with the bare minimum. You won't get lost in the unwanted files under the struts example applications.
Pre-requisites for this struts2 tutorial
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.
Developing the the struts2 “helloworld ” with out any classes
Setting up the stage
- Create a eclipse project named “HelloWorld”.
From eclipse select File -> New -> Project and from the list select “Dynamic Web Project” under the “Web” Group.
- Give the project name as “HelloWorld”.
- Under the “target runtime” select Tomcat 5.5 instance. Click Finish to go back to the workspace.
- Now create a file called “index.jsp” inside “WebContent” folder with the following content
<%@ 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.
Integrating Struts2
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.
- Rght click on the project and select 'Properties'.
- Select 'Java Build Path ' option on the left pan of the properties window.
- Under the 'Source' tab you can see 'HollowWorld/src' already listed as the source folder. Bottum of the same sheet displays the current value of the out put folder. Change that to 'HelloWorld/WebContent/classes'.
- Click OK.
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.
- commons-logging
- freemarker
- ognl
- struts2-core
- xwork
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.
struts2
org.apache.struts2.dispatcher.FilterDispatcher
struts2
/*
Create a files “struts2.xml” under 'HelloWorld/src' and add the following content.
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
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.
helloWorld.jsp
Create a file called helloWorld.jsp under 'WebContent' folder.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
helloWorld.jsp
Hello World from struts2 !
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 !
Comments
struts 2
similar problem
working