检票口中的业务逻辑,其中有一些推荐的放置逻辑的位置(加载逻辑)

发布于 2024-10-20 08:00:38 字数 1653 浏览 0 评论 0原文

所以,我看到这个问题被问到,但我会多一点代码:

Where is the best place to put code to initialize the model before a 页面呈现。我知道有五个选项,但是你通常把它放在哪里 这种类型的初始化?

在页面呈现之前,我想在我的 bean/model 中设置数据 某些属性可能仅特定于该页面。

我认为有五个选择。

  1. 在构造函数中添加初始化逻辑。这可能有效,但我 不知道是否为每个页面调用调用构造函数(例如,当 页面已反序列化)。

  2. 在onBeforeRender中添加初始化逻辑。这很有效,并且需要每个人 要求?但这是最好的地方吗?或者配置上? onInitialize。

然后,您是否使用更新后的值调用 setDefaultModel/setDefaultObject ?

  1. 在“load”或“getmodel”方法中添加初始化逻辑 LoadableDetachableModel 类?

  2. 在上一页的onSubmit方法或onEvent上添加init。 (onSubmit() { initBeanInSession();设置响应页面(); }

  3. 将模型传递给面板或页面构造函数(使用页面参数?)

这些最佳实践中的任何一个或优于另一个。

(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.

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

  2. 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?

  1. Add init logic in a "load" or "getmodel" method in
    LoadableDetachableModel class?

  2. Add init in previous page on onSubmit method or onEvent. (onSubmit()
    { initBeanInSession(); setResponsePage(); }

  3. 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 技术交流群。

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

发布评论

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

评论(2

安人多梦 2024-10-27 08:00:38

1 在构造函数中添加初始化逻辑。这可能有效,但我不知道是否为每个页面调用调用构造函数(例如,当页面反序列化时)。

仅当调用new时才会调用构造函数。反序列化绕过对象的正常构造(它只分配足够的内存,然后直接加载数据 - 前提是您没有为序列化/反序列化创建任何特殊代码)。使用调试器可以轻松检查此类假设。

因此,如果您想使用模型对象为每个请求执行操作,那么构造函数是错误的地方。

2 在onBeforeRender中添加init逻辑。这有效并且它要求每个请求?但这是最好的地方吗?或者配置上? on初始化。

onBeforeRender 仅针对可见组件(针对每个请求)调用。对于每个请求,所有组件(无论可见性如何)都会调用新的 onConfigure 事件。 onInitialize 仅在组件添加到其父组件后调用(即仅调用一次)。

您的问题很难给出答案,因为尚不清楚您是否打算覆盖用户在输入字段中已提供的值。如果是这样,那么您可以在 LoadableDetachableModelload 方法中执行此操作。如果没有,那么您应该在 onInitialize 中执行此操作,或者可能在 App 对象的构造函数中设置这些默认值。

对于此类设计问题,没有金锤子可以解决——它总是取决于

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

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.

2 Add init logic in onBeforeRender. This works and it called for every request? But is it the best place? Or onconfigure? onInitialize.

onBeforeRender is only called for visible components (for every request). The new onConfigure 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 the LoadableDetachableModel. If not, then you should do it in onInitialize, or possibly set those defaults in the constructor of your App object.

There are no golden hammers for such design problems—it always depends.

深白境迁sunset 2024-10-27 08:00:38

我通常将其放在 LoadableDetachableModelload() 方法中。该方法专门用于包含初始化逻辑。

但问题实际上是为什么您需要为每个页面渲染初始化它。我承认在某些情况下没有其他选择,但通常是有的。

I usually put it in the load() method of a LoadableDetachableModel. 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.

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