Struts 1 > Hello World Application

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


web.xml is used to configure the servlet container properties of the hello world appliation.


The gateway for our hello world application is index.jsp file. The index.jsp file should be mentioned in web.xml as shown above.


In the hello world example the index.jsp page simply forwards the request to the hello world action.

<jsp:forward page=""/>


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.


        <form-bean name="HelloWorldActionForm"

        <action input="/index.jsp" name="HelloWorldActionForm" path="/HelloWorld" scope="session" type="com.w3javaguide.HelloWorldAction">
        	<forward name="success" path="/helloWorld.jsp" />


HelloWorldActionForm extends org.apache.struts.action.ActionForm. HelloWorldActionForm class has one String variable message and the corresponding getter and setter methods.

public class HelloWorldActionForm extends
           org.apache.struts.action.ActionForm {

    private String message;

    public HelloWorldActionForm() {

    public String getMessage() {
        return message;

    public void setMessage(String message) {
        this.message = message;


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.

public class HelloWorldAction extends org.apache.struts.action.Action {

    private final static String SUCCESS = "success";

    public ActionForward execute(ActionMapping mapping,ActionForm form,
           HttpServletRequest request,HttpServletResponse response) throws Exception {

        HelloWorldActionForm helloWorldForm = (HelloWorldActionForm) form;
        helloWorldForm.setMessage("Hello World!");
        return mapping.findForward(SUCCESS);

    <action input="/index.jsp" name="HelloWorldActionForm" path="/HelloWorld"
    scope="session" type="com.w3javaguide.HelloWorldAction">
        <forward name="success" path="/helloWorld.jsp" />

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.


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.

<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<title>Hello World</title>
<bean:write name="HelloWorldActionForm" property="message" />

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.

You can download the source code of the Hello World example by clicking on the Download link below.

Source :Download
Source + Lib :Download
Contact Us  |  Copyright © 2012