Display error messages when an action returns them to the input
JavaServer Pages (JSP) page.
About this task
To use ActionErrors, you must add a Struts HTML tag to display an
error message on the input JSP page. You must also add an input field to the
action mapping.
To modify the application to return any bean validation
errors to the input JSP page:
In the validate method of the DateData class,
verify that if the input year is less than 1582, the method will return an
error with the "pre-gregorian" message.
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (year < 1582)
{
errors.add("year", new org.apache.struts.action.ActionError("pre_gregorian"));
}
Locate the "pre-gregorian" error message in the ApplicationResources.properties
file, and verify that the message matches the following definition:
pre-gregorian=<li>Date is before 1582, the year the Gregorian calendar began</li>
To specify the target JSP page for the error message, select an
input for the action that uses the form bean:
Open your Web diagram.
In the diagram, hold the mouse pointer over the /computeDay
node and drag the node connection handle to the input.jsp node.
In the pop-up menu, select Action Input.
Your Web diagram should resemble the following image:
Update the input JSP page to display the error message:
Double-click the input.jsp page to open it in Page Designer.
From the Struts HTML Tags drawer in the palette, drag an Errors
tag to the location in the page where you want the error message to be displayed.
Your input JSP page should resemble the following image:
Save and close your input JSP page and your Web diagram.
Results
Lesson checkpoint
In this lesson, you created the display for an error message in
the input JSP page.
You learned to do these tasks:
Add an HTML tag to display an error message in the input JSP page.
Modify an action mapping to include an input field.