Search This Blog

Loading...

Monday, July 7, 2008

Struts 2 Hello World Example

I had seen many new deveopers struggling against struts2 hello world example. So I decided to write a small example.
Below are the required libraries to run this example which are easily availabel

struts2-core-2.0.11
xwork-2.0.4
commons-logging-1.0.4
commons-logging-api-1.1
freemarker-2.3.8
ognl-2.6.11

The structure of the applictaion which I am following is (Eclipse IDE)
Struts2Demo
|---src
| |----org
| | |----vinod
| | | |----action
| | | | |----HelloWorld।java
|---struts.xml

|---WebContent
| |---jsp
| |---HelloWorld।jsp
|---index.jsp
|---WEB-INF
| |---lib
| |---web.xml


It is true that different IDE's use different structure, but at last when war is build they follow same structure.
Lets start...

HelloWorld.java
package org.vinod.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport{

String greetings = null;

public String execute() throws Exception {
setGreetings("Hello World");
return SUCCESS;
}

/**
* @return the greetings
*/
public String getGreetings() {
return greetings;
}

/**
* @param greetings the greetings to set
*/
public void setGreetings(String greetings) {
this.greetings = greetings;
}

}
HelloWorld.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Struts 2 Example</title>
</head>
<body>
<s:property value="greetings"/>
</body>
</html>
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Struts 2 Example</title>
</head>
<body>
<s:action name="HelloWorldAction" executeResult="true"></s:action>
</body>
</html>
struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="hello" extends="struts-default">
<action name="HelloWorldAction"
class="org.vinod.action.HelloWorld">
<result>/jsp/HelloWorld.jsp</result>
</action>

</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Enjoy the struts2(most popular java framework today)

I had also written some more examples and some solutions of struts which can be easily found at
Struts 2 ajax drop down example
Struts 2 change <actionerror /> <actionmessage />
Struts 2 Session Check Interceptor
About Struts 2

39 comments:

Anonymous said...

Nice,It is very convenient for me to make a first hand example on struts2 with this example. I love your example and follow your blog in future for other references too.
Thanks

Anonymous said...

Thank you for this example. It is the only example I've found that actually works as described (and that includes the examples found at the Apache Struts 2 website).

Vinod Kumar Kashyap said...

Thanks for your compliments. In future you will found more examples

Anonymous said...

Hi
Thanks for your neat examples.But i have 2 issues

1. Once i startup the tomcat 6.0.16 from eclipse 3.3.0, i got the following error.

java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.FilterDispatcher

2. I found some error prone in jsp
Cannot find the tag library descriptor for /struts-tags in JSP. It is shown in Jsp file with error sign at the particular line no.

Kindly let me know idea

Vinod Kumar Kashyap said...

This error sometimes come due to reason that you haven't placed your struts.xml in right place. If it doesn't work then check that it is in classes folder or not. If not place it there and restart your server and then try again.
If you copy the whole example line by line then it should work well. If you have done some modifications then check with above example
Hope, this will solve your problem

Anonymous said...

Hi ,
im getting blank page if i deployed and executed the first sample,im new to struts2 ,i dont know how to run the sample

reena said...

Hi Vinod,
Thanks a lot.I will try this as I m struggling to run the example from roseindia ...
this looks easier.

pin2 said...

Hi,

It is a wonderful work!!! Keep it up.

Could you please write buil.xml file for ant so that one can build the project and deploy in the container?

Vinod Kumar Kashyap said...

I am sure to write an ant build file to deploy it

Sam said...

I am not able to test the basic application described above. Is the directory structure correct? How to go about using MyEclipse?

Vinod Kumar Kashyap said...

Hi Sam
If you are using MyEclipse no problem. Simply make a web application and put the required files in there respective folders and then deploy the application. However I am shortly giving an ant build for this example if above does not work.
This is very simple example and works well with most of the people

Anonymous said...

Hi. This example does not work for me. I get the following error :
SEVERE: Exception starting filter struts2
Unable to load bean: type: class:com.opensymphony.xwork2.ObjectFactory - bean - jar:file:/C:/Apache%20Tomcat%206.0.16%20on%2010.156.0.195/webapps/struts2-blank-2.0.11.2/WEB-INF/lib/struts2-core-2.0.11.2.jar!/struts-default.xml:30:72

Am I doing something wrong?
Please help. Thanks

Vinod Kumar Kashyap said...

Hi
You got this error because you haven't placed your struts.xml in correct path or you haven't included all required libraries
You can mail me the code and I will see why there is a problem with you as nobody gets any problem

Anonymous said...

class com.opensymphony.xwork2.ObjectFactory with the name xwork has already been loaded by bean - jar:file:/C:/dev/INET_1_0/WebContent/WEB-INF/lib/struts2-core-2.0.11.1.jar!/struts-default.xml:30:72 - bean - jar:file:/C:/java/apache-tomcat-6.0.18/wtpwebapps/INET_1_0/WEB-INF/lib/struts2-core-2.0.11.1.jar!/struts-default.xml:30:72

Anonymous said...

Hi Vinod,
Am getting the below error.Am using Eclipe 3.1.2,Tomcat 6.0.

WARNING: Settings: Could not parse struts.locale setting, substituting default VM locale
Sep 30, 2008 9:34:34 AM org.apache.catalina.core.StandardContext filterStart
SEVERE: Exception starting filter struts2
Action class [org.vinod.action.HelloWorld] not found - action - file:/C:/Program%20Files/Apache%20Software%20Foundation/tomcat-6.0.18/webapps/struts2demo/WEB-INF/classes/struts.xml:6:86
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyAction(XmlConfigurationProvider.java:374)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addAction(XmlConfigurationProvider.java:329)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:429)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:239)
at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:111)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reload(DefaultConfiguration.java:152)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:52)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:395)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:452)
at org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:201)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef

Vinod Kumar Kashyap said...

Hi All
If you have any problems post them at:
http://www.nabble.com/Struts-2-f30929.html

Solutions of the most of the problems were given there. If you don't find your solution then you can write your problem. Many struts 2 developers will be available there

Thanks

Veera said...

This is just what I want.:)

Anonymous said...

Hi,

a great workin example (this the first one which works). Would you mind to explain how it works in detail. Why do you use two files index.jsp and HelloWold.jsp. If a touch one of them the example does not work anymore.

Thanks
Eduard

Amit said...

add listener tag no problem occur regarding
SEVERE: Exception starting filter struts2

Amit said...

Add Listener Tag for the problem
SEVERE: Exception starting filter struts2

Nitin said...

Hi,
while executing this example. it is showing
HTTP Status 404 - /struts2Demo/

--------------------------------------------------------------------------------

type Status report

message /struts2Demo/

description The requested resource (/struts2Demo/) is not available.

but if I will comment filter tag in web.xml it is working upto index.jsp........what should I do
Thanx in Advance,
Nitin

Cheekuri said...

yeah ..its working fine for me.
thanks Vinod ,thnx a lot man.
Regards,
Ram

Anonymous said...

thx from Belarus :)

rajeev said...

This simple helped a lot, thanks Vinod.
Struts 2.1.6,and i faced one exception, not able to load the struts-default.xml. Then I copied the jat commons-fileupload-1.2.1.jar to the lib folder and starts my application starts fine.

Very much thanks Vinod, and expects more ideas from your blog.

Anonymous said...

hi Vinod,
Its very simple,straight forward and lucid...
Nice work man keep it up.
sincerely
kaasi

Anonymous said...

I encountered error when I try to ran this sample. I didn't work! Anyway, thanks for the example. For sake of helping others. I compiled my own hello world that is up and running. You guys may want to take a look over this web page.

http://www.howtodothings.com/computers-internet/how-to-create-struts-2-helloworld-on-eclipse

Anonymous said...

Hi, When i deploy my application in tomcat 6.0.18, am getting error as "Project folder" not found. but i have deployed correctly in webapps folder. Pls tel me wat to do?

chandru said...

how to build WAR file?

Anonymous said...

Hi... I too am having trouble finding a working example of a simple struts2 app. I have tried this and I am getting the following error:
org.apache.jasper.JasperException: The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag.

Anonymous said...

Earlier, I posted a question. I think it may have been about "The Struts dispatcher cannot be found." Anyway, if it was, then the solution was because Eclipse was building the war file with the web.xml file in the wrong location. After the application deployed on my Tomcat 5.5.27 server, I moved the file to the correct folder and the problem is resolved.

The big help to resolving this was the catalina.{date}.log file in Tomcat 5.5\logs directory. I had been focusing on another log, the localhost.{date}.log but that was a stack trace that gave no hint at the root cause.

Thanks for the demo.

AMol said...

I used eclipse and tomcat 5.5.25 at my development center and developed an application using struts 2, it works absolutely ok, but when i developed it on server supporting same tomcat it gave me error that
org.apache.jasper.JasperException: File "/struts-tags" not found

Please help me to resolve this problem.

It works on my local PC, but does not work when application is hosted.

Anonymous said...

Hi Vinod,

I Really appreciate your help.



siraj

ramesh said...

Hi Vinoth ,

Hi got an error while invoking the page in browser:


The The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag.

praveekan said...

hi Vinoth, i just referred you sample code for sample application in Struts 2. I wold like to know how to do the login application in struts 2. I have created the login.jsp with had a form action too. but when i cick submit button i getting the following error . what is reason i am getting this error and could you give an idea how to get input from the form and validate it ?
error :

there is no action mapped for namespace / and action doLogin. [unknown location].

my package structure is here
-src
--com.sample.action
---LoginAction.java
--Struts.xml
-web
--WEB-INF
---web.xml
---lib
---view
----success.jsp
----login.jsp
----error.jsp
--META-INF
-index.jsp

please help me in this issue.. Thanks in advance...

Vinod Kumar Kashyap said...

Hi Praveenkan,

It seems that there is some error in the mapping in the jsp file. I will be more sure about it if you send the code to me.

Thanks,

venkateswara said...

good, it is very helpfull to me

Anonymous said...

hi. i am getting file /struts-tags not found error in jsp. what do i do?

Anonymous said...

This is the only example I've found that is written correctly and works. Many thanks for a job well done!

Anonymous said...

I was finding the struts2 in action example difficult to work with.Yours helped me understand it better.Thanks Vinod

Post a Comment