In this tutorial we
will see how to create a simpe Struts 2 Hello World Application. The following
files are needed to create a Hello World Application.
·
web.xml
·
struts.xml
·
HelloWorld.java
·
index.jsp
·
success.jsp
The following
picture shows the directory structure of the Hello World application.
web.xml
web.xml is used to configure the
servlet container properties of the hello world appliation. The filter and the
filter-mapping elements are used to setup the Struts 2 FilterDispatcher. The
filter is mapped to the URL pattern "/*". This means all the incoming request that targets to the Struts 2
action will be handled by FilterDispatcher class.
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.strutsdispatcher.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>
The gateway for our
hello world application is index.jsp file. The index.jsp file should be
mentioned in web.xml as shown above.
struts.xml
The entry point to
the XML declarative architecture is struts.xml file. The struts.xml file
contains the following action mapping.
<struts>
<package name="default" extends="struts-default">
<action name="HelloWorld" class="vaannila.HelloWorld">
<result name="SUCCESS">/success.jsp</result>
</action>
</package>
</struts>
index.jsp
The Struts 2 UI
tags are simple and powerful. To use the struts tags in the jsp page the
following taglib directive should be included.
<%@taglib
uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<s:form action="HelloWorld" >
<s:textfield name="userName" label="User Name" />
<s:submit />
</s:form>
</body>
</html>
HelloWorld.java
As you see the
HelloWorld class is very simple. It contains two properties one for the user
name and the other for displaying the message.
public class HelloWorld {
private String message;
private String
userName;
public HelloWorld() {
}
public String
execute() {
setMessage("Hello
" + getUserName());
return "SUCCESS";
}
public String
getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String
getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
In the execute()
method of the HelloWorld action we compose the message to be displayed. Note we
need not have a seperate form bean like struts 1 to access the form data. We
can have a simple java class as action. The action need not extend any class or
implement any interface. The only obligation is that you need to have an
execute() method which returns a String and has a public scope.
success.jsp
In the success page we display the
"Hello
User" message using the property tag.
<%@taglib
uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1><s:property value="message" /></h1>
</body>
</html>
Extract the downloaded files into the
webapps folder of Tomcat. Start the Tomcat server. Type the following url in
the browser "http://localhost:8080/Example1/index.jsp". The index
page will be displayed.
Enter the user name
and submit the form. Hello user name message will be displayed.
No comments:
Post a Comment