Struts and messages/errors in jsp

When you would like to output, with struts, on a jsp something that sum up some errors, you could use the <html:errors /> tag. However this has not a good customization for layout.

If you would like to give a “list looks”, you could enter in your message resources file something like

errors.header=<ul>
errors.footer=</ul>
errors.prefix=<li>
errors.suffix=</li>

This with the <html:errors /> would output something like

<ul>
   <li>error  1</li>
   <li>...</li>
   <li>error n</li>
</ul>

but what if you would also like to output the error message just next the field as well as the summing up area? You will end by having an <ul> for each error next to the field. Not very good to see.

Let’s say we have an action with code like

//user and password are mandatory
if(frm.getUser().length()<=0){
   errors.add("user",new ActionError("error.mandatory",mr.getMessage("label.user")));
   logger.error("User is a madatory field");
}
if(frm.getPassword().length()<=0){
   errors.add("password",new ActionError("error.mandatory",mr.getMessage("label.password")));
   logger.error("Password is a madatory field");
}

and the jsp like

<html:form action="tsheet.do">
   <fieldset>
      <legend><bean:message key="tsheet.label.login.filedset" /></legend>
      <html:hidden property="d" value="signin" />
      <p>
         <label for="user"><bean:message key="label.user" />:</label>
         <html:text property="user" />
         <html:errors property="user" />
      </p>
      <p>
         <label for="password"><bean:message key="label.password" />:</label>
         <html:password property="password" />
         <html:errors property="password" />
      </p>
      <p>
         <html:submit><bean:message key="label.login" /></html:submit>
      </p>
   </fieldset>
</html:form>

in order to have a summing up div for errors and messages and beside them the error next each field, in the jsp you could use this code

<body>
   <h1><bean:message key="tsheet.label.login.title" /></h1>
   
   <logic:messagesPresent message="true">
      <ul id="messages">
         <html:messages id="msg" message="true">
            <li><bean:write name="msg" /></li>
         </html:messages>
      </ul>
   </logic:messagesPresent>
   
   <logic:messagesPresent>
      <ul id="errors">
         <html:messages id="msg">
            <li><bean:write name="msg" /></li>
         </html:messages>
      </ul>
   </logic:messagesPresent>
   
   <html:form action="tsheet.do">
      <fieldset>
         <legend><bean:message key="tsheet.label.login.filedset" /></legend>
         <html:hidden property="d" value="signin" />
         <p>
            <label for="user"><bean:message key="label.user" />:</label>
            <html:text property="user" />
            <html:errors property="user" />
         </p>
         <p>
            <label for="password"><bean:message key="label.password" />:</label>
            <html:password property="password" />
            <html:errors property="password" />
         </p>
         <p>
            <html:submit><bean:message key="label.login" /></html:submit>
         </p>
      </fieldset>
   </html:form>
   </body>

and avoid the use of errors.header and so on.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s