Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any init method given for struts2 action class?

Tags:

People also ask

What is execute method in Struts2?

The method execute is where we placed what we want this controller to do in response to the hello. action . Method execute of HelloWorldAction. public String execute() throws Exception { messageStore = new MessageStore() ; helloCount++; return SUCCESS; } Note that method execute declares it throws an Exception.

How do you call another action in Struts2?

This is called Action chaining in Struts 2. One action leads to another one and so on. Request > Action 1 > Action 2 > Response In Struts 2, this can be achieved by Chain Result. The Chain Result is a result type that invokes an Action with its own Interceptor Stack and Result.


Is there any init method provided for struts 2 action class that can be called before every method of that action class?

For example, I have an action class for struts 2 as given below

import com.opensymphony.xwork2.ActionSupport;

public class EmployeeAction extends ActionSupport{

    private  DepartmentDaoService deptService = new DepartmentDaoService() ;
    private  EmployeeDaoService empService = new EmployeeDaoService();
    private Employee employee;
    private List<Employee> employees;
    private List<Department> departments;

       public void init()
       {
          //Do initialization stuff here
       }

       public String getAllEmployees(){
          employees = empService.getAllEmployees();
          return "success";
       }

       public String deleteEmployee(){
        empService.deleteEmployee(employee.getEmployeeId());
        return "success";
       }
}

Now in above code when struts action for getAllEmployees() and deleteEmplyee() is called I want init() method to execute first. We can run it by calling it from both functions.

But is there any provision given in struts 2 that will run init method automatically on each call or struts 2 provides any such method for action clases?

Please tell me if anyone knows.

Thanks.