struts2动作类有没有给出的init方法?

发布于 2024-07-22 15:38:47 字数 1108 浏览 5 评论 0原文

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

例如,我有一个 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";
       }
}

现在在上面的代码中,当调用 getAllEmployees()deleteEmplyee() 的 struts 操作时,我想要 首先执行 init() 方法。 我们可以通过从两个函数调用它来运行它。

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

如果有人知道请告诉我。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

女皇必胜 2024-07-29 15:38:47

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

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

Prepare Interceptor is way to go. If your action is using default interceptor stack just rename your init() method to 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()).

花开浅夏 2024-07-29 15:38:47

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.

Cheers.

狼亦尘 2024-07-29 15:38:47

init() 

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

查看此

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文