Struts <html:errors> Tag

This tag displays a set of error messages prepared by a business logic component and stored as an ActionErrors object, a String, or a String array in request scope. If such a bean is not found, nothing will be rendered.


In your ActionForm, validate() method you can have some like this

errors.add("userName", new ActionMessage("error.userName.required"));


The corresponding error messages are configured in the ApplicationResouces.properties file.

error.userName.required=Enter User Name


Now in JSP, you can display error messages like this

User Name :<html:text name="ActionForm" property="userName" />  <html:errors property="userName" />

In order to use this tag successfully, you must have defined an application scope MessageResources bean under the default attribute name, with optional definitions of message keys specified in the following attributes:

  • page – Text that will be rendered before the error messages list. Typically, this message text will end with <ul> to start the error messages list (default “errors.header”).
  • footer – Text that will be rendered after the error messages list. Typically, this message text will begin with <ul> to end the error messages list (default “errors.footer”).
  • prefix – Text that will be rendered before each individual error in the list (default “errors.prefix”).
  • suffix – Text that will be rendered after each individual error in the list (default “errors.suffix”).

You can also see how we have used Struts Errors Tag in our Struts Login Example. For Reference, Click Here

,

No comments yet.

Leave a Reply