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 | { 9 Comments }

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

 

What you are thinkig....

9 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

If you want a pic to show with your comment, go get a gravatar !
Please post your questions on Java4s Answers forum

Name*
Ask a Question ?
or
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 Java4s.com
Tutorials Online :
spring Hibernate struts Json Ajax Log4j Log4j
coreJava Servlets


UpComing :
Servlets, Jsps
is optimized for learning java technologies, all the examples in this site are constantly reviewed to avoid errors. While using this site you agree to have read and accepted our terms of use and privacy policy
Especially i have prepared this blog by keeping fresher's in mind, however it will be very useful for real time developers too.


© 2013 Java4s All rights reserved. | strPro4Tut v(2.0) Theme designed by str-Graphics.com