struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jeffrey Black <jeffblack...@gmail.com>
Subject Re: How to implement alternate error messages in the Struts validation framework. (Struts 2.0.1.4)
Date Tue, 28 Feb 2012 18:42:53 GMT
Hi Joann.

It appears that you are referencing a Struts 1.x book yet you are using
Struts 2.  Is that correct?

If you are indeed using Struts 2 I would have a look at chapters 10 & 11 in
this book --> http://tinyurl.com/7nt9uez

The book does not address the framework's latest feature-set; however it
should get you 90% there.

I hope this helps.

jb



On Tue, Feb 28, 2012 at 9:04 AM, joann luo <joannluo66@gmail.com> wrote:

> Hello,
>
> I am currently using the Struts2 validation framework for my project.  I
> have Struts 2.0.14.   I have been trying to find a way to display alternate
> error message through validator framework.
>
> I tried two things.
>
>
>
>   1. I added a bundle attribute in the following code in the web.xml but
>   that did not work.  The com.mytest.strings bundle is located in the
>   WEB-INF\classes folder.
>
>
>
> <field property="attribute(Number)"
> depends="required,mask,maxbytelength,nonce">
>
> <msg name="required" *bundle=”com.mytest.strings      *
>
> ”key="errors.required" />
>  2.       Based on the following information from Programming Jakarta
> Struts Chapter 11 The Validator Framework
>
> http://books.google.com/books?id=E874l0jaUGQC&pg=PA260&lpg=PA260&dq=change+the+key+values+in+the+validation-rules.xml+if+you+plan+to+use+alternative+messages&source=bl&ots=t5zzY8fSs4&sig=wEOspEiq8XsU2su4yD1WlCz8H6w&hl=en&sa=X&ei=8_ZLT6TOHoPi0QGB4YiTDg&ved=0CCIQ6AEwAA#v=onepage&q=change%20the%20key%20values%20in%20the%20validation-rules.xml%20if%20you%20plan%20to%20use%20alternative%20messages&f=false
>
>
>
>
> *“You should add these to your application’s resource bundle, or change the
> key values in the validation-rules.xml if you plan to use alternative
> messages.”*
>
>
>
> I have the following code in validator-rules.xml file.
>
>
>
> <validator   name="required"
>
>            classname="StrutsValidators"
>
>               method="validateRequired"
>
>         methodParams="java.lang.Object,
>
>                       org.apache.commons.validator.ValidatorAction,
>
>                       org.apache.commons.validator.Field,
>
>                       org.apache.struts.action.ActionMessages,
>
>                       javax.servlet.http.HttpServletRequest"
>
>                  msg="errors.required.new"/>
>
>
>
>            <validator   name="mask"
>
>            classname=" StrutsValidators"
>
>               method="validateMask"
>
>         methodParams="java.lang.Object,
>
>                       org.apache.commons.validator.ValidatorAction,
>
>                       org.apache.commons.validator.Field,
>
>                       org.apache.struts.action.ActionMessages,
>
>                       javax.servlet.http.HttpServletRequest"
>
>              depends=""
>
>                  msg="errors.invalid.new"/>
>
>
>
>
>
> I added the following string to a separate bundle but with the same
> properties file name. That bundle is located in a different directory.    I
> updated the values of these two entries in this new properties file.
>
>
>
> errors.required.new={0} is required NEW.
>
> errors.invalid.new={0} is not valid NEW.
>
>
>
>
>
> My validation.xml file looks like this:
>
>
>
> <field property="attribute(Number)"
> depends="required,mask,maxbytelength,nonce">
>
> <msg name="required" key="errors.required" />
>
> <msg name="maxbytelength" key="errors.maxlength" />
>
> <var>
>
> <var-name>maxbytelength</var-name>
>
> <var-value>32</var-value>
>
> </var>
>
> <var>
>
> <var-name>form</var-name>
>
> <var-value>Info</var-value>
>
> </var>
>
> <msg name="mask" key="errors.invalid" />
>
> <arg0 key="label.Number" />
>
> <var>
>
> <var-name>mask</var-name>
>
> <var-value>^[0-9\-() ]*$</var-value>
>
> </var>
>
> </field>
>
>
>
> When I ran my test I still see the standard error messages such as the ones
> listed below displayed on the UI.  It appears that the validation framework
> still reads from the default bundle.  However, all other labels on the same
> UI can read from the customized properties file.
>
> errors.required.new={0} is required.
>
> errors.invalid.new={0} is not valid.
>
>
>
> Is this a Struts issue?  If not, what is the right way to implement
> alternate validation message in this case?
>
>
>
> Thanks a lot.
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message