java 是否为 struts2 操作类提供了任何 init 方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/925815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:23:51  来源:igfitidea点击:

Is there any init method given for struts2 action class?

javastruts2

提问by amar4kintu

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

是否为 struts 2 操作类提供了任何可以在该操作类的每个方法之前调用的 init 方法?

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

例如,我有一个用于 struts 2 的动作类,如下所示

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.

现在在上面的代码中,当 struts action forgetAllEmployees()deleteEmplyee()被调用时,我希望init()方法首先执行。我们可以通过从两个函数调用它来运行它。

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?

但是,struts 2 中是否有任何规定会在每次调用时自动运行 init 方法,或者 struts 2 是否为动作类提供了任何此类方法?

Please tell me if anyone knows.

请告诉我,如果有人知道。

Thanks.

谢谢。

采纳答案by Guido

Yes there is:

就在这里:

First of all, your action class must implement the Preparable interface. Then, your action must implement Preparable.prepare() method. Struts 2 will execute prepare() everytime before it invokes your action method.

首先,您的操作类必须实现Preparable 接口。然后,您的操作必须实现 Preparable.prepare() 方法。Struts 2 将在每次调用您的操作方法之前执行 prepare()。

Cheers.

干杯。

回答by rdk

Prepare Interceptor is way to go. If your action is using default interceptor stack just rename your init()method to prepare().

准备拦截器是要走的路。如果您的操作使用默认拦截器堆栈,只需将您的init()方法重命名为prepare().

If your action class has multiple action methods (like createEmployee() or deleteEmployee()) you can do specific preparation for concrete method with method named prepare<*ActionMethodName*>()(eg prepareDeleteEmployee()).

如果您的操作类有多个操作方法(如 createEmployee() 或 deleteEmployee()),您可以使用名为prepare<*ActionMethodName*>()(例如prepareDeleteEmployee())的方法为具体方法做特定的准备。

回答by Dhiral Pandya

Yes

是的

init() 

Called after an interceptor is created, but before any requests are processed using intercept , giving the Interceptor a chance to initialize any needed resources.

在创建拦截器之后调用,但在使用拦截处理任何请求之前,让拦截器有机会初始化任何需要的资源。

See this

看到这个