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 .:: | ||
Comments
31 Responses to “Struts 2 Hello World Program”
Will you please explain different tags and uses of them in Struts.xml file. Explain tags like Constant tag and package tag in Struts.
I am ramu:
please will you explain this example.
i cont understand this example.
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!
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?
@Ramu
Execution flow is added, please go through that.
It should clarify your doubt 🙂
I am getting “HTTP Status 404” when used the above code in the provided way.Could you please guide on it?
Hi Siva,
I some books action class does not extend the ActionSupport class. Please explain is it necessary to extend ActionSupport or not.
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
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 two servlet having same value then which will be loaded first?
Sir, Are we explicity create the class folder in web-inf folder.because there is no folder in this name in that place.
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??
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?
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.
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;
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.
Hi Sir ,
i am getting the following exception. Please help
“No configuration found for the specified action: ‘verify’ in namespace:”
while running the above code iam getting HTTP Status 404
Hello sir,
Thank you for clear explanation. I followed your exactly as it is but still I get “HTTP Status 404 – /StrutsHelloWorld/verify” error.
good example.. it is very easy to understand.. thank you
sir, i got http 500 error
will you please explain how to rectify this error and execute the program
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)
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..
can u please tell me how to get struts tags file
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
I am trying to execute this example but i am getting error HTTP status 404
please help asap
From where, the user name and password is setting?
error 404 :p
this is the awesome post for me.
keep posting .
thanks
Your explanation was very nice bro.
Nice example..,
Thank you sir