File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Struts framework Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts framework" Watch "Struts framework" New topic
Author

Struts framework

tanalyw tan
Greenhorn

Joined: Mar 09, 2010
Posts: 10
Hi,

I would like to know if it is possible for an action class to retrieve the errors validated by the struts validator framework.
Thanks.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Yes.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
Please note that your topic description line of Struts framework is pretty useless in a forum dedicated to Struts.
tanalyw tan
Greenhorn

Joined: Mar 09, 2010
Posts: 10
Hi, sorry for putting a generic heading. Thanks for the heads up.

Thanks for the quick reply. Could you tell me how I am suppose to go about it?
I cant seems to find a way to do that. Thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Which version of Struts?
tanalyw tan
Greenhorn

Joined: Mar 09, 2010
Posts: 10
I'm using struts 1.3.5
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234



Under the Action class if there any errors , make an ActionErrors object and put data into it .

You can display the Errors on to your UI using html:errors tag



Save India From Corruption - Anna Hazare.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'd try getErrors() first, although if validation is set to true I don't believe your action will fire, and instead will be forwarded to the input forward.
tanalyw tan
Greenhorn

Joined: Mar 09, 2010
Posts: 10
Hi i tried using getErrors() in my action. However, it doesnt even hit my action. I tried with both validation true and false. Any idea?

Following is my code

validation.xml


input.jsp


action
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It won't hit the action if you have validation turned on.

Personally, I never used automatic validation--rather turn off validation and call it manually.
tanalyw tan
Greenhorn

Joined: Mar 09, 2010
Posts: 10
Is it possible to do validation manually in action using struts validation framework?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Yes, either *actually* manually, by dealing with the form, or by calling the framework mechanism.

For example, I always had a base action class that went to the input forward on a GET, and called validate() on a POST.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Aside, there is no html:errors element in the JSP. This is what is used to display the errors returned by the validation.
tanalyw tan
Greenhorn

Joined: Mar 09, 2010
Posts: 10
I have been trying it around and searching for tutorials. I am able to get the error message printed out using <html:errors>.
However, what I am really looking for is something like this:

When user click on the submit button, the form will be sent to the action class.
Validation using struts validation framework will be used in the action class itself, to check for errors like duplicate name (compare with values from database) etc..

However, what I am doing now is before the form reach the action, it is already validated. But that is not what I need. I need the validation to be done in the action class itself. Correct me if I am wrong.. thanks. Any idea??

Thanks for the replies, appreciate it.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I just told you: turn off automatic validation. Do it manually.
tanalyw tan
Greenhorn

Joined: Mar 09, 2010
Posts: 10
Yes, I had heed your advice and turn off the validation. But how do you do validation in action using struts validation?

Check for errors normally and add an ActionMessage to the ActionErrors?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Call the form's validate() method if you're using the default Struts 1 validation mechanisms.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
how do you do validation in action using struts validation?


You can write the validation logic in the code of the Action class or in the ActionForm class, as already mentioned.
tanalyw tan
Greenhorn

Joined: Mar 09, 2010
Posts: 10
Hi,

I would like to check if what I am doing is correct and comply to the struts validation framework.

I did some checking in the action class and use the following code to send the error message to the jsp.

String msg = "Error Found";
ActionMessages am = new ActionMessages();
ActionMessage msg = new ActionMessage(message, false);
am.add("error", msg);
saveErrors(request, msg);
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

saveErrors takes the request and an ActionMessages, not a single message.

As an aside, the variable name "am" is a little confusing.
tanalyw tan
Greenhorn

Joined: Mar 09, 2010
Posts: 10
ya.. sorry for the mistake.
should be saveErrors(request, am) instead.
Thanks. I'll change the variable name
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Also, please UseAMeaningfulSubjectLine when posting. Explaining your question or remark briefly on the subject line allows us to know what we are getting into, and if we will be able to help. A subject of "Struts framework" in a forum dedicated to Struts isn't very helpful. Thanks!
tanalyw tan
Greenhorn

Joined: Mar 09, 2010
Posts: 10
okie will do. Thanks all for the help =)
 
wood burning stoves
 
subject: Struts framework