>> -----Original Message----- >> From: Laran Evans [mailto:[EMAIL PROTECTED] >> Sent: Wednesday, August 11, 2004 10:08 AM >> To: Struts User Mailing List >> Subject: <html:errors /> >> >> >> When I use <html:errors /> I get a ClassCastException. So I used >> <html:messages id="x" /> which displayed nothing, but worked. >> >> ActionErrors doesn't accept ActionError objects any longer >> (deprecated), just ActionMessage objects. >> >> What's the consensus on the correct way to display <html:errors /> >> currently? > >NOt sure about how common this is, but Husted has a cool tip: >http://husted.com/struts/tips/017.html >
I found this in the struts mailing list archives which may be helpful to you: http://marc.theaimsgroup.com/?l=struts-user&m=108757076826525&w=2 I'm also confused about the preferred method of displaying errors after the deprecation of ActionError. Armed with the knowledge from the archive I did the following: Here's the short version; code follows. (Note: I'm using Struts 1.1) In the action: I create an ActionMessages object called "errors". For each new error I add an ActionMessage to "errors" with the field as the key ("username" in the following case). I then store the ActionMessages object as an attribute called "errors" in the session. In the .jsp page: I output each error next to its respective field in the form. Each error's key is the same as its field name. ---- Say we have a form with a 'username' field. In SubmitFormAction.java : public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionMessages errors = new ActionMessages(); ... if ( isBad(username) ) { errors.add("username", new ActionMessage("error.bad_username")); } ... if ( !errors.isEmpty() ) { request.getSession().setAttribute("errors", errors); return (mapping.findForward("error")); } return (mapping.findForward("success")); } In form.jsp: <em>User Name: </em><html:text property="username"/> <html-el:messages name="errors" property="username" id="username"> <c:out value="${username}"/> </html-el:messages> <br/> The output is: User Name: <text box here> <error message here, if it exists> ---- One main difference between the method in the archive and my method is that I put the "errors" object into the session instead of the request. The reason is that all my actions redirect to their respective .jsp pages to avoid the user re-running an action simply by refreshing the page (e.g. a 'create' action). I think that anything stored in the request is lost by the time the action is redirected. I put "errors" in the session instead. One problem with this is that the "errors" object remains in the session so any subsequent visits to the page display errors. Since an error redirects the user right back to the same page, I think I may remove the "errors" object from the session as soon as the form is submitted. My questions are: 1) Do I need to put "errors" in the session so that it survives the redirection long enough to be displayed? 2) Do I need to use <c:out> inside an <html:messages> tag to output messages? Is it the only way? 3) Is it considered bad form to use ActionErrors? I don't want to build dependencies on soon-to-be-deprecated stuff. Laran: hope that helps Everyone else: hope you help me -tom