This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes Struts 2 + validate int value. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts 2 + validate int value." Watch "Struts 2 + validate int value." New topic
Author

Struts 2 + validate int value.

Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 684


Hello All,

I am using Struts 2 and validate int value using code for this


<field name="maximumTitleSize">
<field-validator type="conversion" short-circuit="true">
<message>
Conversion Error (Integer Wanted) for Maximum Title Size
In Bytes.
</message>
</field-validator>
<field-validator type="int">
<param name="min">0</param>
<param name="max">2147483647</param>
<message>
Positive Integer value required for Maximum Title Size
In Bytes. Maximum value allowed is ${max}.
</message>
</field-validator>
</field>

as int max size is 2147483647 but if i insert 2147483647222 then it shows negative (-ve) value.

please let me know if any one know solution for this.



Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

If a value is larger than Integer.MAX_VALUE, how is Struts supposed to cram it into an int so you can validate it?

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 684


Hi,

yes you are right how it cram in to int but what i mean to say it shows error message and pass validation but when it return back after fail validate it shows negative value. so how to make struts not show negative value.



Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

I'm not sure you see what's happening. When the form is submitted, Struts takes the request parameter for this field and turns it into an int. Since the number is too big to fit in the variable, it gets "wrapped" around and becomes negative. I think if you don't want to see a negative number, you'll have to validate before the form is submitted.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You'd probably need to write an overflow-aware type converter.
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 684


Thanks for your reply,

But i have put my code please let me know how could i resolve my problem ..???

give solution for that ...... :roll:
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

David and I have proposed two different ways to approach your problem. Now it is up to you to ShowSomeEffort
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 684



Yes but i am not getting what you want to say technically.

please explain how to make it and give some reference for it and code.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

There are a number of references on the internet about how Java handles (or doesn't) integer overflow.

The S2 documentation wiki gives the skeleton of a type converter; the XWork source contains many examples. A combination of those two sources should point you in the right direction.
 
 
subject: Struts 2 + validate int value.
 
Similar Threads
Struts 2 Validation
Pop up error while using validation in struts2.0
Action class not being called after validation
Struts2 very simple validation doesn't work
validation for fck:editor