• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Struts 2.1.8 validation.xml & internationalization

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am coverting our site that has internationalization to an MVC architecture instead of just tiles. To do this Ive decided on using struts & tiles. Im truly a beginner at struts and struggling a little with the internationalization / validation. I must say though the OGNL is a powerful and intuitive tool.

My problem is I want to get rid of the 50 or so files javascript script include files we are using for validation and let the server do the work. I have set up a very simple example and for the life of me cant figure out how to internationalize the validation.

--- The form --
<s:form name="loginform" action="loginuser" method="post" enctype="application/x-www-form-urlencoded" >
<s:textfield name="username" label="%{getText('LBL_LOGIN_USER')}"/>
<s:password key="LBL_LOGIN_PWD" name="password" label="%{getText('LBL_LOGIN_PWD')}"/>
<s:submit />
</s:form>

-- The Login-validation.xml --
<validators>
<field name="username">
<field-validator type="requiredstring">
<message key="username.required"/>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<message key="[Password is a required field.]"/>
</field-validator>
</field>
</validators>

-- the properties file in the classes folder --
password.required = Password is a required field.
username.required = User Name is a required field.

So the question is what is the most elegant solution to get my validation internationalized?

I prefer to keep the validation out of my Actions. Some of the validation needed is quite complicated and I am really keen to use the struts functionality to simplify it with regular expressions. I am also looking forward to some of the built in functionality such as date ect.

Thanks,
Earl



 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read. You can edit your post by using the button.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You really shouldn't use the "key" attribute if you're going to use "name" and "label"; "key" is for when you are wanting to *avoid* using "name" and "label".

I'm not entirely sure I know what you're asking; if you're using keys, your app is already localized. If you're talking specifically about date and number formats, depending on the field types, you may already be done as well. Are you having a specific issue?
 
E Robb
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry about the code formatting. Im still learning.

As you know I need help on i18n. I have to tell you Im not sure what the difference between a key name and label is. OK avoid key when using name. Sorry Im just trying out different things to see what I need to do to have the value in <message key="username.required"/> return an internationalized message.

My problem is that I cant figure out how to internationalize error messages using validation.xml. Well thats only one of my problems, struts has a learning curve and I aint a rocket scientist.

May I ask if you were using this model and looking at the code provided how would you get username.required internationalized from a properties file?

Or if I am doing this the wrong way for struts validation can you point me in the right direction.

How can I get username.required from my Language.properties or Language_fr.properties file to display something like "HELLO" or "French HELLO" depending on the language. Im not talking about $ or any date formatting. I just cant get it to see the different properties files. It always displays a literal

No matter what lang I set my browser for validation.xml always brings back a literal its not i18n

earl

 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you looked at the localization documentation?

http://struts.apache.org/2.x/docs/localization.html

I'd start there.
 
E Robb
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep I have a pretty good handle on that. Use extends in the tiles over loading.

I put the property attribute in the tiles.xml
<put-attribute name="title" value="TITLE_CAND_PROFILE" />

Then I grab it in the tiles and use OGNL to grab the definition from the properties file.
<tiles:importAttribute name="title" />
<title><s:text name="SITE_TITLE"/> - <s:text name="%{#attr['title']}"/></title>

What I cannot figure out how to do is i18n with validation.xml. There must be some trick that Im to thick to think of.

You have any examples? Cuz I have googled it. Im using struts 2.1.8 and I am not getting any love.

earl
 
E Robb
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry this reply is for the wrong post. My bad. I will look at the recommended URL further.

Again my bad.

earl
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A post on a different site? Please BeForthrightWhenCrossPostingToOtherSites, if so. If not... not sure what post you're referring to.
 
E Robb
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is not a x post and I dont think its that difficult a question. Maybe no one one the planet has used validation.xml & i18n

Please can someone point me in the right direction.

validation.xml:
<validators>
<field name="username">
<field-validator type="requiredstring">
<message key="requiredstring"/>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<message key="[Password is a required field.]"/>
</field-validator>
</field>
</validators>

How do I make this xml i18n compliant. Yea I shouldnt use Key, and I need to format.

I should use name="propertiesFileKeyName" in my xml
Sorry Man Im dense.

earl
Are you saying it should be <message name="nameKeyfromPropertiesFile">
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read. You can edit your post by using the button.

If you want to get a message from a resource, use the "key" attribute of the message element.

Some details (page looks to be slightly broken, but I don't have time to deal with it right now):

http://struts.apache.org/2.x/docs/validation.html

If that doesn't help, you'll have to be more specific with the problem you're having.
 
E Robb
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im not sure how I can be more detailed that can someone give me an example of struts validation.xml and i18n

The link you provided shows validation.xml but I dont see where it integrates i18n for the response messages.

Thats all I am asking for. Surely, dont call me Shirley... surley someone has used struts xml validation & i18n.

Is it possible, does it work? how do you do it. I wont post my 5 lines of code because you complain there not formatted. Im not asking for the moon and stars just one little thing. How do you use actionName-validate.xml with i18N

thanks,
earl
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

E Robb wrote:The link you provided shows validation.xml but I dont see where it integrates i18n for the response messages.


By using the "key" attribute of the message element. The key defines the message that will be shown on a validation error. The message is defined in a resource file following normal Java I18N resource file naming conventions. The resource files are looked up as detailed in the previous I18N link I provided, which also provides details regarding how the current locale is determined.

I cannot make it any clearer than that, and I don't know what specific issue you're having, because you won't tell me.

I wont post my 5 lines of code because you complain there not formatted.


So format it. I provided a link that shows how.
 
E Robb
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry man I didnt mean to get you jacked up. Im the idiot and dont know what the hell Im doing.

Let me go over this validation.xml stuff again and get back to you.

Thank you for all your help.

Im sure Ill be back though.

earl
 
E Robb
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK sorry I cant help myself. I crack myself up some times. This is the validation.xml that struts is using. Its associated with an action so its called Login-validation.xm.

With in that file are the definitions of validation & more importantly the response Messages. Now if your just a redneck from virginia it doesnt matter if your error messages can support canadian, danish or finish. But if your working on an enterprise application you are going to want your error messages language specific.

So enough of the rant. Im using struts 2.1.8. I have a login form with a Login-validation.xml for struts so struts will automatically handle missing user name / password.

Well when struts sees I havent entered a user name or pass word it throws an error based on the Login-validation file I created AND its always a literal of whats in the xml file it is never i18.

Yes Ive read the documentation. I have found no answers so Im searching around. Here is the Login-validation.xml that I cant get internationalized. I know it looks strange. It looks strange because Im doing everything I can think of to get it to grab the properties files.



</validators>

 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what you mean by "no answers", the docs on the S2 wiki are correct, and IIRC the apps in the distro have an example.

In order to begin answering your question, please provide the deployed file structure including the location and names of the action class, the validation file, and the property file(s) you're using, as well as the actual behavior you're seeing. It's important to TellTheDetails... And remember, ItDoesntWorkIsUseless. It's important to help people help you-we're not in front of your machine, and know only what you tell us.
 
E Robb
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dude your kidding right? If you dont know i18n & validation.xml just say so. Im not deploying all my code to you so you can look at a struts xml file I have already shown you.

earl
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, fine. Read the I18N chapter in the book I wrote about Struts 2.

Good luck.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(And no, I wasn't kidding. Nor did I ask you to repost your files--I asked for the deployment hierarchy. You may want to read Eric Raymond's "How to ask Questions the smart way", and take it to heart.)

http://catb.org/esr/faqs/smart-questions.html
 
E Robb
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HERE is the Answer, dont forget to clear your jboss cache! This works for internationalization. I have been burnt so many times on cache makes me crazy. Apologies for wasting everyones time. Thank you for trying to help a poor lost sole thats making changes & testing with cached data.

actionName-validation.xml


Form:





Properties file contains the following:

password.required = Password is a required field.
username.required = User Name is a required field.

earl
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
* <s:form>'s "validate" attribute is only useful for client-side or Ajax validation; are you actually doing that, or do you just believe it has something to do with server-side validation.

* The "key" attribute is really a replacement for the combination of the "name" and "label" attributes--no real need to use it if you're just going to provide a name and label manually. The "key" attribute would be useful if, say, you had message resources named "username" and "password".
Have you determined that it's actually necessary to provide locale-specific mappings here? I've never had to do that; it's always been handled automatically by Java's I18N facilities.

Ew; tabs.
 
E Robb
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey David,
No I am not using client-side validation. I havent been able to figure it out yet. If you have a good link to client side validation I would LOVE to know how to do it. I have removed validate=true. I was taking the shotgun approach to trying to figure out validation. If you put enough stuff in there something is bound to work.

I also have removed the Key attribute. I see what your saying and your right in this case there is no reason for me to define a key its all handled automatically by struts i18n. Im not using a special properties file.

In regards to constant name="struts.custom.i18n.resources" I cant remember where I picked up this idea but it looks pretty standard. http://struts.apache.org/2.0.14/docs/how-do-i-set-a-global-resource-bundle.html Im hoping by defining the languages struts will have to do less searching for the properties files.

Really you dont like my tabs? dont you think that makes the code more readable?

Earl
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In regards to constant name="struts.custom.i18n.resources" I cant remember where I picked up this idea but it looks pretty standard. http://struts.apache.org/2.0.14/docs/how-do-i-set-a-global-resource-bundle.html Im hoping by defining the languages struts will have to do less searching for the properties files.


I don't see anything there regarding adding the country code when defining the property files, and certainly not about adding locale-specific files--which is completely unnecessary, and a maintenance nightmare. If anything, you've made the initial lookup take *longer* (although not significantly), since the global resources are the last place checked.

E Robb wrote:Really you dont like my tabs? dont you think that makes the code more readable?


Indentation makes code readable--not tabs. Tabs are just one way to get indentation--and fraught with interoperability issues. What if our tab settings are different, and a combination of tabs and spaces are used? As an example, if I cut-and-paste your code sample into an external editor (or it's done automatically by a browser plugin), this is what I see:(Except the spaces are tabs.) That alignment makes no sense to me. Obviously you can indent your code however you want--but there are legitimate issues with tabs.
 
E Robb
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey David,
I will remove all the defined struts.custom.i18n.resources for everything that doesnt regard languages. Point taken, it is a bit of a hassel to have define everything except for non language properties files. Ill let you know how it goes. I have another thread open. Ive apparently done something stupid and cant get my fallback language to come up so Ill post again to that thread after using your advise from this thread.

Now on the formatting thing. I want my code to be clean. Im using eclipse /dream weaver /jBuilder & have been tabbing for years. I see your point & the screen shot you provided is disturbing me a bit. Ive seen it go in the other direction where I have gotten code from the ukrain with people using spaces instead of tabs and its a train wreck to edit in Dream Weaver / eclipse. Is there some sort of accepted standard that I am not aware of?


Appreciate the help.

earl
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I fail to see how spaces could make a file more difficult to edit in Eclipse. DreamWeaver, everything is a disaster.
 
E Robb
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You got that right everything is a disaster! Im lucky if I have a week a month programming that the world is not crashing down around me.

Shift tabbing for formatting blocks of text become more difficult. instead of tab have to space space space space. I dont type so well.

Need to figure out my issue with i18n fall back property files not working. Hope to see you there.

earl
 
reply
    Bookmark Topic Watch Topic
  • New Topic