Action类构造函数如何与Struts2一起使用? [英] How do Action class constructors work with Struts2?

查看:132
本文介绍了Action类构造函数如何与Struts2一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我直接转发到我的动作类中的一个方法(通过struts.xml),但是实际上我并未创建我的动作类的新实例。我问,因为在使用ActionClass进行任何操作之前,我需要设置一些变量,我认为构造函数可能会帮助我做到这一点。

I am directly forwarded to a method within my action class (by struts.xml) but I do not actually create a new instance of my Action Class. I ask because I have some variables to set before anything can be done with the ActionClass, and I thought constructors might help me do just that.

推荐答案

Struts 2提供了一个Prepare Interceptor,可以在实际的execute方法运行之前准备好数据。此拦截器在action上调用prepare()实施Preparable。
在操作类中使用prepare方法的一些用例是

Struts 2 provides a Prepare Interceptor to prepare your data before the actual execute method runs.This interceptor calls prepare() on actions which implement Preparable. Some of the use cases for using the prepare method in your action class are


  1. 从数据库加载对象,以便在参数已设置,可以在此对象上设置。

  2. 准备/初始化在实际方法调用之前所需的任何数据。

简而言之,此拦截器就像Action类的init / constructor一样,您可以控制所有需要初始化的参数或进行任何初始查找。

In short this interceptor is like an init/constructor for the Action class and you have all control to initialize any required parameters or do any initial lookup.

有关详细信息,请参阅拦截器页面
准备-拦截器

For details refer to the interceptor page prepare-interceptor

这篇关于Action类构造函数如何与Struts2一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆