Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Generate Web Output in Multiple Formats and Languages with StrutsCX : Page 4

StrutsCX overcomes the limitations of the Struts Framework by enabling you to utilize XML, XSLT, and XPath technologies instead of its standard JavaServer Pages.

Error Handling with StrutsCX
StrutsCX controls the validation of input errors exclusively inside the Action class. The settings in the ActionMapping conform more or less with the ones in Struts. Inside the ActionForm object, nothing really changes. If errors occur, the validate() method returns an ActionErrors container filled with ActionError objects. However, the method add() of the ActionErrors object does need a Struts ActionMessage object (see Figure 8). Although StrutsCX does not use this object, it is added:

// code-snipped of Struts FormBean: public ActionErrors validate( ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if (title == null || title.length() < 1) { errors.add("title", // name of input field new ActionError("anything")); // anything } return errors; }

Figure 8: The Examination of the ActionErrors Container

By calling the validate() method of the ActionForm object, the Action class gets hold of the ActionErrors container with its ActionError objects:

// code-snipped of Struts Action: ActionErrors errors = actionForm.validate(mapping, request);

If it's filled, the ActionErrors container will be added to the HttpServletRequest and forwarded to the ERROR target defined inside the struts-config.xml. If the ActionErrors container is empty, the HttpServletRequest gets forwarded to the SUCCESS target defined inside the struts-config.xml:

// Action code continued: if (!errors.empty()) { request.setAttribute("errors", errors); forward = "ERROR"; } else { forward = "SUCCESS"; }

Of course, additional ActionError objects can be added to the ActionErrors container inside the Action classes. There is no restriction to the validation of user input. During XSLT transformation, all these error objects become incorporated into the XML-Output-Document so they can be used by the XSL Stylesheet. Here's an extract from of an XML-Output-Document:

<root> ... <variables> ... <!-- error message strings --> <errors> <title>Please insert a title!</title> ... </errors> ... </variables> ... <!-- error flags --> <errors> <title/> ... </errors> ... </root>

Inside the XSL-Stylesheet this information can be used to compose an appropriate error message:

<!-- show error message if error tag present --> <xsl:if test="/root/errors/title = node()"> <div class="error"> <xsl:value-of select="/root/variables/.../errors/title"/> </div> </xsl:if> <textarea name="title" rows="2" cols="30" style="width=250px"> <xsl:value-of select="title"/> </textarea>

Coding the XSL Stylesheet is very similar to using a JSP Tag: the goal is to show the error message just in case the xsl:if statement is true, which means an error Element exists inside the XML-Output-Document (see Figure 9). An XPath Node Test does the validation. The text of the error message is part of the XML-Output-Document too, and this text automatically gets serialized into it with content from an appropriate StrutsCX-Resources-Properties file.

Figure 9: Multi-language Error Messages with StrutsCX

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.