ModelDriven和Prepare的顺序?

发布于 2024-09-13 15:22:34 字数 1309 浏览 7 评论 0原文

我将 println() 放在 Action 类的每个方法中。

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");
  //...
 }

我认为顺序是; 准备→执行→获取模型。

因为我记得我在书上看过,所以我习惯了构造bean类并在prepare()中做一些逻辑,然后在execute()中返回SUCCESS。 我认为 getModel 是为了将 bean 推送到 valueStack,对吗?

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

这对我来说正在成为一个大问题。很难用英语解释原因......但我会尝试!

我曾经创建处理相同 bean 的每个操作类,当然每个操作类中都有重复的相同代码(变量及其 getter 和 setter)。

现在我正在尝试创建一个操作来避免重复。这个action类有几个方法(在struts.xml中映射如下;)。

正如我们在控制台中看到的,这个 view() 在操作类的最后被调用,就像 execute() 一样。 prepare() 只构造 bean,而 view() 则执行实际工作。但是 getModel() 在调用 view() 之前被调用,因此没有机会将 bean 放入 ValueStack。

我希望你们能理解我想解释的内容。

总而言之,BoardView、BoardDelete、BoardWrite 等每个操作类都运行良好!但我讨厌重复的事情,所以我创建了 BoardManager 类。该类具有由类(如 BoardView)处理的每个方法(如 view())。但是这个view()是在调用getModel()之后调用的,因此bean(getModel()的返回)没有机会被推送到ValueStack。

请帮帮我。教我您在该领域的专业知识。我正在自己开发这一切,这让我感到非常困难。

谢谢你!!

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.

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!

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.

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">).

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.

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.

Thank you!!

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

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

发布评论

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

评论(2

メ斷腸人バ 2024-09-20 15:22:43

我找到了自己的解决方案..但不是一个好的解决方案..

当通过setter设置域对象时,我手动将其推送到值堆栈..

它工作得很好,只在我的类中添加了2行。

但我感觉不太好。

I found my own solution.. but not a good one..

When setting domain object by setter, I push it to the valuestack manually..

It works good and only 2 lines added to my class.

But I don't feel that good.

你另情深 2024-09-20 15:22:41

您必须自己设置模型对象,因为模型驱动拦截器只能将其推入堆栈(如果它不为空)。如果你的 getModel() 看起来像这样:

SomeModelClass myModelObject = null;

public Object getModel()
{
   return myModelObject;
}

...你必须设置 modelObject 以便它可以被推送到 valueStack。我猜你可以这样做:

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

...或者只是在现场初始化它:

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

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

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;
}

... 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");

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

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