Java Struts 1.1: Master of the ActionServlet and Form Beans
It's 2003, and the "Spaghetti Code" of JSP files filled with JDBC calls is finally being replaced by a more structured approach. The Apache Struts framework has become the industry standard for J2EE web development. By separating our logic into Actions, our data into ActionForms, and our configuration into XML, we can finally build maintainable enterprise applications.
The struts-config.xml
In Struts 1.1, the XML configuration file is the brain of your application. It maps incoming request paths to specific Java classes.
<struts-config>
<form-beans>
<form-bean name="loginForm" type="com.example.forms.LoginForm" />
</form-beans>
<action-mappings>
<action path="/login"
type="com.example.actions.LoginAction"
name="loginForm"
scope="request"
validate="true"
input="/login.jsp">
<forward name="success" path="/welcome.jsp" />
<forward name="failure" path="/login.jsp" />
</action>
</action-mappings>
</struts-config>
The ActionForm: Capturing User Input
The ActionForm is a simple JavaBean that Struts automatically populates with data from the HTTP request. It’s also where we perform basic validation.
public class LoginForm extends ActionForm {
private String username;
private String password;
public void setUsername(String u) { this.username = u; }
public String getUsername() { return username; }
// validation logic
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (username == null || username.length() < 1) {
errors.add("username", new ActionMessage("error.username.required"));
}
return errors;
}
}
The Action: The Controller Logic
When the user submits the form, the ActionServlet creates the LoginForm, validates it, and then passes it to your Action class.
public class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;
String username = loginForm.getUsername();
// Business logic would go here (e.g., calling an EJB or DAO)
if ("admin".equals(username)) {
return mapping.findForward("success");
} else {
return mapping.findForward("failure");
}
}
}
The Power of Tags
In 2003, we don't write <input type="text"> by hand. We use the Struts Tag Library. This ensures that our JSP pages are tightly coupled to our ActionForms, allowing the framework to automatically re-populate fields if validation fails. It’s a complete ecosystem that, while XML-heavy, brings much-needed order to the chaos of early Java web development.
Aunimeda develops websites and web applications for businesses - corporate sites, e-commerce, portals, and custom platforms.
Contact us to discuss your web project. See also: Web Development, E-commerce Development