检票口中的业务逻辑,其中有一些推荐的放置逻辑的位置(加载逻辑)
所以,我看到这个问题被问到,但我会多一点代码:
Where is the best place to put code to initialize the model before a 页面呈现。我知道有五个选项,但是你通常把它放在哪里 这种类型的初始化?
在页面呈现之前,我想在我的 bean/model 中设置数据 某些属性可能仅特定于该页面。
我认为有五个选择。
在构造函数中添加初始化逻辑。这可能有效,但我 不知道是否为每个页面调用调用构造函数(例如,当 页面已反序列化)。
在onBeforeRender中添加初始化逻辑。这很有效,并且需要每个人 要求?但这是最好的地方吗?或者配置上? onInitialize。
然后,您是否使用更新后的值调用 setDefaultModel/setDefaultObject ?
在“load”或“getmodel”方法中添加初始化逻辑 LoadableDetachableModel 类?
在上一页的onSubmit方法或onEvent上添加init。 (onSubmit() { initBeanInSession();设置响应页面(); }
将模型传递给面板或页面构造函数(使用页面参数?)
这些最佳实践中的任何一个或优于另一个。
(a) 具有 Loadable 可拆卸模型的页面构造函数代码:
MyPage.java:
...
final Form form = new Form(FORM, new
CompoundPropertyModel(new LoadableDetachableModel() {
private static final long serialVersionUID = 1L;
@Override
protected MyBean load() {
final MyBean app = (MyBean) Session.get().getApp();
?????????????
?????????????
initialize here???????
?????????????
return app;
}
};
});
???
onBeforeRender() {
?? Add initiailize here
final MyBean app = (MyBean) Session.get().getApp();
app.setData(doSomeBusinessLogicHere)
}
或 initModel?
/**
* Called once per request on components before they are about to be rendered.
* This method should be used to configure such things as visibility and enabled flags.
*/
@Override
protected void onConfigure() {
super.onConfigure();
// Call business logic and properly set email address.
}
So, I see this question asked but I will a little bit more code:
Where is the best place to put code to initialize the model before a
page renders. I know of five options, but where do you normally put
this type of initialization?
Before a page renders, I want to set the data in my bean/model with
certain attributes that may only be specific to that page.
I think there are five options.
Add initialization logic in the constructor. This may work, but I
don't know if the constructor is called for every page call (E.g. when
the page is deserialized).Add init logic in onBeforeRender. This works and it called for every
request? But is it the best place? Or onconfigure? onInitialize.
And then, do you call setDefaultModel/setDefaultObject with the updated values?
Add init logic in a "load" or "getmodel" method in
LoadableDetachableModel class?Add init in previous page on onSubmit method or onEvent. (onSubmit()
{ initBeanInSession(); setResponsePage(); }Pass a model to a panel or page constructor (using pageparameters?)
Are any of these best practices or preferred over the other.
(a) Page Constructor code with Loadable detachable model:
MyPage.java:
...
final Form form = new Form(FORM, new
CompoundPropertyModel(new LoadableDetachableModel() {
private static final long serialVersionUID = 1L;
@Override
protected MyBean load() {
final MyBean app = (MyBean) Session.get().getApp();
?????????????
?????????????
initialize here???????
?????????????
return app;
}
};
});
???
onBeforeRender() {
?? Add initiailize here
final MyBean app = (MyBean) Session.get().getApp();
app.setData(doSomeBusinessLogicHere)
}
or initModel?
/**
* Called once per request on components before they are about to be rendered.
* This method should be used to configure such things as visibility and enabled flags.
*/
@Override
protected void onConfigure() {
super.onConfigure();
// Call business logic and properly set email address.
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仅当调用
new
时才会调用构造函数。反序列化绕过对象的正常构造(它只分配足够的内存,然后直接加载数据 - 前提是您没有为序列化/反序列化创建任何特殊代码)。使用调试器可以轻松检查此类假设。因此,如果您想使用模型对象为每个请求执行操作,那么构造函数是错误的地方。
onBeforeRender
仅针对可见组件(针对每个请求)调用。对于每个请求,所有组件(无论可见性如何)都会调用新的onConfigure
事件。onInitialize
仅在组件添加到其父组件后调用(即仅调用一次)。您的问题很难给出答案,因为尚不清楚您是否打算覆盖用户在输入字段中已提供的值。如果是这样,那么您可以在
LoadableDetachableModel
的load
方法中执行此操作。如果没有,那么您应该在onInitialize
中执行此操作,或者可能在App
对象的构造函数中设置这些默认值。对于此类设计问题,没有金锤子可以解决——它总是取决于。
A constructor is only called when invoking
new
. Deserialization bypasses normal construction of objects (it only allocates enough memory and then loads the data directly—provided you haven't created any special code for serialization/deserialization). Such assumptions are easily checked with a debugger.So if you want to do things for each request with your model object, the constructor is the wrong place.
onBeforeRender
is only called for visible components (for every request). The newonConfigure
event is called for all components (regardless of visibility) with every request.onInitialize
is only called after a component is added to its parent (i.e. just once).The answer to your question is difficult to give, because it is not clear if you intent to overwrite the values your users have already provided in their input fields. If so, then you could do it in the
load
method of theLoadableDetachableModel
. If not, then you should do it inonInitialize
, or possibly set those defaults in the constructor of yourApp
object.There are no golden hammers for such design problems—it always depends.
我通常将其放在
LoadableDetachableModel
的load()
方法中。该方法专门用于包含初始化逻辑。但问题实际上是为什么您需要为每个页面渲染初始化它。我承认在某些情况下没有其他选择,但通常是有的。
I usually put it in the
load()
method of aLoadableDetachableModel
. This method is specifically meant to contain initialisation logic.But the question really is why you need to init it for every single page rendering. I admit that in some cases there's no other option, but usually there is.