ModelDriven 和 Prepare 的顺序? [英] The sequence of ModelDriven and Prepare?

查看:22
本文介绍了ModelDriven 和 Prepare 的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把 println() 放在 Action 类的每个方法中.

I put the println() in each method of Action class.

public String execute() throws Exception {
  System.out.println("execute");
  //...
 }
 public void prepare() throws Exception {
  System.out.println("prepare");
  //...
 }
 public Object getModel() {
  System.out.print("getModel");
  //...
 }

我以为顺序是;准备→执行→获取模型.

I thought the order would be; prepare → execute → getModel.

因为记得书上看过,所以以前在prepare()中构造bean类并做一些逻辑,在execute()中直接返回SUCCESS>.而且我认为 getModel 用于将 bean 推送到 valueStack,对吗?

Because I remember I read it in the book, so I used to construct beans class and do some logics in prepare(), and just return SUCCESS in execute(). And I think getModel is for pushing the bean to the valueStack, right?

...无论如何,控制台向我展示了这一点.这很奇怪;准备→获取模型→执行.

...anyway the console showed me this. It's very weird; prepare → getModel → execute.

这对我来说正在成为一个大问题.很难用英语解释原因……但我会努力的!

And this is becoming a huge problem to me. It's very hard to explain the reason in English... but I'll try!

我过去常常创建处理相同 bean 的每个动作类,当然每个动作类中都有相同的代码(变量,以及它们的 getter 和 setter).

I used to create each action class which is dealing with same beans, and of course there are same codes(variables, and their getters and setters) duplicated in each action class.

现在我正在尝试创建一个操作来避免重复.这个动作类有几个方法(像这样映射到struts.xml中;<action name="View_board" method="view">).

Now I'm trying to create one action to avoid that duplication. This action class has several methods(mapped in struts.xml like this;<action name="View_board" method="view">).

正如我们在控制台中看到的,这个 view() 在动作类的最后被调用,如 execute().prepare() 只构建 bean,而 view() 做真正的工作.但是 getModel() 在调用 view() 之前被调用,所以没有机会将 bean 放入 ValueStack.

And as we saw in the console, this view() is called at the last in the action class like execute(). The prepare() does only construct beans, and the view() does real jobs. But getModel() is called before calling of view(), so there's no chance to put the beans to ValueStack.

我希望你们能理解我要解释的内容.

I hope you guys understand what I'm trying to explain.

总而言之,有每个动作类,如 BoardView、BoardDelete、BoardWrite……而且它们运行良好!但我讨厌那些重复的东西,所以我创建了 BoardManager 类.此类具有由类(如 BoardView)处理的每个方法(如 view()).但是这个view()是在调用getModel()之后调用的,所以bean(getModel()的返回)没有机会被推送到ValueStack.

To sum it up, there are each action class like BoardView, BoardDelete, BoardWrite... and they worked well! But I hate that duplicated things so I created BoardManager class. This class has each method(like view()) which was handled by class(like BoardView). But this view() was called after the call of getModel(), so the bean(return of getModel()) has no chance to be pushed to the ValueStack.

请帮帮我.教我您在该领域的专业知识.这一切都是我自己开发的,这让我感觉很辛苦.

Please help me out. Teach me your know-how in the field. I'm developing it all on my own and this is making me feel so hard.

谢谢!!

推荐答案

您必须自己设置 Model 对象,因为模型驱动的拦截器只有在它不为 null 时才能将其压入堆栈.如果您的 getModel() 如下所示:

You have to set the Model object yourself as the modeldriven interceptor can only push it to the stack if its not null. If your getModel() looks like this:

SomeModelClass myModelObject = null;

public Object getModel()
{
   return myModelObject;
}

...您必须设置modelObject,以便将其推送到valueStack.我猜你可以这样做:

... you'll have to set the modelObject so it can get pushed to the valueStack. You could do it this way I guess:

public void prepare(){
   myModelObject = new myModelObject("I'm so new");
}

... 或者只是在字段中初始化它:

... or just initialize it in the field:

SomeModelClass myModelObject = new myModelObject("I'm so new");

不要忘记实现适当的接口(ModelDriven 和 Preparable).希望这会有所帮助.

Don't forget to implement the appropriate Interfaces (ModelDriven and Preparable). Hope this helps a bit.

这篇关于ModelDriven 和 Prepare 的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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