I'm trying to make a login page in Struts. The idea is to validate if the user exists, etc, and then if there is an error, return to the login page with the errors in red (the typical login or any form page validation).
I would like to know if someone knows an errors management tutorial in Struts. I'm looking specially for a tutorial (or example) of the
<html:errors>
tag, which I think would solve my problem.
Here's one: //struts.apache.org/1.3.5/struts-taglib/apidocs/org/apache/struts/taglib/html/package-summary.html#package_description
Here I'm assuming Struts 1. I don't know if it has changed for Struts 2.
You can put an errors.header and errors.footer into your message resources file:
The header and footer are displayed only if the ActionErrors object has any errors in it.
In your Action class, do this:
Then before returning:
In your messages resource file:
Now when the
<html:errors/>
tag is processed, it will turn into:Here's a quick summary. You have an
ActionForm
class, sayMyForm
:You have an
Action
class, sayMyAction
:"name" in the action refers to "name" in the form-bean. Because you have
validate="true"
yourActionForm
classMyForm
must definevalidate()
method which will automatically be called:If it returns an empty ActionErrors object, Struts goes on to call your MyAction.execute(). Otherwise, Struts displays /insert.jsp (because that's the input= parm you gave) and expands the html.errors tag to display your errors from ActionErrors.