Struts has a lot of different features, and trying to introduce them all at once can confuse and scare off users. After getting frustrated reading various tutorials, I decided to sit down and figure out just how simple a Struts implementation could be while still preserving a proper MVC (model two) architecture.

The Struts HTML and Bean tagsets are wonderful, but they are by no means required to use Struts: while the tutorials and introductions do not advertise this fact, Struts works fine with conventional HTML forms and with basic JSPs. I was able to add Struts support to a simple WebApp using only three *.jar files and no *.tld files:

  1. commons-beanutils.jar
  2. commons-collections.jar
  3. commons-digester.jar
  4. commons-logging.jar
  5. struts.jar

So, simply add those to your WEB-INF/lib/ directory, and you have all the library support you'll need for now.

Next, you need to set up WEB-INF/web.xml to use Struts' ActionServlet:

 <display-name>Minimal Struts Application</display-name>
 <description>Minimal Struts-based WebApp.</description>

You also need to add one (that's right, just one) extra configuration file, WEB-INF/struts-config.xml:

    <form-bean name="nameBean" type="com.megginson.NameBean"/>
    <action path="/foo" type="com.megginson.SubmitAction" input="/index.jsp"
      name="nameBean" scope="request">
      <forward name="success" path="/hello.jsp"/>
      <forward name="failure" path="/error.jsp"/>

I created two JSPs. First, index.jsp, which contains a simple HTML form and no special JSP logic at all (it could have been named index.html):

  <title>Servlet Test</title>
  <h1>Servlet Test</h1>
  <form action="" type="GET">
   <input type="text" name="name"/>
   <input type="submit" value="Say hi!"/>

The second JSP, hello.jsp, displays the output from the servlet, and uses a bit of ugly embedded Java (yes, a tag library would have been cleaner); again, this is straight-forward, non-Struts stuff:


<jsp:useBean id="nameBean" class="com.megginson.NameBean" scope="request"></jsp:useBean>
<p>Hello, <%=nameBean.getName()%>!</p>


Finally, I added two simple classes. The first one, com.megginson.Name{{`Bean, extends the Struts Action}}`Form class, and is the bean that holds state information for the request (Struts populates it automatically from the HTML form):

package com.megginson;


import org.apache.struts.action.ActionForm;

public class NameBean
    extends ActionForm
    implements Serializable

    public NameBean ()

    public String getName ()
        return name;

    public void setName (String name)
    { = name;

    private String name;


The second class, com.megginson.Submit{{`Action, is virtually useless in its current state, but is here to show how the architecture works. It extends the Struts Action class, and is invoked by the Struts Action}}`Servlet:

package com.megginson;

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;

public class SubmitAction
    extends Action

    public ActionForward execute (ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
        throws Exception
        NameBean nameBean = (NameBean) form;
        nameBean.setName(nameBean.getName() + ", esquire");
        return mapping.findForward("success");

And that's it: a simple Struts MVC app.

  • No labels