Meyyappan has posted 101 posts at DZone. View Full User Profile

Struts Hello World Tutorial

06.14.2012
| 71859 views |
  • submit to reddit
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.

<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.

index.jsp

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

<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.

<struts-config>

    <form-beans>
        <form-bean name="HelloWorldActionForm"
              type="com.vaannila.HelloWorldActionForm"/>
    </form-beans>

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

</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.

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

    private String message;

    public HelloWorldActionForm() {
        super();
    }

    public String getMessage() {
        return message;
    }

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

}

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.

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-mappings>
    <action input="/index.jsp" name="HelloWorldActionForm" path="/HelloWorld"
    scope="session" type="com.vaannila.HelloWorldAction">
        <forward name="success" path="/helloWorld.jsp" />
    </action>
</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.

<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>
<bean:write name="HelloWorldActionForm" property="message" />
</h1>
</body>
</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.



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

Source :Download
Source + Lib :Download
Published at DZone with permission of its author, Meyyappan Muthuraman.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

victor marquez ... replied on Mon, 2013/05/13 - 11:54pm

Where can I find documentation of how tdl files?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.