ModelDriven和Prepare的顺序?
我将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了自己的解决方案..但不是一个好的解决方案..
当通过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.
您必须自己设置模型对象,因为模型驱动拦截器只能将其推入堆栈(如果它不为空)。如果你的 getModel() 看起来像这样:
...你必须设置 modelObject 以便它可以被推送到 valueStack。我猜你可以这样做:
...或者只是在现场初始化它:
不要忘记实现适当的接口(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:
... you'll have to set the modelObject so it can get pushed to the valueStack. You could do it this way I guess:
... or just initialize it in the field:
Don't forget to implement the appropriate Interfaces (ModelDriven and Preparable). Hope this helps a bit.