Newsletter

Struts 2 Hello World Program

Struts » on Oct 22, 2011 { 31 Comments } By Sivateja

Let us see the Hello World program of struts 2, files required..

  • success.jsp
  • error.jsp
  • index.jsp
  • LogingEx.java [ in java4s package ]
  • web.xml  [ in web-inf ]
  • struts.xml  [ in web-inf/classes folder ]

Directory Structure

index.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>

<s:form action="verify">

<s:textfield name="uname" label="Enter Username" /><br>
<s:submit value="Click" align="center" />

</s:form>
</body>
</html>

success.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
You have been successfully executed struts 2 hello world program..

error.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
Login failed, wrong user name, user name must be java4s

LogingEx.java

package java4s;
import com.opensymphony.xwork2.ActionSupport;
public class LogingEx extends ActionSupport{
	private static final long serialVersionUID = 1L;

	private String uname;

	public String getUname() {
		return uname;
	}

	public void setUname(String uname) {
		this.uname = uname;
	}

	public String execute()
	{
		if(uname.equals("java4s"))
		{
			return SUCCESS;

		}else
			return ERROR;
	}

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<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>

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>
   <include file="struts-default.xml"/>
   <package name="a" extends="struts-default">
      <action name="verify" class="java4s.LogingEx">
         <result name="success">/success.jsp</result>
         <result name="error">/error.jsp</result>
      </action>
   </package>
</struts>

Execution Flow

  • Right click on the project root > Run As > Run on Server
  • index.jsp will be executed automatically, because we have given index.jsp in <welcome-file></welcome-file> of web.xml file
  • Enter username as java4s and press ‘click‘ button
  • In index.jsp we have given form action as verify [ line number 5 ], so container come to web.xml and check for URL pattern and jumps to struts.xml
  • Now it will search for action name ‘verify‘, if found then corresponding java file given in class attribute will be executed [line number 10 ]
  • Now execute() method will be executed in LogingEx.java, if condition is satisfied it will returns SUCCESS
  • Again come to struts.xml line number 11, corresponding view will be executed
  • That’s it.

After Execution

Output

Enter java4s as user name and press click button

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

31 Responses to “Struts 2 Hello World Program”
  1. Ibrahim says:

    Will you please explain different tags and uses of them in Struts.xml file. Explain tags like Constant tag and package tag in Struts.

  2. ramu says:

    I am ramu:
    please will you explain this example.
    i cont understand this example.

  3. Robert Han says:

    Hi author, thanks for the code. I now have a question, it seems like I have to add a /input.jsp to the
    for the program to run, and no matter what I input as the username, the program will always get redirected to input.jsp. Could you please advise? Thanks in advance!

  4. I am trying to execute this application with struts 2.3.8 jar files in glassfish 3.1.2.

    When I load the index.jsp after deploying, I’m getting a warning message in the log

    org.apache.struts2.components.ServletUrlRenderer|_ThreadID=91;_ThreadName=Thread-2;|No configuration found for the specified action: ‘verify’ in namespace: ”. Form action defaulting to ‘action’ attribute’s literal value.|#]

    Just followed the directory and xml structure as mentioned in the example.

    How to proceed further by solving this error?

  5. Java4s says:

    @Ramu

    Execution flow is added, please go through that.
    It should clarify your doubt 🙂

  6. I am getting “HTTP Status 404” when used the above code in the provided way.Could you please guide on it?

  7. Nishant says:

    Hi Siva,

    I some books action class does not extend the ActionSupport class. Please explain is it necessary to extend ActionSupport or not.

  8. Sanjay says:

    Hi Nishant,

    As Action Support Class implements Action,Validetable,Validation aware,text provider,Locale provider & Serilizable interface by default, so in our Action class by Default we will get the Advantages(Methods, Constants …etc ) in our class. It Makes our Design Robust.

    Thanks

  9. venkatesh says:

    excellent answer good web site i gave 100 marks for clear explanation
    so please help me for more answers for my request…..
    thanking you,
    venkatesh.j

  10. If two servlet having same value then which will be loaded first?

  11. Raghu says:

    Sir, Are we explicity create the class folder in web-inf folder.because there is no folder in this name in that place.

  12. Abid says:

    Thank you very much for this tutorial.
    I have one question that in execute() method of LogingEx class ,string variables SUCCESS or ERROR is return but where its actually declared or initialized??

  13. Abhishek says:

    Hi, I tried the same example with Struts2 2.16.3 version and i am getting org.apache.struts2.dispatcher.Dispatcher – Dispatcher initialization failed error mssg. can you please look into it?

  14. I am doing StrutsHelloWorld demo.
    My directory structure is not showing WEB-INF/classes directory which contains struts.xml file.Is it the reason why my program is not running.It is showing error that
    requested resource culdn’t found.

  15. hello dear,
    I am getting the following error when i compile LoginEx.java file
    Please help me….Thanks in advance

    LogingEx .java:2: error: package com.opensymphony.xwork2 does not exist
    import com.opensymphony.xwork2.ActionSupport;

  16. Satyendra Mishra says:

    Hello frnd,
    In action class either we can declare SUCCESS or ERROR both as String or in return statement enclosed with(” SUCCESS”)OR (“ERROR”) by mistake he forgot to write double quote.
    In struts1.X we have tag forward and in struts2.x we are using result tag. What ever execute() method return success or error based on that it check in result tag and calls appropriate jsp file.

  17. hari krishna says:

    Hi Sir ,

    i am getting the following exception. Please help

    “No configuration found for the specified action: ‘verify’ in namespace:”

  18. saikiran says:

    while running the above code iam getting HTTP Status 404

  19. Rupesh Vislawath says:

    Hello sir,
    Thank you for clear explanation. I followed your exactly as it is but still I get “HTTP Status 404 – /StrutsHelloWorld/verify” error.

  20. jithendar says:

    good example.. it is very easy to understand.. thank you

  21. Jhansi says:

    sir, i got http 500 error
    will you please explain how to rectify this error and execute the program

  22. kumaresan says:

    used this code nullpionter exception occured?please tell me soloution?

    ex::::
    java.lang.NullPointerException
    org.apache.struts2.impl.StrutsActionProxy.getErrorMessage(StrutsActionProxy.java:69)
    com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)

  23. nabarun says:

    I am getting an error “HTTP Status 404 – /StrutsHelloWorld/verify” error. I am downloaded and imported your project. So many guyz have the same problem please answer to it. I am a regular reader of your site..

  24. pratik chauhan says:

    can u please tell me how to get struts tags file

  25. Akhil says:

    I am trying to do this Example but i got this error”HTTP Status 404 – /Strutsex/

    type Status report

    message /Strutsex/

    description The requested resource is not available.

    Apache Tomcat/7.0.34
    pls Help me Sir

  26. sonia says:

    I am trying to execute this example but i am getting error HTTP status 404
    please help asap

  27. Dijeesh says:

    From where, the user name and password is setting?

  28. souma says:

    error 404 :p

  29. this is the awesome post for me.
    keep posting .
    thanks

  30. Mani says:

    Your explanation was very nice bro.

  31. Ramkrishna Sitap says:

    Nice example..,
    Thank you sir

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.