Struts Tutorial: Hello World Example in Eclipse
Struts Tutorial: Hello World Example in Eclipse
Struts Tutorial: Hello World Example in Eclipse
html
In this tutorial you will learn how to create a Struts hello world application in eclipse. First
create a new project, go to File->New and select DynamicWebProject.
On executing the application the "Hello World" message gets displayed to the user.
MVC Architecture Subscrib
e
RSS
Struts MVC Architecture
The following events happen when the Client browser issues an HTTP request.
Bundles all the request values into a JavaBean class which extends
Struts ActionForm class.
Decides which action class to invoke to process the request.
Validate the data entered by the user.
The action class process the request with the help of the model
component. The model interacts with the database and process the request.
After completing the request processing the Action class returns
an ActionForward to the controller.
Based on the ActionForward the controller will invoke the appropriate
view.
The HTTP response is rendered back to the user by the view
component.
Hello World Application
Lets say a quick hello to struts. Struts follows MVC 2 pattern. The following files are needed
to create a hello world application.
index.jsp
helloWorld.jsp
web.xml
struts-config.xml
HelloWorldAction.java
HelloWorldActionForm.java
web.xml
web.xml is used to configure the servlet container properties of the hello world appliation.
1.<welcome-file-list>
2.<welcome-file>index.jsp</welcome-file>
3.</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.
index.jsp
In the hello world example the index.jsp page simply forwards the request to the hello world
action.
1.<jsp:forward page="HelloWorld.do"/>
struts-config.xml
struts-config.xml file is used to configure the struts framework for the hello world
application. This file contains the details regarding the form bean and the action mapping.
01.<struts-config>
02.
03.<form-beans>
04.<form-bean name="HelloWorldActionForm"
05.type="com.vaannila.HelloWorldActionForm"/>
06.</form-beans>
07.
08.<action-mappings>
09.<action input="/index.jsp" name="HelloWorldActionForm"path="/HelloWorld" sco
pe="session"type="com.vaannila.HelloWorldAction">
10.<forward name="success" path="/helloWorld.jsp" />
11.</action>
12.</action-mappings>
13.
14.</struts-config>
HelloWorldActionForm.java
HelloWorldActionForm extends org.apache.struts.action.ActionForm. HelloWorldActionForm
class has one String variable message and the corresponding getter and setter methods.
01.public class HelloWorldActionForm extends
02.org.apache.struts.action.ActionForm {
03.
04.private String message;
05.
06.public HelloWorldActionForm() {
07.super();
08.}
09.
10.public String getMessage() {
11.return message;
12.}
13.
14.public void setMessage(String message) {
15.this.message = message;
16.}
17.
18.}
HelloWorldAction.java
HelloWorldAction class extends org.apache.struts.action.Action. The action class contains an
execute method which contains the business logic of the application. To access the
HelloWorldActionForm variables in the Action we need to type caste the form object to
HelloWorldActionForm. Then we can access the variables using the getter and setter
methods. The execute method returns a value of type ActionForward, based on its value the
corresponding view will be called. This configuration is done in struts-config.xml file.
01.public class HelloWorldAction extends org.apache.struts.action.Action {
02.
04.
07.
09.helloWorldForm.setMessage("Hello World!");
10.return mapping.findForward(SUCCESS);
11.
12.}
13.}
1.<action-mappings>
2.<action input="/index.jsp" name="HelloWorldActionForm"path="/HelloWorld"
3.scope="session" type="com.vaannila.HelloWorldAction">
4.<forward name="success" path="/helloWorld.jsp" />
5.</action>
6.</action-mappings>
The name "success" is mapped to the view helloWorld.jsp. So when the execute method in
the action returns "success" the request will be forwarded to the helloWold.jsp page.
helloWorld.jsp
In helloWorld.jsp we get the value of the form variable message and display it. We use
struts bean tag to do this. The name attribute of the bean tag hold the value of the action
form and the property attribute holds the value of the variable to be displayed.
01.<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
02.<html>
03.<head>
04.<title>Hello World</title>
05.</head>
06.<body>
07.<h1>
08.<bean:write name="HelloWorldActionForm" property="message" />
09.</h1>
10.</body>
11.</html>
Extract the downloaded files into the webapps folder of the Tomcat server. Start the Tomcat
server. Type the following url in the browser
"http://localhost:8080/Example1/index.jsp". There you go, you have your first struts
program up and running.
Struts 1 > Login Application Using Action Form
In this example we will see how to create a login application using ActionForm. The
following files are required for the login application.
login.jsp
success.jsp
failure.jsp
web.xml
struts-config.xml
LoginAction.java
LoginForm.java
ApplicationResource.properties
web.xml
The first page that will be called in the login application is the login.jsp page. This
configuration should be done in web.xml as shown below.
1.<welcome-file-list>
2.<welcome-file>login.jsp</welcome-file>
3.</welcome-file-list>
login.jsp
We use Struts HTML Tags to create login page. The form has one text field to get the user
name and one password field to get the password. The form also has one submit button,
which when clicked calls the login action. <html:errors /> tag is used to display the error
messages to the user.
01.<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
02.<html>
03.<head>
04.<title>Login Page</title>
05.</head>
06.<body>
07.<div style="color:red">
08.<html:errors />
09.</div>
10.<html:form action="/Login" >
12.Password :<html:password name="LoginForm" property="password" />
13.<html:submit value="Login" />
14.</html:form>
15.</body>
16.</html>
The user enters the user name and password and clicks the login button. The login action is
invoked.
struts-config.xml
The validate method in the LoginForm class is called when the Form is submitted. If any
errors are found then the control is returned back to the input page where the errors are
displayed to the user. The input page is configured in the action tag of strut-config file.
<html:errors /> tag is used to display the errors in the jsp page.
01.<struts-config>
02.<form-beans>
03.<form-bean name="LoginForm" type="com.vaannila.LoginForm"/>
04.</form-beans>
05.
06.<action-mappings>
07.<action input="/login.jsp" name="LoginForm" path="/Login"scope="session" type
="com.vaannila.LoginAction">
08.<forward name="success" path="/success.jsp" />
09.<forward name="failure" path="/failure.jsp" />
10.</action>
11.</action-mappings>
12.</struts-config>
Here the action is "/Login" , the input page is "login.jsp" and the corresponding action
class is LoginAction.java. Now the validate method in the LoginForm class will be invoked.
LoginForm.java
Inside the validate method, we check whether the user name and password is entered. If
not the corresponding error message is displayed to the user. The error messages are
configured in the ApplicationResource.properties file.
01.public ActionErrors validate(ActionMapping mapping, HttpServletRequest
request) {
04.errors.add("userName", newActionMessage("error.userName.required"));
05.}
07.errors.add("password", newActionMessage("error.password.required"));
08.}
09.return errors;
10.}
ApplicationResource.properties
The ApplicationResource.properties file contains the error messages. The key
"error.userName.required" is used in the validate function to add a new error. Since the
error messages are configured in a seperate properties file they can be changed anytime
without making any changes to the java files or the jsp pages.
1.error.userName.required = User Name is required.
If either user name or password is not entered then the corresponding error message will be
added to the ActionErrors. If any errors are found then the control is returned back to the
input jsp page, where the error messages are displayed using the <html:errors /> tag. The
validate method is used to perform the client-side validations. Once when the input data is
valid the execute method in the LoginAction class is called.
LoginAction.java
The execute method contains the business logic of the application. Here first we typecast
the ActionForm object to LoginForm, so that we can access the form variables using the
getter and setter methods. If the user name and password is same then we forward the
user to the success page else we forward to the failure page.
01.public class LoginAction extends org.apache.struts.action.Action {
02.
05.
08.if (loginForm.getUserName().equals(loginForm.getPassword())) {
09.return mapping.findForward(SUCCESS);
10.} else {
11.return mapping.findForward(FAILURE);
12.}
13.}
14.}
Lets enter the user names and password as "Eswar". Since the user name and password is
same the execute method will return an ActionForward "success". The corresponding result
associated with the name "success" will be shown to the user. This configuration is done in
struts-config.xml file.
1.<action-mappings>
2.<action input="/login.jsp" name="LoginForm" path="/Login"scope="session" type=
"com.vaannila.LoginAction">
3.<forward name="success" path="/success.jsp" />
4.<forward name="failure" path="/failure.jsp" />
5.</action>
6.</action-mappings>
If the user name and password did not match the user will be forwarded to the failure page.
Lets try entering "Joe" as the user name and "Eswar" as the password, the following page
will be displayed to the user.