Struts2 - Step by step guide for the programmers

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

  1. Create a eclipse project named “HelloWorld”.
  2. From eclipse select File -> New -> Project and from the list select “Dynamic Web Project” under the “Web” Group.

  3. Give the project name as “HelloWorld”.
  4. Under the “target runtime” select Tomcat 5.5 instance. Click Finish to go back to the workspace.
  5. 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.

  1. Rght click on the project and select 'Properties'.
  2. Select 'Java Build Path ' option on the left pan of the properties window.
  3. 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'.
  4. 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.

  1. commons-logging
  2. freemarker
  3. ognl
  4. struts2-core
  5. 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.

<!-- add filter just after the displaname property or after other filters. -->
 <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
   <!-- Add the filter mapping after all filters -->
     <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

Create a files “struts2.xml” under 'HelloWorld/src' and add the following content.

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

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.

<action name="HelloWorld">
            <result>helloWorld.jsp</result>
        </action>

Create a file called helloWorld.jsp under 'WebContent' folder.
<%@ 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>helloWorld.jsp</title>
</head>
<body>
<h1><span style="color:green"> Hello World from struts2 ! </span></h1>
</body>
</html>

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

rajesh.v

91+9349791324

hi i am just add struts2 filter to web.xml
and create struts2.xml and helloWorld.jsp
add action tag to struts2.xml as u said
and did all as explained


helloWorld.jsp

but the index page is working try to access /HelloWorld/HelloWorld.action
getting error
************************
SEVERE: Could not find action or result
There is no Action mapped for namespace / and action name
HelloWorld. - [unknown location]
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:186)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:41)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:494)
****************************************

and till now not add action class just add filter to web.xml
from that gettingthis exception cant find

i did all the thing explained as above

hope fully

with regards

rajesh

similar problem

I had the same problem, but was able to get it working by changing 'struts2.xml' to 'struts.xml'

hope this helps,
zmelvin

working

This example is working fine but u need to rename struts2.xml file to struts.xml

Regards
Jhandu

Syndicate content