Lesson 8: Display error messages
Use this lesson to learn how to display error messages when an action returns them to the input JSP page.
To use ActionErrors, 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.ActionMessage("pre_gregorian")); }
- Locate the
"pre-gregorian" error message in ApplicationResources.properties, 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:
Your Web diagram should resemble the following image:
- 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.
- Update the input JSP page to display the error message:
Your input JSP page should resemble the following image:
- Double-click the input.jsp page to open it in the editor.
- 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.
- Save and close your input JSP page and your Web diagram.
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.