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.
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.
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.
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
}
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
}
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.
public void validate()
{
if (name == null || name.trim().isEmpty())
{
addFieldError("name", "Name is required")
}
}
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.
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.
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.