You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by joann luo <jo...@gmail.com> on 2012/02/28 16:04:29 UTC
How to implement alternate error messages in the Struts validation
framework. (Struts 2.0.1.4)
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.
Re: struts2-jquery-plugin: form submit returns twice the submit and
textfield
Posted by Mounir Benzid <mb...@meetingmasters.de>.
Hi Dave,
targets="formResult" in <s:aj> is unnecessary in my case. Simply
removing it solved the problem.
Thanks!
- Mounir
Am 29.02.2012 00:50, schrieb Dave Newton:
> You update the div with the same jsp as the form, so you're adding the page
> to itself.
>
> d.
>
> (pardon brevity and typos, on cell)
> On Feb 28, 2012 6:44 PM, "Mounir Benzid"<mb...@meetingmasters.de> wrote:
>
>> Here's a simple enough example of how to use the<sj:a> tag available in
>> the struts2-jquery plugin (v 3.2.1)
>> http://www.weinfreund.de/**struts2-jquery-showcase/index.**action<http://www.weinfreund.de/struts2-jquery-showcase/index.action>
>>
>>
>> Well what I get though is somehow mysterious (to me).
>>
>> The first time when I call the action [1] given by its URL
>> http://localhost:8080/myApp/**test/me<http://localhost:8080/myApp/test/me>
>> I get the correct server response, that is
>>
>> - a line saying "Echo: Hello me!"
>> - underneath it a textfield containig the phrase "Hello me !"
>> - followed by a submit button
>>
>> This is what it looks like:
>> Before: http://i40.tinypic.com/vf8782.**jpg<http://i40.tinypic.com/vf8782.jpg>
>>
>> Nice.
>>
>> But after I submit the form the textfield and the submit button get
>> somehow mysteriously duplicated! (but not the "Echo:..." textline)
>>
>> So there's still the initial textfield with the updated Hello +
>> whatever-you-typed-in phrase but yet another textfield and yet another
>> submit button.
>>
>> The only difference I can tell between the initial and the duplicated
>> textfield is that the latter never gets a refresh.
>> It always contains the initial "Hello me !" phrase.
>>
>> This is how it looks like after the submit:
>> After: http://i41.tinypic.com/2ji44j.**jpg<http://i41.tinypic.com/2ji44j.jpg>
>>
>> Maybe someone can tell what I'm missing here.
>> Thanks!
>>
>>
>> [1] http://pastebin.com/Wq0Ek1H4<http://pastebin.com/Wq0Ek1H4>**(Action
>> class)
>>
>> [2] http://pastebin.com/6yF2xwu4 (jsp)
>>
>> ------------------------------**------------------------------**
>> ------------------------------**------------------------------**
>> ------------------------------**----------------------
>>
>> [1] ACTION class
>>
>> package xxx.actions.yyy;
>>
>> import org.apache.log4j.Logger;
>> import org.apache.struts2.convention.**annotation.Action;
>> import org.apache.struts2.convention.**annotation.Actions;
>> import org.apache.struts2.convention.**annotation.Namespace;
>> import org.apache.struts2.convention.**annotation.Result;
>>
>> import com.opensymphony.xwork2.**ActionSupport;
>>
>> @Namespace("/test")
>> public class HelloWorldAction extends ActionSupport {
>>
>> public String getYourName() {return yourName; }
>> public void setYourName(String value) {this.yourName = value;}
>>
>> @Actions({
>> @Action(value="{yourName}", results={@Result(location =
>> "test.jsp")}),
>> @Action(value="put", results={@Result(location = "test.jsp")})
>> })
>> public String execute() {
>> yourName = "Hello " + yourName + " !";
>> logger.debug("execute: " + yourName);
>> return SUCCESS;
>> }
>>
>> private static final long serialVersionUID = 1L;
>> private String yourName;
>> private static Logger logger = Logger.getLogger(**
>> HelloWorldAction.class);
>>
>> }
>>
>>
>> [2] (jsp)
>> ------------------------------**------------------------------**
>> ------------------------------**----------------------------
>>
>> <%@ taglib prefix="s" uri="/struts-tags" %>
>> <%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
>>
>> <html>
>>
>> <head>
>> <sj:head/>
>> </head>
>>
>> <body>
>>
>> <div id="formResult">
>> <p>Echo : ${yourName}</p>
>> </div>
>>
>> <s:form id="form" action="put">
>> <s:textfield id="echo" name="yourName"/>
>> </s:form>
>>
>> <sj:a
>> id="ajaxformlink"
>> formIds="form"
>> targets="formResult"
>> indicator="indicator"
>> button="true"
>> buttonIcon="ui-icon-gear"
>> Submit form here
>> </sj:a>
>>
>> <img id="indicator" src="images/indicator.gif" alt="Loading..."
>> style="display:none"/>
>>
>> </body>
>>
>> </html>
>>
>>
>>
>>
>>
>>
--
Mounir Benzid
Systementwickler / EDV
meetingmasters.de
meetings meisterhaft managen
· Unabhängige Vermittlung von Tagungshotels
· Online-Hotelreservierungssystem zur Integration in die Veranstaltungs-Webseite
· Webbasiertes Veranstaltungs- und Teilnehmermanagement
· E-Procurement für den Tagungshoteleinkauf
· Webbasierte Anfrage und Verhandlung von Firmenraten
Max-Planck-Straße 22
D-54296 Trier
fon +49 (0)651-145789-38
fax +49 (0)651-145789-20
www.meetingmasters.de
mb@meetingmasters.de
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: struts2-jquery-plugin: form submit returns twice the submit and textfield
Posted by Dave Newton <da...@gmail.com>.
You update the div with the same jsp as the form, so you're adding the page
to itself.
d.
(pardon brevity and typos, on cell)
On Feb 28, 2012 6:44 PM, "Mounir Benzid" <mb...@meetingmasters.de> wrote:
> Here's a simple enough example of how to use the <sj:a> tag available in
> the struts2-jquery plugin (v 3.2.1)
> http://www.weinfreund.de/**struts2-jquery-showcase/index.**action<http://www.weinfreund.de/struts2-jquery-showcase/index.action>
>
>
> Well what I get though is somehow mysterious (to me).
>
> The first time when I call the action [1] given by its URL
> http://localhost:8080/myApp/**test/me<http://localhost:8080/myApp/test/me>
> I get the correct server response, that is
>
> - a line saying "Echo: Hello me!"
> - underneath it a textfield containig the phrase "Hello me !"
> - followed by a submit button
>
> This is what it looks like:
> Before: http://i40.tinypic.com/vf8782.**jpg<http://i40.tinypic.com/vf8782.jpg>
>
> Nice.
>
> But after I submit the form the textfield and the submit button get
> somehow mysteriously duplicated! (but not the "Echo:..." textline)
>
> So there's still the initial textfield with the updated Hello +
> whatever-you-typed-in phrase but yet another textfield and yet another
> submit button.
>
> The only difference I can tell between the initial and the duplicated
> textfield is that the latter never gets a refresh.
> It always contains the initial "Hello me !" phrase.
>
> This is how it looks like after the submit:
> After: http://i41.tinypic.com/2ji44j.**jpg<http://i41.tinypic.com/2ji44j.jpg>
>
> Maybe someone can tell what I'm missing here.
> Thanks!
>
>
> [1] http://pastebin.com/Wq0Ek1H4 <http://pastebin.com/Wq0Ek1H4>**(Action
> class)
>
> [2] http://pastebin.com/6yF2xwu4 (jsp)
>
> ------------------------------**------------------------------**
> ------------------------------**------------------------------**
> ------------------------------**----------------------
>
> [1] ACTION class
>
> package xxx.actions.yyy;
>
> import org.apache.log4j.Logger;
> import org.apache.struts2.convention.**annotation.Action;
> import org.apache.struts2.convention.**annotation.Actions;
> import org.apache.struts2.convention.**annotation.Namespace;
> import org.apache.struts2.convention.**annotation.Result;
>
> import com.opensymphony.xwork2.**ActionSupport;
>
> @Namespace("/test")
> public class HelloWorldAction extends ActionSupport {
>
> public String getYourName() {return yourName; }
> public void setYourName(String value) {this.yourName = value;}
>
> @Actions({
> @Action(value="{yourName}", results={@Result(location =
> "test.jsp")}),
> @Action(value="put", results={@Result(location = "test.jsp")})
> })
> public String execute() {
> yourName = "Hello " + yourName + " !";
> logger.debug("execute: " + yourName);
> return SUCCESS;
> }
>
> private static final long serialVersionUID = 1L;
> private String yourName;
> private static Logger logger = Logger.getLogger(**
> HelloWorldAction.class);
>
> }
>
>
> [2] (jsp)
> ------------------------------**------------------------------**
> ------------------------------**----------------------------
>
> <%@ taglib prefix="s" uri="/struts-tags" %>
> <%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
>
> <html>
>
> <head>
> <sj:head/>
> </head>
>
> <body>
>
> <div id="formResult">
> <p>Echo : ${yourName}</p>
> </div>
>
> <s:form id="form" action="put">
> <s:textfield id="echo" name="yourName"/>
> </s:form>
>
> <sj:a
> id="ajaxformlink"
> formIds="form"
> targets="formResult"
> indicator="indicator"
> button="true"
> buttonIcon="ui-icon-gear"
> >
> Submit form here
> </sj:a>
>
> <img id="indicator" src="images/indicator.gif" alt="Loading..."
> style="display:none"/>
>
> </body>
>
> </html>
>
>
>
>
>
>
struts2-jquery-plugin: form submit returns twice the submit and textfield
Posted by Mounir Benzid <mb...@meetingmasters.de>.
Here's a simple enough example of how to use the <sj:a> tag available in
the struts2-jquery plugin (v 3.2.1)
http://www.weinfreund.de/struts2-jquery-showcase/index.action
Well what I get though is somehow mysterious (to me).
The first time when I call the action [1] given by its URL
http://localhost:8080/myApp/test/me
I get the correct server response, that is
- a line saying "Echo: Hello me!"
- underneath it a textfield containig the phrase "Hello me !"
- followed by a submit button
This is what it looks like:
Before: http://i40.tinypic.com/vf8782.jpg
Nice.
But after I submit the form the textfield and the submit button get
somehow mysteriously duplicated! (but not the "Echo:..." textline)
So there's still the initial textfield with the updated Hello +
whatever-you-typed-in phrase but yet another textfield and yet another
submit button.
The only difference I can tell between the initial and the duplicated
textfield is that the latter never gets a refresh.
It always contains the initial "Hello me !" phrase.
This is how it looks like after the submit:
After: http://i41.tinypic.com/2ji44j.jpg
Maybe someone can tell what I'm missing here.
Thanks!
[1] http://pastebin.com/Wq0Ek1H4 <http://pastebin.com/Wq0Ek1H4>(Action
class)
[2] http://pastebin.com/6yF2xwu4 (jsp)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[1] ACTION class
package xxx.actions.yyy;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
@Namespace("/test")
public class HelloWorldAction extends ActionSupport {
public String getYourName() {return yourName; }
public void setYourName(String value) {this.yourName = value;}
@Actions({
@Action(value="{yourName}", results={@Result(location =
"test.jsp")}),
@Action(value="put", results={@Result(location = "test.jsp")})
})
public String execute() {
yourName = "Hello " + yourName + " !";
logger.debug("execute: " + yourName);
return SUCCESS;
}
private static final long serialVersionUID = 1L;
private String yourName;
private static Logger logger =
Logger.getLogger(HelloWorldAction.class);
}
[2] (jsp)
----------------------------------------------------------------------------------------------------------------------
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<html>
<head>
<sj:head/>
</head>
<body>
<div id="formResult">
<p>Echo : ${yourName}</p>
</div>
<s:form id="form" action="put">
<s:textfield id="echo" name="yourName"/>
</s:form>
<sj:a
id="ajaxformlink"
formIds="form"
targets="formResult"
indicator="indicator"
button="true"
buttonIcon="ui-icon-gear"
>
Submit form here
</sj:a>
<img id="indicator" src="images/indicator.gif" alt="Loading..."
style="display:none"/>
</body>
</html>
Re: Maven repository and struts2-jquery-plugin
Posted by Łukasz Lenart <lu...@googlemail.com>.
jQuery Plugin is available from Central
http://code.google.com/p/struts2-jquery/wiki/FAQ#How_can_I_use_the_Plugin_from_Maven?
Regards
--
Łukasz
Mobile +48 606 323 122
Office +27 11 0838747
http://www.lenart.org.pl/
Warszawa JUG conference - Confitura http://confitura.pl/
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Maven repository and struts2-jquery-plugin
Posted by Mounir Benzid <mb...@meetingmasters.de>.
Hi,
please point me to a maven repo with the latest jq-plugin release
I only have this link on google code:
http://code.google.com/p/struts2-jquery/downloads/detail?name=struts2-jquery-plugin-3.2.1.jar
Thanks!
cheers
- Mounir
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: How to implement alternate error messages in the Struts
validation framework. (Struts 2.0.1.4)
Posted by Jeffrey Black <je...@gmail.com>.
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 <jo...@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.
>