Tutorial: Create Struts 2 Application in Eclipse

struts2-hello-world
Welcome to the Part 2 of 7-part series where we will explore the world of Struts 2 Framework. In previous article we went through the basics of Struts2, its Architecture diagram, the request processing lifecycle and a brief comparison of Struts1 and Struts2. If you have not gone through the previous article, I highly recommend you to do that before starting hands-on today.

Things We Need

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

  1. JDK 1.5 above (download)
  2. Tomcat 5.x above or any other container (Glassfish, JBoss, Websphere, Weblogic etc) (download)
  3. Eclipse 3.2.x above (download)
  4. Apache Struts2 JAR files:(download). Following are the list of JAR files required for this application.
    • commons-logging-1.0.4.jar
    • freemarker-2.3.8.jar
    • ognl-2.6.11.jar
    • struts2-core-2.0.12.jar
    • xwork-2.0.6.jar

    Note that depending on the current version of Struts2, the version number of above jar files may change.

Our Goal

Our goal is to create a basic Struts2 application with a Login page. User will enter login credential and if authenticated successfully she will be redirected to a Welcome page which will display message ” Howdy, <username>…!“. If user is not authenticated, she will be redirected back to the login page.
struts2-application-login-page

Getting Started

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.
Dynamic Web Project in Eclipse

After selecting Dynamic Web Project, press Next.
Eclipse Struts2 Project

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.

Once the project is created, you can see its structure in Project Explorer.
Eclipse Project Explorer: Struts2 Example

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

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.

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

	<display-name>Struts2 Application</display-name>
	<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>Login.jsp</welcome-file>
	</welcome-file-list>

</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.
struts2-source-package
Create a class called LoginAction in net.viralpatel.struts2 package with following content.

package net.viralpatel.struts2;

public class LoginAction {
	private String username;
	private String password;

	public String execute() {

		if (this.username.equals("admin")
				&& this.password.equals("admin123")) {
			return "success";
		} else {
			return "error";
		}
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}

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.
struts2-resource-folder
Specify folder name resources and press Finish.

Create a file ApplicationResources.properties under resources folder.
struts-2-application-resources-properties
Copy following content in ApplicationResources.properties.

label.username= Username
label.password= Password
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.

Login.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Struts 2 - Login Application | ViralPatel.net</title>
</head>

<body>
<h2>Struts 2 - Login Application</h2>
<s:actionerror />
<s:form action="login.action" method="post">
	<s:textfield name="username" key="label.username" size="20" />
	<s:password name="password" key="label.password" size="20" />
	<s:submit method="execute" key="label.login" align="center" />
</s:form>
</body>
</html>

Welcome.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Welcome</title>
</head>

<body>
	<h2>Howdy, <s:property value="username" />...!</h2>
</body>
</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.
struts2-struts-xml
Copy following content into struts.xml.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	<constant name="struts.enable.DynamicMethodInvocation"
		value="false" />
	<constant name="struts.devMode" value="false" />
	<constant name="struts.custom.i18n.resources"
		value="ApplicationResources" />

	<package name="default" extends="struts-default" namespace="/">
		<action name="login"
			class="net.viralpatel.struts2.LoginAction">
			<result name="success">Welcome.jsp</result>
			<result name="error">Login.jsp</result>
		</action>
	</package>
</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.

	<action name="login" method="authenticate"
		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.
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:

label.username= Username
label.password= Password
label.login= Login
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:

package net.viralpatel.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
	private String username;
	private String password;

	public String execute() {

		if (this.username.equals("admin")
				&& this.password.equals("admin123")) {
			return "success";
		} else {
			addActionError(getText("error.login"));
			return "error";
		}
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}

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
struts2-application-login-page

Welcome page
struts2-welcome-page

Login page with error
struts2-login-page-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.

If you read this far, you should follow me on twitter here.



201 Comments

  • Priyanka wrote on 11 July, 2010, 0:40

    HTTP Status 404 – There is no Action mapped for namespace / and action name .

    type Status report

    message There is no Action mapped for namespace / and action name .

    description The requested resource (There is no Action mapped for namespace / and action name .) is not available.

    please let me know why this error is coming. I dint modify anything from above code.

    • deepak gaikwad wrote on 10 June, 2011, 15:01

      hiiiiiiiii
      please make the statement as public class loginAction extends ActionSupport
      reply me urgently ask me any problem if you have about struts2.0

  • vara wrote on 12 July, 2010, 16:18

    /Login/LoginAssitancestep1.jsp

    when i am deploy and run it in browser at that time when click on button, the button action name is “LoginAssi1.action” but in url it will show the folder name after Project name. like in this way
    “http://localhost:15789/MF_SSOWEB/Login/LogAssi1.action”
    how to remove “/Login” from the url………i want “http://localhost:15789/MF_SSOWEB/LogAssi1.action” in this only for in this what i can do………
    if u know plz reply me……………..
    my mail id is
    “xxxx@gmail.com”

    • Viral Patel wrote on 12 July, 2010, 16:43

      @vara: Check your struts.xml. In <package> tag does the namespace is “Login”? If so then change it to namespace=”/”. Hope this will work.

  • vara wrote on 14 July, 2010, 14:55

    ya, i can try for it, but no use. in the url again “http://localhost:15789/MF_SSOWEB/Login/LogAssi1.action” came in this way. if remove “/Login” from the url it will came. now it will came, what is the problem in my code.

    struts.xml:

    /Registration/Registration.jsp

    /Registration/Success.jsp
    /Registration/Registration.jsp

    /campagainplan/ProductPortal.jsp
    /Login/Login.jsp

    /Login/LoginAssitancestep1.jsp

    /Login/LoginAssistancestep2.jsp
    /Login/LoginAssitancestep1.jsp

    /Login/LoginAssistancestep3.jsp
    /Login/LoginAssistancestep2.jsp

    /Login/LoginAssiEmail.jsp

    /Login/LoginAssistancestep2ForgotPass.jsp
    /Login/LoginAssiEmail.jsp

    /Login/LoginAssiStep4.jsp
    /Login/LoginAssistancestep2ForgotPass.jsp

    /Login/Login.jsp
    /Login/LoginAssistancestep3.jsp

    /Login/ForgotPasswordComplete.jsp
    /Login/LoginAssiStep4.jsp

    /Login/Login.jsp

  • vinay wrote on 15 July, 2010, 9:45

    deploy?path=/home/varaprasads/NetBeansProjects/MF_SSOWEB/build/web&name=MF_SSOWEB&force=true failed on GlassFish v3 (2)
    /home/varaprasads/NetBeansProjects/MF_SSOWEB/nbproject/build-impl.xml:695: The module has not been deployed.

    i got the above error at the time of deploy………….what i can do for remove the error………….

  • avijit wrote on 17 July, 2010, 19:17

    hi Viral,

    I am very new to Struts and Just have Idea of MVC. I was trying to develop the sturts application you have explained here. But unfortunately I m getting some exceptions here. Please look into it and help me.

    Here is the Exception stack trace,

    17 Jul, 2010 7:38:35 PM org.apache.catalina.core.AprLifecycleListener init
    SEVERE: Dispatcher initialization failed
    at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:58)
    Caused by: Unable to load bean: type:org.apache.struts2.dispatcher.multipart.MultiPartRequest class:org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest – bean – jar:file:/C:/Workspace/.metadata/.plugins/org.eclipse.wst.server.coreCaused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
    Caused by: java.lang.NoClassDefFoundError: org/apache/commons/fileupload/RequestContext
    Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
    17 Jul, 2010 7:38:37 PM org.apache.catalina.core.StandardContext start
    SEVERE: Error filterStart
    17 Jul, 2010 7:38:37 PM org.apache.catalina.core.StandardContext start
    SEVERE: Context [/StrutsHelloWorld] startup failed due to previous errors

  • avijit wrote on 17 July, 2010, 19:52

    The problem has been resolved now.
    I have included commons-fileupload-1.2.1.jar in the WEB-INF/lib folder. After that it sarted working.

    Thanks to Viral for a simple explanation.

  • avijit wrote on 18 July, 2010, 11:17

    Now I am facing new problem. Once I clicked the “Login” button in the Login.jsp page after entering the username and password, it does not redirect the page to either Welcome.jsp or Login.jsp. it does not show me the warning either, if at all I am entering the wrong username and password.

    Here is the exception stack trace,

    18 Jul, 2010 11:38:58 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.
    18 Jul, 2010 11:38:58 AM org.apache.coyote.http11.Http11Protocol init
    INFO: Initializing Coyote HTTP/1.1 on http-8080
    18 Jul, 2010 11:38:58 AM org.apache.catalina.startup.Catalina load
    INFO: Initialization processed in 801 ms
    18 Jul, 2010 11:38:58 AM org.apache.catalina.core.StandardService start
    INFO: Starting service Catalina
    18 Jul, 2010 11:38:58 AM org.apache.catalina.core.StandardEngine start
    INFO: Starting Servlet Engine: Apache Tomcat/6.0.28
    18 Jul, 2010 11:38:59 AM com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
    INFO: Parsing configuration file [struts-default.xml]
    18 Jul, 2010 11:38:59 AM com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
    INFO: Unable to locate configuration files of the name struts-plugin.xml, skipping
    18 Jul, 2010 11:38:59 AM com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
    INFO: Parsing configuration file [struts-plugin.xml]
    18 Jul, 2010 11:38:59 AM com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
    INFO: Parsing configuration file [struts.xml]
    18 Jul, 2010 11:38:59 AM com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
    INFO: Loading global messages from ApplicationResources
    18 Jul, 2010 11:38:59 AM org.apache.coyote.http11.Http11Protocol start
    INFO: Starting Coyote HTTP/1.1 on http-8080
    18 Jul, 2010 11:38:59 AM org.apache.jk.common.ChannelSocket init
    INFO: JK: ajp13 listening on /0.0.0.0:8009
    18 Jul, 2010 11:38:59 AM org.apache.jk.server.JkMain start
    INFO: Jk running ID=0 time=0/26 config=null
    18 Jul, 2010 11:38:59 AM org.apache.catalina.startup.Catalina start
    INFO: Server startup in 1608 ms

    Can anyone please help me out in this case.. I cant sort it out this time.

  • Deep wrote on 26 July, 2010, 14:32

    awesome awesome…
    Thanks to Viral and Sas…
    M a lazy fellow, I added all the jar files.. and was dealing wd the errors as ppl abve
    nice tutorial
    Me Likes…

  • Vaibhav Pawar wrote on 2 August, 2010, 16:05

    Hello Viral,
    I am trying same tut in eclipse. I’ve included the .jar files you have said.
    But m still getting the same problem as that of ‘Chris’ – “Can not find the tab library descriptor for “/struts-tags” in both Login.jsp and Welcome.jsp. I’ve included ‘servlet-2.3.jar’ extra.
    Please help….
    Thanks.

  • dilini wrote on 6 August, 2010, 13:09

    I followed this tutorial. when i click the submit button it doesn’t go to the welcome.jsp page.
    it gives the following error message.

    type Status report

    message There is no Action mapped for namespace / and action name login.

    my folder structure is same as above.

    description The requested resource (There is no Action mapped for namespace / and action name login.) is not available.

  • dilini wrote on 6 August, 2010, 13:12

    Sorry. I have made a mistake.
    Error message is

    Status report
    message There is no Action mapped for namespace / and action name login.
    description The requested resource (There is no Action mapped for namespace / and action name login.) is not available.

  • vipin wrote on 10 August, 2010, 11:57

    in struts.xml file do this

    remove method= authenticate. also in jsp files give space between ” and % you can run program

  • Priya wrote on 9 September, 2010, 15:30

    HTTP Status 404 – /StrutsHelloWorld/

    ——————————————————————————–

    type Status report

    message /StrutsHelloWorld/

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

    ——————————————————————————–

    i am getting this when i run the above example. i dont know why. is there any problem in my tomcat?

    plz help me

    • Daniel wrote on 18 June, 2011, 15:27

      Thx for the tutorial. It would be even greater if i could run it ;)
      I have the exactly same problem using Tomcat 7.
      Is there a solution to this?
      Can anyone tell me?

  • vin wrote on 14 September, 2010, 13:22

    hi patel
    i can’t understand the u have given name in struts.xml ,”login” and “form action=”login.action” method=”post”" in login.jsp.
    i m little bit confused to understand this naming convention.
    pls help me.

  • vin wrote on 15 September, 2010, 9:10

    hello viral,
    i have solved the problem but still not understand the naming convention u have given .
    i m a beginner for struts plz. helm me.

  • tanti wrote on 16 September, 2010, 14:59

    i also have some problems like dilini

    the title that read from applicationresources cannot shown and
    when i click the submit button it doesn’t go to the welcome.jsp page.
    it gives the following error message.

    type Status report

    message There is no Action mapped for namespace / and action name login.

    my folder structure is same as above.

    description The requested resource (There is no Action mapped for namespace / and action name login.) is not available.

    thanks

  • grasshopper wrote on 23 September, 2010, 0:40

    I am using Tomcat 7 and had to add the following jars to my WEB-INF\lib folder:

    Commons-logging-1.0.4.jar
    commons-fileupload-1.2.1.jar
    commons-io-1.3.2.jar
    freemarker-2.3.16.jar
    javassist-3.7.ga.jar
    ognl-3.0.jar
    struts2-core-2.2.1.jar
    xwork-core-2.2.1.jar

    Many thanks for the tutorial. Really appreciate your work!

  • Sarwar wrote on 8 October, 2010, 22:44

    I cannot run it. Here’s what I get why I try to run it on server

    Oct 8, 2010 10:34:02 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:\Program Files\Java\jre6\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre6/bin/client;C:/Program Files/Java/jre6/bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Intel\DMIX;C:\Program Files\Java\jdk1.6.0_20\bin;C:\Program Files\Liquid Technologies\Liquid XML Studio 2010\XmlDataBinder8\Redist8\cpp\win32\bin;C:\Program Files\CVSNT\
    Oct 8, 2010 10:34:02 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
    WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property ‘source’ to ‘org.eclipse.jst.jee.server:StrutsJatra’ did not find a matching property.
    Oct 8, 2010 10:34:02 AM org.apache.coyote.http11.Http11Protocol init
    INFO: Initializing Coyote HTTP/1.1 on http-8080
    Oct 8, 2010 10:34:02 AM org.apache.catalina.startup.Catalina load
    INFO: Initialization processed in 429 ms
    Oct 8, 2010 10:34:02 AM org.apache.catalina.core.StandardService start
    INFO: Starting service Catalina
    Oct 8, 2010 10:34:02 AM org.apache.catalina.core.StandardEngine start
    INFO: Starting Servlet Engine: Apache Tomcat/6.0.29
    Oct 8, 2010 10:34:02 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:1645)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1491)
    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:4001)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4651)
    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:445)
    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)
    Oct 8, 2010 10:34:02 AM org.apache.catalina.core.StandardContext start
    SEVERE: Error filterStart
    Oct 8, 2010 10:34:02 AM org.apache.catalina.core.StandardContext start
    SEVERE: Context [/StrutsJatra] startup failed due to previous errors

  • vidhya wrote on 21 October, 2010, 17:00

    hi viral ur tutorial is very simple and understandaable .. i exactly folowed ur steps but am getting the following error.. plz any1 help
    WARNING: No configuration found for the specified action: ‘login.action’ in namespace: ”. Form action defaulting to ‘action’ attribute’s literal value.
    Oct 21, 2010 5:20:15 PM org.apache.struts2.components.Form evaluateExtraParamsServletRequest
    WARNING: No configuration found for the specified action: ‘login.action’ in namespace: ”. Form action defaulting to ‘action’ attribute’s literal value.
    Oct 21, 2010 5:20:24 PM org.apache.struts2.dispatcher.Dispatcher serviceAction
    SEVERE: Could not find action or result
    There is no Action mapped for namespace / and action name login. – [unknown location]
    at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:186)
    at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:41)
    at java.lang.Thread.run(Thread.java:595)
    Oct 21, 2010 5:21:46 PM org.apache.struts2.components.Form evaluateExtraParamsServletRequest
    WARNING: No configuration found for the specified action: ‘login.action’ in namespace: ”. Form action defaulting to ‘action’ attribute’s literal value.
    Oct 21, 2010 5:21:46 PM org.apache.struts2.components.Form evaluateExtraParamsServletRequest
    WARNING: No configuration found for the specified action: ‘login.action’ in namespace: ”. Form action defaulting to ‘action’ attribute’s literal value.
    Oct 21, 2010 5:21:54 PM org.apache.struts2.dispatcher.Dispatcher serviceAction
    SEVERE: Could not find action or result
    There is no Action mapped for namespace / and action name login. – [unknown location]
    at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:186)
    at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:41)
    at java.lang.Thread.run(Thread.java:595)

  • Pratik Shah wrote on 26 October, 2010, 17:09

    A very useful and easy to understand tutorial.
    It has increased my knowledge horizion to no extent.
    Kudos to Viral.
    Thanks

  • hms wrote on 31 October, 2010, 20:45

    Hello Viral

    I am kind of stuck here, I have tried first following your instructions and then even tried downloading and running your code. I am using following JARs:
    commons-fileupload-1.2.1
    commons-io-1.3.2
    commons-logging-1.0.4
    freemarker-2.3.16
    ognl-3.0
    struts2-core-2.2.1
    xwork-core-2.2.1

    and running on Tomacat6 server using JDK1.6.0_20

    I am getting following exception

    SEVERE: Dispatcher initialization failed
    java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
    Caused by: java.lang.reflect.InvocationTargetException
    Caused by: java.lang.ExceptionInInitializerError
    … 46 more
    Caused by: java.lang.IllegalArgumentException: Javassist library is missing in classpath! Please add missed dependency!
    … 47 more
    Caused by: java.lang.ClassNotFoundException: javassist.ClassPool
    … 47 more
    can you please help on the same.

    Regards

  • hms wrote on 31 October, 2010, 21:55

    the error got resolved after adding javassist-3.7.ga.jar

  • snap wrote on 2 November, 2010, 2:23

    thanks for the tutorial!

  • jugi wrote on 4 November, 2010, 23:20

    I have a problem when i run my StrutsHelloWorld i am getting a error that
    type Status report

    message There is no Action mapped for namespace / and action name login.

    description The requested resource (There is no Action mapped for namespace / and action name login.) is not available.

    after clicking the login button.plz help

  • Raj wrote on 12 November, 2010, 10:50

    Well Explained tutorial. Kudos for the good work

  • Kashyap wrote on 15 November, 2010, 17:24

    Very very ve……..ry useful doccument. I have been fighting since long time to get my sample struts2 application working through Eclipse. Searched number of pages, tried, copy-pasted number of demos but didn’t find anything as useful as this one. Thanks a lot man! Just followed the steps (truely clear and complete description… you get one more point for that) and wo…… my (??? :P ) demo started working!!!

  • joec wrote on 17 November, 2010, 2:05

    also get 404 not found error, however get it fixed by removing spaces in web.xml file(which may occur during copy and past).

    thanks for the tutorial, Excellent article.

  • Ananth wrote on 18 November, 2010, 15:20

    hey virpal,

    i just followed these steps a to z ,but still i m getting error like the requested resource(StrutshelloWorld) is not available………i checked all the jars also……pls help me……..

  • vikrant sharma wrote on 1 December, 2010, 15:36

    very nice tutorial……………….thanx

  • Mark wrote on 5 January, 2011, 1:30

    The 404 error happens when you have missing .jar files… specifically,
    commons-fileupload-1.2.2.jar
    and
    commons-io-1.3.jar
    (The version number may vary)
    This differs depending on the apache tomcat installation you have. Some will not get an error. If using the specific Windows 32 bit Tomcat, then you will need to drop these two jars into your WEB-INF directory, they are not automatically included with the Windows Tomcat download.

  • Ghanashyam wrote on 20 January, 2011, 10:50

    Viral, thanks for such a writeup. I would recommend to all my friends. This is the best

    • Viral Patel wrote on 20 January, 2011, 14:27

      @Ghanashyam – Thanks for the kind words :) I am glad these tutorials are helpful to you.

  • David Jensen wrote on 31 January, 2011, 5:39

    grasshopper,
    Thanks for listing the jars, worked like a charm.

    I seek not to know the answers, but to understand the questions.
    Caine “Kung Fu”

  • king Shinok wrote on 24 February, 2011, 5:06

    It’s a great job. keep up

    just need to add
    commons-fileupload-1.2.1.jar
    to WEB-INF/lib

  • sachin wrote on 27 February, 2011, 11:04

    About the 404 error. I needed to add javassist.jar. Then it went fine. But the console of tomcat startup has lot of errors. I don’t really understand what they mean? But here are they:
    a) WARNING: Could not create JarEntryRevision for [jar:file:/C:/Environment/Workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/StrutsHelloWorld/WEB-INF/lib/struts2-core-2.2.1.1.jar]!

    b) WARNING: No configuration found for the specified action: ‘login’ in namespace: ”. Form action defaulting to ‘action’ attribute’s literal value. : On this, Does it have to do with
    , because in struts.xml it is different,->login.action

    Any suggestions Viral?

    But anyway good tutorial and thanks a lot!

  • sriram wrote on 1 March, 2011, 10:42

    Hi viral sir,i use netbeans 6.9.1 to run this example but applicationresources.properties file values is not getting into the application.so plz clarify my doubt.

    • Viral Patel wrote on 2 March, 2011, 15:33

      @Sriram – The IDE doesn’t have to do anything with the application flow. Check if you have properly define constant struts.custom.i18n.resources in your struts.xml as discussed in above example.

  • jay singh wrote on 30 March, 2011, 10:07

    HTTP Status 404 –

    ——————————————————————————–

    type Status report

    message

    description The requested resource () is not available.

    ——————————————————————————–

    Apache Tomcat/6.0.29

  • christopher wrote on 5 April, 2011, 11:31

    for 404 error
    download and include in lib the jar xwork-2.1.6.jar

  • singelton_trop wrote on 11 April, 2011, 14:08

    try start but got errors
    SEVERE: Dispatcher initialization failed
    java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
    Caused by: java.lang.reflect.InvocationTargetException
    … 32 more
    Caused by: java.lang.ExceptionInInitializerError
    at com.opensymphony.xwork2.ognl.OgnlValueStackFactory.setContainer(OgnlValueStackFactory.java:85)
    … 37 more
    Caused by: java.lang.IllegalArgumentException: Javassist library is missing in classpath! Please add missed dependency!
    at ognl.OgnlRuntime.(OgnlRuntime.java:165)
    … 38 more
    Caused by: java.lang.ClassNotFoundException: javassist.ClassPool
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
    … 38 more
    11.04.2011 11:50:54 org.apache.catalina.core.StandardContext filterStart
    SEVERE: Exception starting filter struts2
    java.lang.reflect.InvocationTargetException – Class: com.opensymphony.xwork2.inject.ContainerImpl$MethodInjector
    File: ContainerImpl.java
    Method: inject
    Line: 295 – com/opensymphony/xwork2/inject/ContainerImpl.java:295:-1
    at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:428)
    at org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:190)

    pls help…
    tnx for any advices

  • Vishnu wrote on 13 April, 2011, 12:31

    Tutorial is Awsome,

    But the i could not get application to run , Viral , please help

    Below is the Error i get

    HTTP Status 404 – There is no Action mapped for namespace / and action name login.

    ——————————————————————————–

    type Status report

    message There is no Action mapped for namespace / and action name login.

    description The requested resource (There is no Action mapped for namespace / and action name login.) is not available.

    ——————————————————————————–

    Apache Tomcat/5.5.17

    • Viral Patel wrote on 13 April, 2011, 13:25

      @Vishnu – Please check if there is some other exception in error log. 404 error comes only when all the Struts library are not loaded properly due to issues such as missing jar files, incompatible class files, configuration issues etc.

  • anjan wrote on 16 April, 2011, 23:48

    Hi
    i m very new to Struts
    i did everything as per ur instruction but now i m getting the below error
    can u plz suggest me how i ll overcome from this error

    this is my folder structure –

    HTTP Status 404 – /StrutsHelloWorld/Login.jsp

    ——————————————————————————–

    type Status report

    message /StrutsHelloWorld/Login.jsp

    description The requested resource (/StrutsHelloWorld/Login.jsp) is not available.

    ——————————————————————————–

    Apache Tomcat/

    plz help me

    tnx

  • Jai wrote on 20 April, 2011, 8:25

    Hi All. First of all a good introduction to Struts2 and a simple app. I was facing some issues which was resolved as below.
    1. LabelNames not Appearing properly:
    Keep your struts.xml in WEB-INF/Classes folder. Create a folder called classes in WEB-INF and put your struts.xml there. Keep your ApplicationResources.properties in resources folder as mentioned. The labels will default.
    2. Namespace error:
    Just enter your package name as created in the src folder, no need of namespace tag.

  • Prasanna wrote on 20 April, 2011, 21:14

    Hi,

    I’m new to Struts2. While trying to build, I’m getting the following errors.. please help..

    1.com.opensymphony cannot be resolved…
    2. ActionSupport cannot be resolved
    3. Cannot find the tag library desscriptor for /Struts-tags

    Thank you.
    Prasanna

  • Latif wrote on 29 April, 2011, 21:25

    I imported the source code form this site and I get the following error.I’m running apache tomcat 7.0.8:

    Please assist

    type Status report

    message

    description The requested resource () is not available.

  • mega wrote on 13 May, 2011, 6:27

    Plz help me,
    i tried everything but always get error code 404 requested Resource not available.
    i am stuck with it, wheneve i import other’s struts wars ,they run fine but not able to run any newly created struts projects
    thanks.

  • octavian wrote on 18 May, 2011, 0:06

    Hi, I have the same error:
    HTTP Status 404 –
    ——————————————————————————–
    type Status report
    message
    description The requested resource () is not available.
    ——————————————————————————–
    Apache Tomcat/7.0.11
    I don’t understand, where is the problem,
    thank you!

  • Vu Le wrote on 23 May, 2011, 7:17

    Hi Viral,
    Many thanks for this great tutorial.
    Btw, the source code which you posted has an error.
    In “struts.xml” file, you specified method as “authenticate()”

    but in LoginAction.java you set default method “execute()” which causes a problem if user clicks “Login” button.

    Regards,

  • Pramod wrote on 5 June, 2011, 18:03

    Hello,

    When I followed the procedure mentioned above, I got the 404 error.
    I’m using Tomcat 6 and had added all the jars from struts 2.2.3.

    Below are the problems I faced :-

    1. I removed all the jars and added only the 5 jars mentioned above but got the same error again.
    After going through the server log, I started adding the missing jar mentioned in the log and finally ran the program successfully.

    Below is the list of jars besides the 5 mentioned above I had to add :-
    1. commons-fileupload-1.2.2
    2.commons-io-2.0.1
    3.commons-lang-2.5
    4.javassist-3.11.0.GA

    2] Also the above screen doesn’t show our action class extending ActionSupport and importing the related package for it though it is mentioned in the source code.

    3] I was bit confused regarding the position of struts.xml file as to whether I should put it under WebContent. But the above mapping works fine.

    And thank you so much for posting this fabulous tutorial.

    Cheers,
    Pramod

  • javaWhatThe wrote on 14 June, 2011, 11:53

    this is why i hate java web development. so many exceptions for proper configuration( not for a real coding – - +). horrible configuration. and not working ..not working.. because of whatever mistake or something wrong in a configuration!

  • jeremyko69 wrote on 16 June, 2011, 12:40

    in your UserImage.jsp, I’m having a strange problem…

    this does not work..

    But..

    it works! what’s the problem with me??

  • jeremyko69 wrote on 16 June, 2011, 12:42

    sorry. posting again..
    in the UserImage.jsp, I’m having a strange problem…
    this does not work..
    …action=”userImage” method=…..

    But..
    …action=”userImage.action” method=…..
    it works! what’s the problem with me??

Leave a Reply

Your email address will not be published. Required fields are marked *

*