error message on jsp
Am trying to learn struts...not easy.
Anyway, I want to display an error message on a page if the user forgets to enter a field. I figured out how to do that with the validate() method in the ActionForm. OK, but now I want to make the error show up if they did something the database does not like. So I am off into the Action now.
Lets say they want to order some widgets, and there were some in the inventory when the screen came up, but they looked at it a while, they went and had a cup of coffee, and when they got back and clicked the "order" button the application goes out to the database and lo and behold, there are no more widgets left. So how do I deal with this and show that nice little error message on the same screen. "sorry, the last widget has been sold".
Here is how I normally handle something like this:
My WidgetManager.getWidgets() throws a NoWidgetsFoundException. That is caught in the Action and then you create a new ActionMessage. The message that is going to be displayed is still in the ApplicationResource.properties file, just like the errors you collect in the Form.validate() method.
public ActionForward getWidgets(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception
ActionMessages errors = new ActionMessages();
ActionForward forward = new ActionForward();
forward = mapping.findForward("gotthem");
catch (NoWidgetsFoundException e)
errors.add("noWidgetsLeftError", new ActionMessage("error.noWidgetsLeftError"));