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 saveErrors method in Action class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "saveErrors method in Action class" Watch "saveErrors method in Action class" New topic
Author

saveErrors method in Action class

jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
I have the following in my code:

ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.logon.connect"));

saveErrors(request,errors); // how am i able to call the saveErrors method without a object ?
return (new ActionForward(mapping.getInput()));
}
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What do you mean, without an object?
jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
saveErrors() is a method of org.apache.struts.Action class. So when we call saveErrors() why are we not using object.methodname() ?

Can i say that the ActionServlet takes care of creating the instance of Actionform ?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

jose chiramal wrote:saveErrors() is a method of org.apache.struts.Action class. So when we call saveErrors() why are we not using object.methodname() ?

Because you're in the action whose saveErrors() you're calling... you might want to brush up on some Java basics before proceeding much further--if you're getting lost with casting and Java programming fundamentals it's a lot more productive to get them nailed down before diving into applications.
Can i say that the ActionServlet takes care of creating the instance of Actionform ?

The request processor instantiates the action forms.
jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
Hi David Newton,

In java any method has to be called using an object unless it is a static method which can be called directly or using class name. In this case saveErrors() is not static, how are we able to call saveErrors(request,errors) directly without using any object is my question.

Awaiting a reply...
jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
Supporting my previous post , for example to use the add method in ActionErrors i use :

ActionErrors errors = new ActionErrors();

errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.general");
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

jose chiramal wrote:In java any method has to be called using an object unless it is a static method which can be called directly or using class name. In this case saveErrors() is not static, how are we able to call saveErrors(request,errors) directly without using any object is my question.


Java 101. Please, please go back to the basics before considering doing much else, otherwise almost all code is going to look very confusing to you.

See Foo.baz()? It calls bar().

bar() is an instance method of class Foo that can be called by "any" other (non-static) method of Foo.

A Struts 1 action is an instance of class Action. Your action is a subclass of Action. Your action methods can call other methods of your action--or methods contained in its superclass... which is Action.

There's an implicit this in front of instance method calls within an instance. saveErrors(...) is an instance method of an Action. Essentially every single non-trivial Java class will call instance methods like this--it's almost impossible you haven't seen this before.
jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
Yes, i remember having seen this ..thanks David Newton for your reply.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: saveErrors method in Action class