This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Struts and the fly likes Struts2 validation localization problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Frameworks » Struts
Reply Bookmark "Struts2 validation localization problem" Watch "Struts2 validation localization problem" New topic
Author

Struts2 validation localization problem

Raymond Holguin
Ranch Hand

Joined: Aug 11, 2009
Posts: 47
Ok i have a problem with localization and the error output.

lets say i have a message defined like this

and a validator like this

if this fails Im going to get an error message like

"requestDate is required"

but what I want is

"Request Date is required".

in struts1 using the commons validator I was able to define a parameter which would be used for display purposes on error messages...how can i achieve that in struts2 validation framework??
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

All OGNL is available in resource strings, allowing all sorts of madness. If I remember I'll cut-and-paste an example from the book where I cover this.
Raymond Holguin
Ranch Hand

Joined: Aug 11, 2009
Posts: 47
thanks david, unless you can suggest a better solution i came up with this idea to just do something like this



and just rinse and repeat for all my fields. i dont particularly like this method...i dont know why but its looks messy but i gotta work with what i have.


EDIT: or better yet maybe something like this...i like this a little better
EDIT#2: Below DOES NOT WORK, but leaving it here for reference!!


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Doesn't that mean you're defining the label in two places though?
Raymond Holguin
Ranch Hand

Joined: Aug 11, 2009
Posts: 47
im not sure what you are referring too david. But either way...the second option was a no go as the validator does not except params that are not defined in that type of validator. so i was not able to create a custom "label" param.

as for my first solution if your referring to the fact I define a string
form.requestDate.label = Request Date

and then call that string like this
<message>${getText("form.requestDate.label")} ${getText("required")}</message>

then i suppose it could be defining it twice since i might as well just explicitly say "Request Date" in the <message>, but the fact is that there are many instances in the app where the same field is used throughout many different forms. so defining it once makes it reusable no matter what form or page and its easy just to update the label field once in case i need too instead of searching through multiple validation files to update that field.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I was talking about the second one (which I didn't actually think would work, but misinterpreted your message as meaning it did).
Raymond Holguin
Ranch Hand

Joined: Aug 11, 2009
Posts: 47
yea that was my bad, i know that its struts1 you could arbitrarily define any params you want and i assumed you could do the same in struts2 so i suggested #2 w/out even testing it. wont make that mistake again!! ill edit my post to clarify it doesn't 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: Struts2 validation localization problem
 
Similar Threads
Validation Struts2
Struts2 conversion/validation problem
Struts2 & validation [SOLVED]
Struts2 validation
Conditional validation in Struts2