|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 5
The open source Jakarta PerlUtil class makes working with regular expressions deceptively simple. The static method PerlUtil.match(pattern, string) does the trick.
By introducing attributes for a pattern and an error message at the appropriate point in the inheritance hierarchy, you endow all field tags with those attributes at once. When you are done, you can write something like this:
<struts:text
property="account"
pattern="/^[0-9]{7,9}$/"
errorMessage="requires 7 to 9 digits"/>
Somewhere in the vicinity, often right next to the field, you can place a JSP expression to show an eventual error:
<%= bean.getError("account") %>;
To nail down the functionality of validating page beans, let's write the small interface shown in Listing 4.
Listing 4. Validator.java: The Validator interface
package com.bank;
public interface Validator {
public void setPattern(String field, String pattern);
public void setErrorMessage(String field, String message);
public String getError(String field);
public boolean validate(javax.servlet.http.HttpServletRequest request);
}
When everything is in place the setPattern and setErrorMessage methods will be called from Struts when tags using the pattern and errorMessage attributes are processed at page construction time. Those methods should simply store the association between the form field
names on one hand and the patterns and the error messages on the other. The validate method will be called from the controller code after the form is submitted, and it should match the field values of the incoming
request with the patterns stored earlier. If the match fails, the associated error messages should be tied to the field. The
getError will be called from the JSP, and it should return that error message or an empty string if the match succeeded. Finally,
the setErrorMessage method allows you to use the same mechanism to display other error messages on the page. All that is required is a call to
setErrorMessage(name, message) function in the code in which the error is discovered.
Listing 5 shows the change to the Struts BaseFieldTag class that enables it to work with automatic validation. Apart from properties for the new attributes, you implant a call
to give the attributes and their values to the page bean just before the page is constructed in the doStartTag method. Added code is shown in bold font.
Listing 5. BaseFieldTag.java: Implanting validator functionality in Struts
private String pattern;
public String getPattern() {
return pattern;
}
public void setPattern(String pattern) {
this.pattern = pattern;
}
private String errorMessage = null;
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
...
public int doStartTag() throws JspException { Object pagebean = pageContext.findAttribute(name);
if(pagebean instanceof com.bank.Validator) {
((com.bank.Validator) pagebean).setPattern(property, pattern);
((com.bank.Validator) pagebean).setErrorMessage(property, errorMessage);
}
...
The PageBean shown in Listing 6 implements both the interfaces and can serve as a base class for other PageBeans. In some cases, it may even prove sufficient by itself.