Archive for the 'Apache Struts' Category

Struts Validator’s Mask Validation does not work!

Saturday, December 9th, 2006

Mask validation is a good way of validating fields with regular expressions. When you’re using mask validation in struts validator, you may get this error at runtime -

java.lang.NoClassDefFoundError: org/apache/oro/text/perl/Perl5Util
at org.apache.commons.validator.GenericValidator. matchRegexp(GenericValidator.java:68)
at org.apache.struts.validator.FieldChecks. validateMask(FieldChecks.java:238)

Reason is the validator is not able to find a class org/apache/oro/text/perl/Perl5Util You’ll need to include the jakarta-oro project in your web-inf/lib folder. Download the jakarta-oro jar from jakarta project site ->http://apache.osuosl.org/jakarta/oro/binaries/

Validating Multi Page Forms With Struts Validator plugin

Saturday, December 9th, 2006

The field element (in validation.xml) has an optional page attribute. It can be set to an integer. All validation for any field on a page less than or equal to the current page is performed server side. All validation for any field on a page equal to the current page is generated for the client side Javascript. A mutli-part form expects the page attribute to be set. To do this -

  1. Include int property – page in your ActionForm.
  2. In your JSP, give a value to page property - <html:hidden property=”page” value=”2″/>
  3. In your validations.xml, for the field tag, specify the page attribute.

<field property=”field1″ depends=”required” page =â€?1â€?>
<msg name=”required” key=”field1.required” />
</field>
<field property=”field2″ depends=”required” page =â€?2â€?>
<msg name=”required” key=”field2.required” />
</field>
<field property=”field3″ depends=”required” page =â€?3â€?>
<msg name=”required” key=”field3.required” />
</field>

Here, since we’ve given value of page property as 2 in our JSP, only field2 & field3 would be validated. Reference - http://struts.apache.org/1.x/faqs/validator.html

Use EL language with Struts Tag library

Saturday, December 9th, 2006

While using struts’ html tags I noticed that I couldn’t use the EL expressions with them. EL - Expression language that lets you access bean values using simple dot operators, instead of having to use scriptlets <%…%>. We normally give EL expressions enclosed with ${…}. So for e.g. if you want to access strName property of Customer bean which is placed in session, then EL expression - ${sessionScope.mycustomer.strName} would retrive the value of Customer bean’s strName property. This will work only when you follow java bean specification, i,e, the class should be serializable, must have getXXX(), setXXX(), isXXX() methods.

Sorry for digressing, but I thought it would be better to let the newbies know what is EL tags. Ok, so I tried to use EL expression with the html:multibox tag (I’d taken a iteration, didn;t show here. I wanted to generate unique ids for each checkbox generated.) -

<html:multibox styeId=�${count} />

…the expression ${count} didn’t evaluate. <input type=”checkbox” id=”${count}”>

Ok…so solution is….to be able to use the EL language with struts tag library, I’ve to use the struts EL tag library. So replace below -

<%@ taglib uri=”/WEB-INF/taglib/struts-tiles.tld” prefix=”tiles” %>
<%@ taglib uri=”/WEB-INF/taglib/struts-html.tld” prefix=”html” %>
<%@ taglib uri=”/WEB-INF/taglib/struts-bean.tld” prefix=”bean” %>

With

<%@ taglib uri=”/WEB-INF/taglib/struts-tiles-el.tld” prefix=”tiles” %>
<%@ taglib uri=”/WEB-INF/taglib/struts-html-el.tld” prefix=”html” %>
<%@ taglib uri=”/WEB-INF/taglib/struts-bean-el.tld” prefix=”bean” %>

…and you should be able to use the EL language, provided you have the struts-el.jar jar in WEB-INF/lib folder of your web app.

To understand the struts-el tag library, follow the link - http://struts.apache.org/1.2.x/userGuide/building_view.html#struts-el
I’m borrowing this from the above link -

” Although the Struts-EL tag library is a direct “port” of the tags from the Struts tag library, not all of the tags in the Struts tag library were implemented in the Struts-EL tag library. This was the case if it was clear that the functionality of a particular Struts tag could be entirely fulfilled by a tag in the JSTL. It is assumed that developers will want to use the Struts-EL tag library along with the JSTL, so it is reasonable to assume that they will use tags from the JSTL if they fill their needs.”

So you need to be aware that not all struts tags would be available when you use the struts-el tag library. In short, whenever you can use JSTL tags, use it. Most probably you’ll only ever need to use the struts html tag library.

Very Good UI Technique

Friday, December 8th, 2006

We normally use checkboxes for making multiple choices. A typical form is checkbox and then label. For e.g.

Without Label

You can use HTML Label tag to associate a label with checkbox, so that when you click on the label, the checkbox gets checked. Here’s the code to do this…

<input id=”chkName” value=”b” type=”checkbox”><label for=”chkName”>With a label</label>

Struts Validator IllegalArgumentException

Friday, December 8th, 2006

I tried to use the Apache Validator. Made entries in validation.xml and got following errors -

java.lang.IllegalArgumentException: Resources cannot be null.

This happens because I’d not defined the the validator plugin in my struts-config.xml file. Here’s how to include it -

<plug-in className=”org.apache.struts.validator.ValidatorPlugIn”>
<set-property property=”pathnames” value=”/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml” />
</plug-in>

Also define the message property files where all the error messages will be kept.

<!– Default Application resource file –>
<message-resources null=”false” parameter=”Messages” />

Apache Struts Resources

Saturday, November 11th, 2006

I’m now working on a development project for designing web application for MNC company. Technology - we decided to use apache struts which provides a very good implementation on MVC2 framework. I’ve created a page where I’ll share any interesting and helpful articles, tutorials on Struts. So zoom off to Struts resources page.