when will be the execute() method in action class will be called?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

when will be the execute() method in action class will be called?

Naag
Hello,

when will be the execute() method in the action class called?
when will be the prepare() method will be called?
are the both prepare() and execute() methods must for any action class?
 
 i am aking these questions,because in my project,in some action classes both prepare and action methods are called and in some action classes only prepare method is called?
thats why i am in a confused state?

the code i used is as follows---

in this class both methods are called----

public String execute() throws Exception {
            log.info("entering into execute method");
                long id = (Long)ServletActionContext.getRequest().getSession().getAttribute("deviceId");
                BootstrapRequestDao device = BootstrapRequestDao.getById(id);
                log.info("getting the selected device for edit");
                populateDevice(device);
                log.info("device::" +device);
                return SUCCESS;
        }
       
        public void prepare() throws Exception {
                OperatorDao op;
          Iterator<OperatorDao> ops = OperatorDao.list().iterator();
                operators = new LinkedHashMap<Long, String>();
                while (ops.hasNext()) {
                        op = ops.next();
                operators.put(new Long(op.getCode()), op.getName());
                }
               
        }




another class is that------

in this class only prepare method is called---------

        public void prepare() throws Exception {
       
                OperatorDao op;
                Iterator<OperatorDao> ops = OperatorDao.list().iterator();
                operators = new LinkedHashMap<Long, String>();
                while (ops.hasNext()) {
                        op = ops.next();
                        operators.put(new Long(op.getCode()), op.getName());
                                       
                }
                populateDevices();
     
       
        }





please tell me the reasons----

i apologises if it is chilly question, but i am very beginer to the development

thanking u
urs
Naag


   

 
Reply | Threaded
Open this post in threaded view
|

Re: when will be the execute() method in action class will be called?

dynamicd
check ur interceptors
prepare will be called before the execute I am assuming you are implementing the Preparable interface
also <interceptor-ref name="prepare" /> is one of the interceptors in your interceptor stack


Reply | Threaded
Open this post in threaded view
|

Re: when will be the execute() method in action class will be called?

dynamicd
In reply to this post by Naag
1) When u have the action defined in the struts.xml file where the method is not defined by default it will call the execute method.

3) the execute action is not a must for every action class.. In the struts.xml file you can define what method you want to be called by setting the method attribute for an action
method ="Somemethod" and it will call that method

Reply | Threaded
Open this post in threaded view
|

Re: when will be the execute() method in action class will be called?

abdou
Hi
What about if i am 2 or more methods in my action and i put them in struts.xml file ... execute() method will be called . Thanks
Reply | Threaded
Open this post in threaded view
|

Re: when will be the execute() method in action class will be called?

sankar.lp.gym
In reply to this post by Naag
First all getXXX methods are called.

The execute method called

Then setXXX methods are called

Struts2 Training in Chennai