Nadia, have you ever looked at this site?
http://www.reumann.net/do/struts/main
Should be some good examples of using ActionMessages and ActionErrors in there.
Erik
Nadia Kunkov wrote:
Do I add saveMessages(request, (ActionMessages) errors); to the Validate method of ActionForm? Since that is where the validation is happening. Thanks
-----Original Message----- From: Hollaway, Shedrick CIV (TRFKB C600) [mailto:[EMAIL PROTECTED] Sent: Friday, October 15, 2004 10:00 AM To: 'Struts Users Mailing List' Subject: RE: Newbie <html:errors/> never displays errors
Looks like you are not saving your errors. Try this after errors.add:
saveMessages(request, (ActionMessages) errors);
-----Original Message----- From: Nadia Kunkov [mailto:[EMAIL PROTECTED] Sent: Friday, October 15, 2004 9:01 AM To: Struts Users Mailing List Subject: RE: Newbie <html:errors/> never displays errors
David,
I followed your suggestions. I have everything set up the way you said.
I have a default error: errors.add("countryName", new ActionError("errors.required","Country Name"));
I'm displaying the error using
<logic:messagesPresent> <ul> <html:messages id="error" property="countryName"> <li><bean:write name="error"/></li> </html:messages> </ul> </logic:messagesPresent>
I get the following error: javax.servlet.ServletException: Cannot find bean error in any scope
I also tried doing the following: logic:messagesPresent> <ul> <html:messages id="error"> <li><bean:write name="countryName"/></li> </html:messages> </ul> </logic:messagesPresent>
I still get an error :
javax.servlet.ServletException: Cannot find bean countryName in any scope
It seems like the error doesn't get added to the session or request. Anything else you could suggest? Thanks Nadia
-----Original Message----- From: David G. Friedman [mailto:[EMAIL PROTECTED] Sent: Wednesday, October 13, 2004 3:26 PM To: Struts Users Mailing List Subject: RE: Newbie <html:errors/> never displays errors
Nadia,
Are you sure you have the 3 important pieces?
A) a struts-config.xml (or module) define message resources file? Mine is
the file WEB-INF/classes/application.properties, which contains the line:
errors.required={0} is required.
My struts-config.xml includes this resource with the line: <message-resources parameter="application" null="true" />
B) Have you put a 'default' error message in your ActionForm's validate()
method for testing purposes so your method does not end with:
return(errors);
but with:
errors.add("countryName", new ActionError("errors.required","Country
Name"));
return(errors);
C) You have JSP Code like this:
<logic:messagesPresent> <ul> <html:messages id="error"> <li><bean:write name="error"/></li> </html:messages> </ul> </logic:messagesPresent>
I got this to work but only AFTER I made sure I had step A configured properly.
Since you used property key names when you added each error instead of using
the Globals.ERROR key, a.k.a. the string "error". You added each item under
keys like "countryName" and "countryCapital" so you could access them
individually like this:
<logic:messagesPresent> <ul> <html:messages id="error" property="countryName"> <li><bean:write name="error"/></li> </html:messages> </ul> </logic:messagesPresent>
AND
<logic:messagesPresent> <ul> <html:messages id="error" property="countryCapital"> <li><bean:write name="error"/></li> </html:messages> </ul> </logic:messagesPresent>
Regards, David
-----Original Message----- From: Nadia Kunkov [mailto:[EMAIL PROTECTED] Sent: Wednesday, October 13, 2004 1:20 PM To: Struts Users Mailing List Subject: RE: Newbie <html:errors/> never displays errors
Nooo... I guess that's it!
I thought you need to call saveErrors only when you create ActionErrors
inside Action and not in Validate...
Here is my Action (I'm using DispatchAction):
public ActionForward Add (ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest req, HttpServletResponse res) throws Exception {
javax.sql.DataSource dataSource;
dataSource = (DataSource)
servlet.getServletContext().getAttribute(org.apache.struts.Glo
bals.DATA_SOUR
CE_KEY);
CountryService countryService = new CountryService();
CountryDTO country = new CountryDTO();
BeanUtils.copyProperties( country,(AddCountryNewForm) actionForm);
countryService.addCountry(country,dataSource);
return actionMapping.findForward("newcountrylisting"); }
Where do I save the errors generated by the validate?
Thanks
-----Original Message----- From: David G. Friedman [mailto:[EMAIL PROTECTED] Sent: Wednesday, October 13, 2004 1:10 PM To: Struts Users Mailing List Subject: RE: Newbie <html:errors/> never displays errors
Did you call saveErrors() or saveMessages() in your Action to save the messages into the (request) scope for use in the JSP?
-----Original Message----- From: Nadia Kunkov [mailto:[EMAIL PROTECTED] Sent: Wednesday, October 13, 2004 12:59 PM To: Struts Users Mailing List Subject: RE: Newbie <html:errors/> never displays errors
Yes, here is what I tried:
<logic:messagesPresent> <ul> <html:messages id="error" name="countryName"> <li><bean:write name="error"/></li> </html:messages> </ul> </logic:messagesPresent>
I get an error: can't find bean "error" in any scope :( This drives me nuts. Thanks for helping me.
-----Original Message----- From: David G. Friedman [mailto:[EMAIL PROTECTED] Sent: Wednesday, October 13, 2004 12:56 PM To: Struts Users Mailing List Subject: RE: Newbie <html:errors/> never displays errors
Nadia,
The bean:write's name attribute must match the id parameter of the html:messages tag. That's why I suggested your html:messages tag be:
<html:messages id="error" name="countryName">
This means it should pull the errors from the area "countryName" and present
them for the bean:write tag to use as the name "error" (see id=".."). Have
you tried this yet?
Regards, David
-----Original Message----- From: Nadia Kunkov [mailto:[EMAIL PROTECTED] Sent: Wednesday, October 13, 2004 12:46 PM To: Struts Users Mailing List Subject: RE: Newbie <html:errors/> never displays errors
Here is my jsp: ...... ..... <form:submit property="method" value="Add"></form:submit> </form:form>
<logic:messagesPresent> <ul> <html:messages id="error" > <li><bean:write name="countryName"/></li> </html:messages> </ul> </logic:messagesPresent>
</body> </html:html>
Thanks for your help
-----Original Message----- From: David G. Friedman [mailto:[EMAIL PROTECTED] Sent: Wednesday, October 13, 2004 12:35 PM To: Struts Users Mailing List Subject: RE: Newbie <html:errors/> never displays errors
Please show your JSP code. I think this should have worked:
<logic:messagesPresent> <ul> <html:messages id="error" name="countryName"> <li><bean:write name="error"/></li> </html:messages> </ul> </logic:messagesPresent>
Remember, the "id" is essentially the key you give to the bean:write but the
name="" attribute is what messages key to retrieve. That name="" attribute
defaults to the global errors word if you don't specify anything else.
Regards, David
-----Original Message----- From: Nadia Kunkov [mailto:[EMAIL PROTECTED] Sent: Wednesday, October 13, 2004 12:31 PM To: Struts Users Mailing List Subject: RE: Newbie <html:errors/> never displays errors
Thanks Did that and specified the write bean name this time. <li><bean:write name="countryName"/></li> Now I get an error that bean countryName doesn't exist in any scope. I get a feeling it's something with my message resources.
How can I see that the following line works. errors.add("countryName", new ActionError("errors.required","Country Name"));
Thanks Nadia
-----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: Wednesday, October 13, 2004 11:32 AM To: [EMAIL PROTECTED] Subject: RE: Newbie <html:errors/> never displays errors
Hi Nadia, Use this to write your message :
<logic:messagesPresent> <ul> <html:messages id="error"> <li><bean:write name="error"/></li> </html:messages> </ul> </logic:messagesPresent>
<bean:write> will write error message who are call in your validate method
and <logic:messagesPresent> will check if these message are present.
Charles Gouin-Vallerand
-----Original Message----- From: Nadia Kunkov [mailto:[EMAIL PROTECTED] Sent: October 13, 2004 11:16 AM To: Struts help (E-mail) Subject: Newbie <html:errors/> never displays errors
Hi,
I have gone through multiple postings and followed the suggestions about
displaying the errors. I just need another pair of eyes to look at this,
I'm probably missing something.
I can't display errors on the page. I know that I go through the validate
method and it finds errors because I'm not moving to the next page when I do
a submit. If I have an error I want to stay on the same page and let the
user resubmit. I do stay on the same page but under the form I want error
messages to be displayed and they are never there. Please take a look
below.
I have this in my MyAppResources.properties which resides in /WEB-INF/classes directory errors.required={0} is required.
in my struts-config.xml I have validate="true"
and also <message-resources parameter="MyAppResources" null="false"/>
In my ActionForm I have the following in the Validate method:
public ActionErrors validate (ActionMapping actionMapping,
HttpServletRequest httpServletRequest)
{
ActionErrors errors = new ActionErrors();
if (getCountryName() == null || getCountryName().length() <
1)
{
errors.add("countryName", new
ActionError("errors.required","Country Name"));
}
if (getCountryCapital() == null ||
getCountryCapital().length() < 1)
{
errors.add("countryCapital", new
ActionError("errors.required","Country Capital"));
}
return errors;
}
In my jsp file I have the following .....some stuff......
<form:submit property="method" value="Add"></form:submit> </form:form> <html:errors/> </body> </html:html>
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]