Business logic in wicket, where are some recommended places to put logic (on load logic)
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.
}
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.
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.
精彩评论