aspose file tools
The moose likes Struts and the fly likes Problems with Struts 2 number validation Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Reply Bookmark "Problems with Struts 2 number validation" Watch "Problems with Struts 2 number validation" New topic
Author

Problems with Struts 2 number validation

Jack Dragano
Greenhorn

Joined: Dec 02, 2010
Posts: 5
Hi all!
I'm developing a simple e-commerce web project for school, and today I had some troubles using validation framework built-in with Struts 2.
As discussed in this thread, it seems that Struts 2 simply ignore the fact that fields that should contain numbers, are empty. In other words, an empty string is considered to be a valid number.

I would like to solve this problem using XML only, is it possible? I know I could check fields within the Java code, but I think it's not the best solution.
I tried to use some validators (regex, required, requiredstring) before the <field-validator type="long">, but none of them worked. I suppose the reason's that all non-number validators expect a String from the getter, but they receives a long.

Any idea?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8265

Jack Dragano wrote: I know I could check fields within the Java code, but I think it's not the best solution.


Frankly, the "best solution" is the one that works. It looks like the XML validator doesn't do what you want it to do.
Since it looks like a few people have encountered this problem, you may want to check the Struts mailing list to see if anyone has an acceptable workaround. If you find one, be sure to check back in with us!


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Jack Dragano
Greenhorn

Joined: Dec 02, 2010
Posts: 5
On an empty string Tomcat throws a silent exception, because it cannot find the method that takes a string.

I thought to change getter and setter to:


and then evaluate it with both requiredstring and regex, to be sure that it's a number. Although not much elegant, it should work.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Problems with Struts 2 number validation
 
Similar Threads
Validation Struts2
How to disable bean validation on some fields in JSF 2 page?
Struts2: use EmailValidator in my validate() method?
Form Validation using xml in struts2
Multiple field validation in Struts2 Validation using validation.xml file