简体   繁体   English

Struts2-如何在页面加载时调用prepare()方法?

[英]Struts2 - How do I invoke the prepare() method on page load?

I want to invoke the prepare() method when a page loads in Struts 2. 我想在Struts 2中加载页面时调用prepare()方法。

It is my understanding that if I implement the Preparable interface on my action that the prepare() method should automatically fire when the page loads. 据我了解,如果我在操作上实现Preparable接口,则在页面加载时prepare()方法应自动触发。 Is this correct? 这个对吗? Or is there any other configuration required? 还是需要其他配置?

I ask because this is what I have done and the prepare method only fires upon a button click, just before the execute method fires. 我问,因为这是我所做的,并且prepare方法仅在单击execute方法之前就在单击按钮时触发。

I want the prepare method to fire just after/before the page loads to populate an attribute on the action. 我希望prepare方法在页面加载之后/之前触发,以填充操作上的属性。 How can I do this? 我怎样才能做到这一点?

prepare() executes when a Preparable action is executed. prepare()执行Preparable动作时执行。

If it's not firing, then your prepare() method is either in the wrong action, you're not accessing the page through an action initially (eg, hitting a plain JSP), or there's an interceptor stack configuration issue. 如果没有触发,则说明您的prepare()方法执行错误,或者您最初没有通过某个操作(例如,击中普通的JSP)访问该页面,或者存在拦截器堆栈配置问题。

It is my understanding that if I implement the Preparable interface on my action that the prepare() method should automatically fire when the page loads. 据我了解,如果我在操作上实现Preparable接口,则在页面加载时prepare()方法应自动触发。

As per my understanding this is not correct . 根据我的理解, 这是不正确的 The prepare method is used to prepare some data to be used by the execute() method or any other worker method of the Action class and is thus called just before the worker method on the request from the Browser. prepare方法用于准备一些要由Action类的execute()方法或任何其他工作方法使用的数据,因此在浏览器的请求之前,在工作方法之前被调用。

Invoking the prepare() method on page load would be programatically cumbersome and thus should be avoided. 在页面加载时调用prepare()方法在程序上很麻烦,因此应避免。 Try setting the attribute in other way 尝试以其他方式设置属性

Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM