Struts Form Handling and Annotation Action Mappings Tutorial |Video upload date:  · Duration: PT13M0S  · Language: EN

Learn Struts form binding validation and annotation based action mappings for Java web apps with clear steps and practical tips

Why annotations and not a giant XML graveyard

If you like jumping between files and arguing with your IDE then keep using XML. If you prefer keeping wiring next to the code that does the work then annotations are your friend. Struts2 annotations place action mappings near the action class and make form handling and validation less like a scavenger hunt.

Quick project setup without the drama

Use Maven or Gradle to pull in Struts core and commons dependencies. Let the build tool handle versions and the classpath so you can waste your brain cells on debugging real problems later. A typical setup just needs the Struts2 core and the validation support on the classpath.

Create a form bean and action class

Keep form fields as plain Java properties with getters and setters. Struts2 will populate action properties that match form input names automatically when the form posts. That means no reflection rituals from the dark ages.

Action class skeleton

public class SaveAction extends ActionSupport
{
    private String name

    public String getName() { return name }
    public void setName(String name) { this.name = name }

    // mapping and results will go here
}

Add annotation based action mappings

Drop the XML mapping and add annotations on the action class or its methods. The mappings stay close to the logic which reduces context switching and the urge to scream. Basic examples look like the snippets below.

@Action(value = "save", results = {
    @Result(name = "success", location = "/success.jsp"),
    @Result(name = "input", location = "/form.jsp")
})
public String execute()
{
    return SUCCESS
}

Handle submission and validation like a human

You can validate with annotations from the validation framework or by overriding the validate method on the action. Both bind errors back to the form so the user does not have to guess what went wrong.

In action validate method

public void validate()
{
    if (name == null || name.trim().isEmpty())
    {
        addFieldError("name", "Name is required")
    }
}

Define result views and keep paths sensible

Map logical names such as success and input to JSPs or other view templates using the annotation results. Keep view paths consistent and avoid hard coded references littered across classes. Logical names are your friend for testing and refactoring.

Test the flow and fix the weird cases

Try the happy path and the broken path with validation errors. If you land on a page you did not expect check parameter names, action mapping values and result locations. Turn on logging to trace dispatches when the app behaves like it has a grudge against you.

Checklist to avoid common traps

  • Confirm form input names match action property names
  • Verify annotation value matches the URL or action name you post to
  • Use success and input as logical result names for predictable flow
  • Keep validation messages tied to fields for better UX
  • Run both happy path and validation error scenarios when testing

Parting advice that is actually useful

Annotations tidy configuration and speed up development. Validation can live on the action or as annotations depending on your taste for centralized rules versus local convenience. Either way the core steps stay the same bind form fields to action properties map results to views and validate input before committing side effects.

Follow the steps above and you will have a Struts2 form handling flow that behaves like a professional instead of a mystery puzzle. If that fails at least you will have better logs and sharper sarcasm to share with your team.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.