struts2动作类有没有给出的init方法?
是否为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看一下Preparable 接口和Prepare Interceptor。
Take a look at the Preparable interface and the Prepare Interceptor.
准备拦截器是可行的方法。 如果您的操作使用默认拦截器堆栈,只需将您的
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 toprepare()
.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*>()
(egprepareDeleteEmployee()
).是的,有:
首先,您的操作类必须实现 Preparable 接口。 然后,您的操作必须实现 Preparable.prepare() 方法。 Struts 2 每次调用您的操作方法之前都会执行prepare()。
干杯。
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.
是
在创建拦截器之后但在使用拦截处理任何请求之前调用,使拦截器有机会初始化任何所需的资源。
查看此
Yes
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