Struts Hello World (MVC)

In this tutorial I will show you how to create a simple hello world using Struts MVC

First create a new Dynamic Web Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml


1. Action Form

Create a Action Form class and name it that contains the following code.

package com.kruders.form;
import org.apache.struts.action.ActionForm;
public class HelloWorldForm extends ActionForm{
    private static final long serialVersionUID = 1L;
    private String message;
    public String getMessage() {
        return message;
    public void setMessage(String message) {
        this.message = message;

2. Action Class (Controller)

Let us create an Action class that will handle the request.

package com.kruders.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.kruders.form.HelloWorldForm;
public class HelloWorldAction extends Action{
    public ActionForward execute(ActionMapping mapping,ActionForm form,
            HttpServletRequest request,HttpServletResponse response)
            throws Exception {
            HelloWorldForm helloWorld = (HelloWorldForm) form;
            helloWorld.setMessage("Hello World...");
            return mapping.findForward("success");

3. View

Create two JSP files, index.jsp and helloWorld.jsp with the following content in your WebContent folder.


<%@ taglib uri="" prefix="logic" %>
<logic:redirect forward="helloWorld"/>

When we run the application the index.jsp page will be executed first. In the index.jsp page we redirect the request to the helloWorld URI, which inturn invokes the HelloWorldAction.


<%@taglib uri="" prefix="bean"%>
<h1><bean:write name="helloWorldForm" property="message" />

In the action class we return the ActionForward “success” which is mapped to the helloWorld.jsp page. In the helloWorld.jsp page we display the “Hello World…” message via Struts tag library.

4. Struts Config

Create a struts-config.xml file in WEB-INF folder.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
       <form-bean name="helloWorldForm"
        <forward name="helloWorld" path="/"/>
       <action path="/helloWorld"
        <forward name="success" path="/helloWorld.jsp"/>

5. Deployment Descriptor

Now configure the deployment descriptor. Here, we have asked the container to give our ActionServlet any request that matches the pattern *.do

Add the following configuration information in the web.xml file


6. Run Project

Following screen will be displayed as shown in Figure 1.1

Figure 1.1 Figure 1.1

The folder structure of the example is shown below in Figure 1.2

Figure 1.2 Figure 1.2

You can download the source code of this example here.

No comments yet.

Leave a Reply