Frustrated with struts validation

G

Greg Williams

Has anyone noticed this? When declaring a form bean, I declare a
property (pluNumber) as type Long (java.lang.Long). Then I edit
validation.xml (could be called something else) and add the following:

<form name="thisForm">
<field
property="pluNumber"
depends="required">
<arg0 key="setup.thisForm.pluNumber.displayName"/>
</field>
</form>


So, provided I declare this form to be validated in my action
declarations, struts should return an error message if nothing is
entered into the input box for this property. However, what really
happens is that, since it is a Long, struts instantiates this class
witha value of 0. So the "required" check passes and no error message
is created. In fact, i could put "required,integer" and enter a string
"cncnvbeueb" into the input and this would pass because Long can be
intantiated by passing a string. I've had trouble making this field a
primitive long, not sure if you can?. i dont really want to anyway
because when i copy the properties
(org.apache.commons.beanutils.BeanUtils.copyProperties) I want the
property to be the same type as in the model object (or DTO) before
working with it. Am I missing something?

greg
 
G

Greg Williams

This is great, thanks. In terms of a URL, I'd love to be directed to
somewhere that would demonstrate what Wendy S mentioned about
registering a converter:
Then when you BeanUtils.copyProperties() to your DTO, if the type
conversion
doesn't happen automatically (many of them do) you can register a
Converter
to tell it how you want the conversion done.

I wont be working with these values in my Action class. All the
validation should be done by struts and then ill just pass them on to
the ejb container. If i can avoid parsing longs each time in my action
class, and have it be done automatically, that seems like it would be
the best. Seems like this type of thing would have been written?

thanks to both for your input. very helpful.
-greg
 
W

Wendy S

Greg Williams said:
This is great, thanks. In terms of a URL, I'd love to be directed to
somewhere that would demonstrate what Wendy S mentioned about
registering a converter:

BeanUtils is part of the Jakarta Commons project:
http://jakarta.apache.org/commons/beanutils.html

You write a Converter class by implementing the interface:
http://jakarta.apache.org/commons/beanutils/api/org/apache/commons/beanutils/Converter.html

Then you register it by calling ConvertUtils.register( Converter c)
http://jakarta.apache.org/commons/beanutils/api/org/apache/commons/beanutils/ConvertUtils.html

However, I don't think you'll have to do this as String-to-Long is one of
the default converters.

If you use Strings for your Form bean attributes, validate them, then do
BeanUtils.copyProperties() to move the values over to your DTO, you
shouldn't have any problems. Validation will catch it if the user has put
in something that can't be converted to a long, and BeanUtils will do the
conversion for you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top