You can view the tutorials best in Google Chrome, Mozilla Firefox, Opera, higher version of Internet Explorer

Struts 2 Hello World Program

Struts » On Oct 22, 2011 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

success.jsp

error.jsp

LogingEx.java

web.xml

struts.xml

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






By [ Java/J2EE Developer & Web Designer ]
Founder of Java4s.com - Indian, lives in United States. You can contact me at sivateja [at] java4s.com
Follow me on


Comments

18 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

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links
Current & UpComing Tutorials
Sitemap :
spring Hibernate struts Json Ajax Log4j Log4j coreJava Servlets Web Services


Upcoming
  Web Services [ In Progress ], JSP
Facebook_Java4s  Twitter_Java4s  GooglePlus_Java4s  Youtube_Java4s 
© 2010 - 2014 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.