Form handling in Apache Struts follows an explicit, traceable path: a JavaBean holds the data, an action class processes it, and JSP pages render the results. No magic, no hidden state — just a predictable flow you can follow through the configuration files.
This piece walks through the actual steps without treating Struts as a mystery.
At a glance
- Form data lives in a JavaBean mapped directly to HTML form fields
- The action class receives the populated bean and handles processing logic
- The struts-config.xml file wires the form, the action, and the view together
- JSP pages use Struts tags to bind form elements to bean properties
- Testing requires deploying to a servlet container and submitting the form
Setting up the development environment
Get the Struts libraries into your project. You need the core Struts JARs and a servlet container. Create a new Struts project using the framework's project template as a starting point. That gives you the basic directory structure and configuration files without guessing at the layout.
Creating the JavaBean
The form data maps to a plain JavaBean with getter and setter methods for each field. Suppose your form collects a name, an email address, and a phone number. Your bean has three private fields with corresponding getName, setName, getEmail, setEmail, getPhone, and setPhone methods.
Keep the bean simple. Struts populates it via reflection, so the property names in the bean must match the field names in your HTML form.
Building the JSP form
Your JSP page uses Struts tags to render the form. The html:form tag wraps everything and points to the action path defined in your configuration. Inside the form, html:text binds each input to a bean property:
“`
<html:form action="/submitForm">
Name: <html:text property="name" /><br>
Email: <html:text property="email" /><br>
Phone: <html:text property="phone" /><br>
<html:submit />
</html:form>
“`
Struts populates the fields with values from the bean on the way back to the browser.
Writing the action class
The action class extends Struts's Action and overrides the execute method. The method receives the form bean and the request and response objects. Inside execute, you retrieve the form field values, run whatever processing is needed, and attach output data to the request or session before returning a forward.
“`
Public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
MyFormBean bean = (MyFormBean) form;
String name = bean.getName();
// process the data, set output attributes
Return mapping.findForward("success");
}
“`
Wiring it all together
The struts-config.xml file is where everything connects. You declare the form bean, the action class, and the forward mappings in one place:
“`
<form-bean name="myForm" type="com.example.MyFormBean" />
<action path="/submitForm" type="com.example.SubmitAction"
Name="myForm" input="/form.jsp">
<forward name="success" path="/result.jsp" />
</action>
“`
When the form submits, Struts instantiates the bean, populates it, passes it to the action, and follows the forward that the action returns.
Testing the flow
Deploy the application to your servlet container, open the form page in a browser, fill in the fields, and submit. Watch the server logs for exceptions. Common problems include mismatched property names between the bean and the form, a missing forward definition, or a bean that hasn't been declared in struts-config.xml.
Bottom line
Struts form handling is verbose but transparent. Every piece has a clear location: the bean in one class, the logic in another, the configuration in XML. That verbosity is an advantage when you need to trace a bug — you can follow the path without guessing.
Who should care
- Legacy maintainers**: If you're working on an existing Struts application, this workflow shows how to extend form handling without disrupting the existing architecture.
- New developers joining a Struts team**: The explicit configuration makes it easier to understand how data flows through the application.
- Architects evaluating migration**: Understanding Struts form handling clarifies why rewriting can be costly — the tight coupling between forms, actions, and configuration is deeply embedded.
Key takeaways
- Form data lives in a JavaBean; property names must match HTML form field names
- The struts-config.xml wires the bean, action, and view together
- Keep beans simple; Struts handles the population via reflection
- Test the form flow end to end in a servlet container before assuming the configuration is correct
Frequently asked questions
What is the role of the JavaBean in Struts form handling?
The JavaBean holds the form field values. Struts populates it automatically when the form is submitted and passes it to the action class.
Can I use standard HTML form elements instead of Struts tags?
You can, but Struts tags handle bean binding automatically. Standard HTML requires manual data retrieval from the request object.
What happens if a form property doesn't have a setter in the bean?
Struts cannot populate the field, and the value will be empty or default. Make sure every form field has a corresponding setter.
How do I map the form submit to an action in the configuration?
In struts-config.xml, declare the form-bean and action elements. The action path must match the action attribute in your html:form tag.
What should the action class return?
The action class returns an ActionForward object that points to a JSP results page, determined by the forward name configured in struts-config.xml.