You are on page 1of 11

Struts 2 Tutorial: Create Struts 2 Application in Eclipse | Javalobby Page 1 of 11

Login
Join DZone

The heart of the Java developer community

Home Zones Library Refcardz Forums Links ITQuestions Snippets

Home Search

14 Struts 2 Tutorial: Create Struts 2 Application


2 in Eclipse
Submitted by Viral Patel on Fri, 2010/01/15 - 4:45am
Tags: struts2 struts2 application Frameworks

Welcome to the Part 2 of 7-part series where we will explore the We Recommend These Resources
world of Struts 2 Framework. In previous article we went through
Java EE & Servlet Containers for ColdFusion
the basics of Struts2, its Architecture diagram, the request
Developers
processing lifecycle and a brief comparison of Struts1 and Struts2.
Free Trial: InstallAnywhere 2009 for Java-Based
If you have not gone through the previous article, I highly
Setups
recommend you to do that before starting hands-on today.
Code Signing for Adobe AIR Applications
Struts 2 Tutorial List VeriSign Authenticated Content Signing
• Part 1: Introduction to Struts 2 Framework How to Digitally Sign Downloadable Code for
• Part 2: Create Hello World Application in Struts 2 Secure Content Transfer
• Part 3: Struts 2 Validation Framework Tutorial with Example
• Part 4: Struts 2 Tiles Plugin Tutorial with Example
About the author
• Part 5: Struts 2 Interceptors Tutorial with Example
First Name: Viral
• Part 6: Struts 2 File Upload and Save Example
Last Name: Patel
• Part 7: Struts 2 Ajax Tutorial with Example Posts: 11
Related: Create Struts Application with Eclipse Joined: 12/03/2008
Things We Need View full user profile

Before we starts with our first Hello World Struts 2 Example, we will need few tools.

1. JDK 1.5 above (download)


Spotlight Features
2. Tomcat 5.x above or any other container (Glassfish, JBoss, Websphere, Weblogic etc) (download) Lean Transformations
3. Eclipse 3.2.x above (download)
Mary Poppendieck reviews the tenets
4. Apache Struts2 JAR files:(download). Following are the list of JAR files required for this application. of Lean and provides important
◦ commons-logging-1.0.4.jar considerations for improving software
development and product quality.
◦ freemarker-2.3.8.jar
◦ ognl-2.6.11.jar Getting Groovy with Flex
◦ struts2-core-2.0.12.jar Rapidly prototype data-enabled Flex
applications using Groovy and Grails
◦ xwork-2.0.6.jar in combination with the Flex plugin for
Grails.
Note that depending on the current version of Struts2, the version number of above jar files may change.
7 Wastes of Software
Our Goal Development
Matt Stine describes how to eliminate
Our goal is to create a basic Struts2 application with a Login page. User will enter login credential and if authenticated some of the common sources of waste
from your software development
successfully she will be redirected to a Welcome page which will display message ” Howdy, <username>…!“. If user is not efforts.
authenticated, she will be redirected back to the login page.

Popular at DZone
NetBeans 7.0

InfoQ: Revving Up Your Hibernate Engine

Git tips that I learned from the Master

Jevgeni Kabanov from JRebel "Bares All"

Object Orientation is not a Goal

Groovy for Business Software. Why not?

Speed up your build with Maven 3

See more popular at DZone

Subscribe to the RSS feed

http://java.dzone.com/articles/struts2-tutorial-part-27 10/9/2010
Struts 2 Tutorial: Create Struts 2 Application in Eclipse | Javalobby Page 2 of 11

Getting Started Advertising - Terms of Service - Privacy - © 1997-2010, DZone,


Inc.
Let us start with our first Struts2 based application.
Open Eclipse and goto File -> New -> Project and select Dynamic Web Project in the New Project wizard screen.

After selecting Dynamic Web Project, press Next.

Write the name of the project. For example StrutsHelloWorld. Once this is done, select the target runtime environment
(e.g. Apache Tomcat v6.0). This is to run the project inside Eclipse environment. After this press Finish.

http://java.dzone.com/articles/struts2-tutorial-part-27 10/9/2010
Struts 2 Tutorial: Create Struts 2 Application in Eclipse | Javalobby Page 3 of 11

Once the project is created, you can see its structure in Project Explorer.

Now copy all the required JAR files in WebContent -> WEB-INF -> lib folder. Create this folder if it does not exists.

Mapping Struts2 in WEB.xml


As discussed in the previous article (Introduction to Struts2), the entry point of Struts2 application will be the Filter define in
deployment descriptor (web.xml). Hence we will define an entry of org.apache.struts2.dispatcher.FilterDispatcher class
in web.xml.

Open web.xml file which is under WEB-INF folder and copy paste following code.

01. <?xml version="1.0" encoding="UTF-8"?>


02. <web-app id="WebApp_9" version="2.4"
03. xmlns="http://java.sun.com/xml/ns/j2ee"
04. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
05. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-
app_2_4.xsd">
06. <display-name>Struts2 Application</display-name>
07. <filter>
08. <filter-name>struts2</filter-name>
09. <filter-class>
10. org.apache.struts2.dispatcher.FilterDispatcher
11. </filter-class>
12. </filter>
13. <filter-mapping>
14. <filter-name>struts2</filter-name>
15. <url-pattern>/*</url-pattern>
16. </filter-mapping>
17. <welcome-file-list>
18. <welcome-file>Login.jsp</welcome-file>
19. </welcome-file-list>
20. </web-app>

The above code in web.xml will map Struts2 filter with url /*. The default url mapping for struts2 application will be /*.action.
Also note that we have define Login.jsp as welcome file.

The Action Class


We will need an Action class that will authenticate our user and holds the value for username and password. For this we will
create a package net.viralpatel.struts2 in the source folder. This package will contain the action file.

Create a class called LoginAction in net.viralpatel.struts2 package with following content.

01. package net.viralpatel.struts2;


02. public class LoginAction {

http://java.dzone.com/articles/struts2-tutorial-part-27 10/9/2010
Struts 2 Tutorial: Create Struts 2 Application in Eclipse | Javalobby Page 4 of 11

03. private String username;


04. private String password;
05. public String execute() {
06. if (this.username.equals("admin")
07. && this.password.equals("admin123")) {
08. return "success";
09. } else {
10. return "error";
11. }
12. }
13. public String getUsername() {
14. return username;
15. }
16. public void setUsername(String username) {
17. this.username = username;
18. }
19. public String getPassword() {
20. return password;
21. }
22. public void setPassword(String password) {
23. this.password = password;
24. }
25. }

Note that, above action class contains two fields, username and password which will hold the values from form and also
contains an execute() method that will authenticate the user. In this simple example, we are checking if username is admin
and password is admin123.

Also note that unlike Action class in Struts1, Struts2 action class is a simple POJO class with required attributes and
method.

The execute() method returns a String value which will determine the result page. Also, in Struts2 the name of the method is
not fixed. In this example we have define method execute(). You may want to define a method authenticate() instead.

The ResourceBundle
ResourceBundle is very useful Java entity that helps in putting the static content away from the source file. Most of the
application define a resource bundle file such as ApplicationResources.properties file which contains static messages such
as Username or Password and include this with the application.

ResourceBundle comes handy when we want to add Internationalization (I18N) support to an application.

We will define an ApplicationResources.properties file for our application. This property file should be present in WEB-
INF/classes folders when the source is compiled. Thus we will create a source folder called resources and put the
ApplicationResources.properties file in it.

To create a source folder, right click on your project in Project Explorer and select New -> Source Folder.

Specify folder name resources and press Finish.

Create a file ApplicationResources.properties under resources folder.

Copy following content in ApplicationResources.properties.

1. label.username= Username
2. label.password= Password
3. label.login= Login

The JSP
We will create two JSP files to render the output to user. Login.jsp will be the starting point of our application which will
contain a simple login form with username and password. On successful authentication, user will be redirected to
Welcome.jsp which will display a simple welcome message.

Create two JSP files Login.jsp and Welcome.jsp in WebContent folder of your project. Copy following content into it.

http://java.dzone.com/articles/struts2-tutorial-part-27 10/9/2010
Struts 2 Tutorial: Create Struts 2 Application in Eclipse | Javalobby Page 5 of 11

Login.jsp
01. <%@ page contentType="text/html; charset=UTF-8"%>
02. <%@ taglib prefix="s" uri="/struts-tags"%>
03. <html>
04. <head>
05. <title>Struts 2 - Login Application | ViralPatel.net</title>
06. </head>
07. <body>
08. <h2>Struts 2 - Login Application</h2>
09. <s:actionerror />
10. <s:form action="login.action" method="post">
11. <s:textfield name="username" key="label.username" size="20" />
12. <s:password name="password" key="label.password" size="20" />
13. <s:submit method="execute" key="label.login" align="center" />
14. </s:form>
15. </body>
16. </html>

Welcome.jsp
01. <%@ page contentType="text/html; charset=UTF-8"%>
02. <%@ taglib prefix="s" uri="/struts-tags"%>
03. <html>
04. <head>
05. <title>Welcome</title>
06. </head>
07. <body>
08. <h2>Howdy, <s:property value="username" />...!</h2>
09. </body>
10. </html>

Note that we have used struts2 <s:> tag to render the textboxes and labels. Struts2 comes with a powerful built-in tag library
to render UI elements more efficiently.

The struts.xml file


Struts2 reads the configuration and class definition from an xml file called struts.xml. This file is loaded from the classpath
of the project. We will define struts.xml file in the resources folder. Create file struts.xml in resources folder.

Copy following content into struts.xml.

01. <?xml version="1.0" encoding="UTF-8" ?>


02. <!DOCTYPE struts PUBLIC
03. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
04. "http://struts.apache.org/dtds/struts-2.0.dtd">
05. <struts>
06. <constant name="struts.enable.DynamicMethodInvocation"
07. value="false" />
08. <constant name="struts.devMode" value="false" />
09. <constant name="struts.custom.i18n.resources"
10. value="ApplicationResources" />
11. <package name="default" extends="struts-default" namespace="/">
12. <action name="login"
13. class="net.viralpatel.struts2.LoginAction">
14. <result name="success">Welcome.jsp</result>
15. <result name="error">Login.jsp</result>
16. </action>
17. </package>
18. </struts>

Note that in above configuration file, we have defined Login action of our application. Two result paths are mapped with
LoginAction depending on the outcome of execute() method. If execute() method returns success, user will be redirected to
Welcome.jsp else to Login.jsp.

Also note that a constant is specified with name struts.custom.i18n.resources. This constant specify the resource bundle
file that we created in above steps. We just have to specify name of resource bundle file without extension
(ApplicationResources without .properties).

Our LoginAction contains the method execute() which is the default method getting called by Sturts2. If the name of method
is different, e.g. authenticate(); then we should specify the method name in <action> tag.

1. <action name="login" method="authenticate"


2. class="net.viralpatel.struts2.LoginAction">

Almost Done
We are almost done with the application. You may want to run the application now and see the result yourself. I assume you
have already configured Tomcat in eclipse. All you need to do:
Open Server view from Windows -> Show View -> Server. Right click in this view and select New -> Server and add your
server details.

http://java.dzone.com/articles/struts2-tutorial-part-27 10/9/2010
Struts 2 Tutorial: Create Struts 2 Application in Eclipse | Javalobby Page 6 of 11

To run the project, right click on Project name from Project Explorer and select Run as -> Run on Server (Shortcut:
Alt+Shift+X, R)

But there is one small problem. Our application runs perfectly fine at this point. But when user enters wrong credential, she
is redirected to Login page. But no error message is displayed. User does not know what just happened. A good application
always show proper error messages to user. So we must display an error message Invalid Username/Password. Please try
again when user authentication is failed.

Final Touch
To add this functionality first we will add the error message in our ResourceBundle file.
Open ApplicationResources.properties and add an entry for error.login in it. The final ApplicationResources.properties will
look like:

1. label.username= Username
2. label.password= Password
3. label.login= Login
4. error.login= Invalid Username/Password. Please try again.

Also we need to add logic in LoginAction to add error message if user is not authenticated. But there is one problem. Our
error message is specified in ApplicationResources.properties file. We must specify key error.login in LoginAction and the
message should be displayed on JSP page.

For this we must implement com.opensymphony.xwork2.TextProvider interface which provides method getText(). This
method returns String value from resource bundle file. We just have to pass the key value as argument to getText() method.
The TextProvider interface defines several method that we must implement in order to get hold on getText() method. But we
don’t want to spoil our code by adding all those methods which we do not intend to use. There is a good way of dealing with
this problem.

Struts2 comes with a very useful class com.opensymphony.xwork2.ActionSupport. We just have to extend our LoginAction
class with this class and directly use methods such as getText(), addActionErrors() etc. Thus we will extend the LoginAction
class with ActionSupport class and add the logic for error reporting into it. The final code in LoginAction must look like:

01. package net.viralpatel.struts2;


02. import com.opensymphony.xwork2.ActionSupport;
03. public class LoginAction extends ActionSupport {
04. private String username;
05. private String password;
06. public String execute() {
07. if (this.username.equals("admin")
08. && this.password.equals("admin123")) {
09. return "success";
10. } else {
11. addActionError(getText("error.login"));
12. return "error";
13. }
14. }
15. public String getUsername() {
16. return username;
17. }
18. public void setUsername(String username) {
19. this.username = username;
20. }
21. public String getPassword() {
22. return password;
23. }
24. public void setPassword(String password) {
25. this.password = password;
26. }
27. }

And that’s it. Our first Hello World Struts2 Application is now ready.

That’s All Folks


Execute the application in Eclipse and run it in your favorite browser.
Login page

http://java.dzone.com/articles/struts2-tutorial-part-27 10/9/2010
Struts 2 Tutorial: Create Struts 2 Application in Eclipse | Javalobby Page 7 of 11

Welcome page

Login page with error

Download Source Code


Click here to download Source Code without JAR files (9KB).

Moving On
Now that we have created our first webapp using Struts2 framework, we know how the request flows in Struts2. We also
know the use of struts.xml and properties file. In this application we implemented a preliminary form of validation. In next
part we will learn more about Validation Framework in Struts2 and implement it in our example.

From: http://viralpatel.net/blogs/

Your rating:

Login or register to post comments 23360 reads Printer-friendly version

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Dave Newton replied on Tue, 2010/01/19 - 10:15am

I'd really recommend using a more recent version of Struts 2 for new tutorials.

Login or register to post comments

Heinrich Winter replied on Tue, 2010/06/29 - 2:41am

Hello, I tried to run Struts HelloWorld, but Igot the following Exception: Unable to load configuration. -
bean -
jar:file:/C:/ecl_hiber_ws/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/StrutsHelloWorld/WEB
-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:47:178 at
com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration
(ConfigurationManager.java:58) at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration
(Dispatcher.java:374) at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:418) at
org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:190) at
org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275) at
org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:397) at
org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:108) at
org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3800) at
org.apache.catalina.core.StandardContext.start(StandardContext.java:4450) at
org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045) at

http://java.dzone.com/articles/struts2-tutorial-part-27 10/9/2010
Struts 2 Tutorial: Create Struts 2 Application in Eclipse | Javalobby Page 8 of 11

org.apache.catalina.core.StandardHost.start(StandardHost.java:722) at
org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045) at
org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443) at
org.apache.catalina.core.StandardService.start(StandardService.java:516) at
org.apache.catalina.core.StandardServer.start(StandardServer.java:710) at
org.apache.catalina.startup.Catalina.start(Catalina.java:583) at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at
java.lang.reflect.Method.invoke(Unknown Source) at org.apache.catalina.startup.Bootstrap.start
(Bootstrap.java:288) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413) Caused by:
Unable to load bean: type:org.apache.struts2.dispatcher.multipart.MultiPartRequest
class:org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest - bean -
jar:file:/C:/ecl_hiber_ws/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/StrutsHelloWorld/WEB
-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:47:178 at
com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register
(XmlConfigurationProvider.java:221) at
org.apache.struts2.config.StrutsXmlConfigurationProvider.register
(StrutsXmlConfigurationProvider.java:101) at
com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer
(DefaultConfiguration.java:169) at
com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration
(ConfigurationManager.java:55) ... 21 more Caused by: java.lang.NoClassDefFoundError:
org/apache/commons/fileupload/FileUploadException at java.lang.Class.getDeclaredConstructors0
(Native Method) at java.lang.Class.privateGetDeclaredConstructors(Unknown Source) at
java.lang.Class.getDeclaredConstructors(Unknown Source) at
com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register
(XmlConfigurationProvider.java:211) ... 24 more Caused by: java.lang.ClassNotFoundException:
org.apache.commons.fileupload.FileUploadException at
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1387) at
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1233) ... 28
more

Login or register to post comments

Pourya Shahroudi replied on Thu, 2010/07/15 - 6:41pm

Heinrich Winter if you have created struts.properties you need to add the resource there instead of
the struts.xml file
struts.custom.i18n.resources=ApplicationResources,languages_actions,languages_views

This should probably solve your problem...

Login or register to post comments

Sarav Sri replied on Thu, 2010/07/29 - 9:59pm

removed this.

Login or register to post comments

Sarav Sri replied on Tue, 2010/07/27 - 11:14pm

Hi, I'm really new to struts and eclipse. I tried to import the source code provided here and I got the
following error. I apologize if this is a simple question or already covered in other forums. I really
appreciate your guidance.

Jul 27, 2010 11:54:59 PM org.apache.tomcat.util.digester.SetPropertiesRule begin WARNING:


[SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to
'org.eclipse.jst.jee.server:StrutsHelloWorld' did not find a matching property. Jul 27, 2010 11:54:59 PM
org.apache.coyote.http11.Http11Protocol init INFO: Initializing Coyote HTTP/1.1 on http-8080 Jul 27,
2010 11:54:59 PM org.apache.catalina.startup.Catalina load INFO: Initialization processed in 689 ms
Jul 27, 2010 11:54:59 PM org.apache.catalina.core.StandardService start INFO: Starting service
Catalina Jul 27, 2010 11:54:59 PM org.apache.catalina.core.StandardEngine start INFO: Starting
Servlet Engine: Apache Tomcat/6.0.26 Jul 27, 2010 11:55:00 PM
com.opensymphony.xwork2.util.logging.commons.CommonsLogger info INFO: Parsing configuration
file [struts-default.xml] Jul 27, 2010 11:55:00 PM
com.opensymphony.xwork2.util.logging.commons.CommonsLogger error SEVERE: Dispatcher
initialization failed Unable to load configuration. - bean -
jar:file:/C:/JavaDevelopment/Workspace/StrutsHelloWorld/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/StrutsHelloWorld/WEB
-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:47:178 at
com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration
(ConfigurationManager.java:58) at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration
(Dispatcher.java:374) at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:418) at
org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:190) at
org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:295) at

http://java.dzone.com/articles/struts2-tutorial-part-27 10/9/2010
Struts 2 Tutorial: Create Struts 2 Application in Eclipse | Javalobby Page 9 of 11

org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:422) at
org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:115) at
org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3838) at
org.apache.catalina.core.StandardContext.start(StandardContext.java:4488) at
org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045) at
org.apache.catalina.core.StandardHost.start(StandardHost.java:785) at
org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045) at
org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443) at
org.apache.catalina.core.StandardService.start(StandardService.java:519) at
org.apache.catalina.core.StandardServer.start(StandardServer.java:710) at
org.apache.catalina.startup.Catalina.start(Catalina.java:581) at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at
java.lang.reflect.Method.invoke(Unknown Source) at org.apache.catalina.startup.Bootstrap.start
(Bootstrap.java:289) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414) Caused by:
Unable to load bean: type:org.apache.struts2.dispatcher.multipart.MultiPartRequest
class:org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest - bean -
jar:file:/C:/JavaDevelopment/Workspace/StrutsHelloWorld/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/StrutsHelloWorld/WEB
-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:47:178 at
com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register
(XmlConfigurationProvider.java:221) at
org.apache.struts2.config.StrutsXmlConfigurationProvider.register
(StrutsXmlConfigurationProvider.java:101) at
com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer
(DefaultConfiguration.java:169) at
com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration
(ConfigurationManager.java:55) ... 21 more Caused by: java.lang.NoClassDefFoundError:
org/apache/commons/fileupload/FileUploadException at java.lang.Class.getDeclaredConstructors0
(Native Method) at java.lang.Class.privateGetDeclaredConstructors(Unknown Source) at
java.lang.Class.getDeclaredConstructors(Unknown Source) at
com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register
(XmlConfigurationProvider.java:211) ... 24 more Caused by: java.lang.ClassNotFoundException:
org.apache.commons.fileupload.FileUploadException at
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1516) at
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1361) ... 28
more Jul 27, 2010 11:55:00 PM org.apache.catalina.core.StandardContext filterStart SEVERE:
Exception starting filter struts2 Unable to load configuration. - bean -
jar:file:/C:/JavaDevelopment/Workspace/StrutsHelloWorld/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/StrutsHelloWorld/WEB
-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:47:178 at
org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:431) at
org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:190) at
org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:295) at
org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:422) at
org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:115) at
org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3838) at
org.apache.catalina.core.StandardContext.start(StandardContext.java:4488) at
org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045) at
org.apache.catalina.core.StandardHost.start(StandardHost.java:785) at
org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045) at
org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443) at
org.apache.catalina.core.StandardService.start(StandardService.java:519) at
org.apache.catalina.core.StandardServer.start(StandardServer.java:710) at
org.apache.catalina.startup.Catalina.start(Catalina.java:581) at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at
java.lang.reflect.Method.invoke(Unknown Source) at org.apache.catalina.startup.Bootstrap.start
(Bootstrap.java:289) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414) Caused by:
Unable to load configuration. - bean -
jar:file:/C:/JavaDevelopment/Workspace/StrutsHelloWorld/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/StrutsHelloWorld/WEB
-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:47:178 at
com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration
(ConfigurationManager.java:58) at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration
(Dispatcher.java:374) at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:418) ... 19 more
Caused by: Unable to load bean: type:org.apache.struts2.dispatcher.multipart.MultiPartRequest
class:org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest - bean -
jar:file:/C:/JavaDevelopment/Workspace/StrutsHelloWorld/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/StrutsHelloWorld/WEB
-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:47:178 at
com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register
(XmlConfigurationProvider.java:221) at
org.apache.struts2.config.StrutsXmlConfigurationProvider.register
(StrutsXmlConfigurationProvider.java:101) at
com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer
(DefaultConfiguration.java:169) at

http://java.dzone.com/articles/struts2-tutorial-part-27 10/9/2010
Struts 2 Tutorial: Create Struts 2 Application in Eclipse | Javalobby Page 10 of 11

com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration
(ConfigurationManager.java:55) ... 21 more Caused by: java.lang.NoClassDefFoundError:
org/apache/commons/fileupload/FileUploadException at java.lang.Class.getDeclaredConstructors0
(Native Method) at java.lang.Class.privateGetDeclaredConstructors(Unknown Source) at
java.lang.Class.getDeclaredConstructors(Unknown Source) at
com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register
(XmlConfigurationProvider.java:211) ... 24 more Caused by: java.lang.ClassNotFoundException:
org.apache.commons.fileupload.FileUploadException at
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1516) at
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1361) ... 28
more Jul 27, 2010 11:55:00 PM org.apache.catalina.core.StandardContext start SEVERE: Error
filterStart Jul 27, 2010 11:55:00 PM org.apache.catalina.core.StandardContext start SEVERE: Context
[/StrutsHelloWorld] startup failed due to previous errors

Login or register to post comments

Sarav Sri replied on Wed, 2010/07/28 - 12:06am

Sorry for multiple posts. I tried to reimport the project again and I'm getting differ error. I tried to keep
struts.xml in resources as well in root (StrutsHelloWorld dir itself) and I'm getting the same error as
beloe. Please help.

Jul 28, 2010 12:53:35 AM org.apache.catalina.core.AprLifecycleListener init INFO: The APR based
Apache Tomcat Native library which allows optimal performance in production environments was not
found on the java.library.path: C:\Sun\Java\jre6
\bin;.;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Sun/Java/jre6/bin/client;C:/Sun/Java/jre6/bin;C:/Sun/Java/jre6/lib/i386;C:\oracle\product\10.2.0
\db_1\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Program
Files\CyberLink\Power2Go\;C:\Program Files\Common Files\Roxio Shared\DLLShared\;C:\Program
Files\Common Files\Roxio Shared\9.0\DLLShared\;C:\Program
Files\QuickTime\QTSystem\;C:\Program Files\MySQL\MySQL Server 5.1
\bin;C:\Sun\AppServer\bin;C:\Sun\Java\jdk1.6.0_20\bin
Jul 28, 2010 12:53:35 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source'
to 'org.eclipse.jst.jee.server:StrutsHelloWorld' did not find a matching property.
Jul 28, 2010 12:53:35 AM org.apache.coyote.http11.Http11Protocol init INFO: Initializing Coyote
HTTP/1.1 on http-8080 Jul 28, 2010 12:53:35 AM org.apache.catalina.startup.Catalina load INFO:
Initialization processed in 613 ms Jul 28, 2010 12:53:35 AM
org.apache.catalina.core.StandardService start INFO: Starting service Catalina Jul 28, 2010
12:53:35 AM org.apache.catalina.core.StandardEngine start INFO: Starting Servlet Engine:
Apache Tomcat/6.0.26
Jul 28, 2010 12:53:36 AM org.apache.catalina.core.StandardContext filterStart
SEVERE: Exception starting filter struts2
java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.FilterDispatcher
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1516)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1361)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:269)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef
(ApplicationFilterConfig.java:422)
at org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:115)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3838)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4488)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:519)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:581)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Jul 28, 2010 12:53:36 AM org.apache.catalina.core.StandardContext start
SEVERE: Error filterStart
Jul 28, 2010 12:53:36 AM org.apache.catalina.core.StandardContext start
SEVERE: Context [/StrutsHelloWorld] startup failed due to previous errors

Login or register to post comments

Shrinivas Krish... replied on Thu, 2010/08/05 - 1:53am

@Sarav Sri: I got the exact same errors that you have posted. After some trials, I was able to
overcome them by removing all the struts jar files except the 5 jars that have are needed from my
project build path.

http://java.dzone.com/articles/struts2-tutorial-part-27 10/9/2010
Struts 2 Tutorial: Create Struts 2 Application in Eclipse | Javalobby Page 11 of 11

commons-logging-1.0.4.jar, freemarker-2.3.8.jar, ognl-2.6.11.jar, struts2-core-2.0.14.jar, xwork-


2.0.7.jar

Remove all the others, export your project as a war file to tomcat/webapps and start the server, your
tomcat startup will be clean. If you are still facing any trouble, let us know.

I initially added all the jars from the struts-2.0.14-lib.zip, assuming that they will be needed for the
remaining struts2 tutorials (2 through 7). I guess I'll find out which jars are actually needed as I
progress through the remaining tuts.

@Viral Patel: Thank you for this tutorial, and the rest. They are very well written and explained. Teach
by Trying works very well for me, Thank you.

Login or register to post comments

Vinnie Pamula replied on Wed, 2010/09/22 - 2:22am

For Struts 2.2.1, Eclipse 3.6-jee, Tomcat 6 you need to make these small changes/additions for the
application to run. 1. Add the following jars to the WebContent/WEB-INF/lib/ folder: commons-
fileupload.jar commons-io.jar javaassist.jar (download from jboss files at sourceforge.net) 2. To make
"Serializable class without serialVersionUID" warning go away: Window - Preferences - Java -
Compiler - Errors/Warnings – Potential Programming Problems and set Serializable class without
serialVersionUID to "ignore". 3. “setPropertiesRule” warning message when starting Tomcat from
Eclipse: Double click on your tomcat server. It will open the server configuration. Under server options
check ‘Publish module contents to separate XML files’ checkbox. Restart your server. This time your
page will come without any issues. Your application should work without issues now.

Login or register to post comments

Comment viewing options

Flat list - expanded Date - oldest first 30 comments per page Save settings
Select your preferred way to display the comments and click "Save settings" to activate your changes.

http://java.dzone.com/articles/struts2-tutorial-part-27 10/9/2010

You might also like