You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Michael Chandler <Mi...@onassignment.com> on 2013/02/18 18:47:16 UTC
Fundamental forms/models issue
Good morning/afternoon everyone.
I'm having a basic problem fully deciphering how to best manage my forms, specifically related to Models that are attached to forms. Since a Wicket WebPage has it's constructor invoked only one time in the application lifecycle, I'm failing to fully understand how to present a form that has a model bound to it without inadvertently sharing that instance of the Model with every user of the application. It seems like a fundamental issue that I'm failing to fully grasp and could use some input.
As an example, I have the following in my constructor:
LoadableDetachableModel<Job> jobModel = new LoadableDetachableModel<Job>() {
private static final long serialVersionUID = 1L;
@Override
protected Job load() {
Job job = (Job) EntityFactory.getInstance().getBean("job");
// if we're editing an existing job, load the object
if (jobId >= 1) {
job.load(jobId);
}
return job;
}
};
I later create a form and after adding it in my constructor, bind the model to it as follows:
jobForm.setModel(new CompoundPropertyModel<Job>(jobModel));
As you can imagine, every user session from this point on now has that instance of a Job object bound to that form due to these declarations being in the page constructor. I have come a long way on my own, but I'm at a point where I clearly do not have a full grasp of how to best approach this. I suspect I can potentially override an instance of a Model's getObject() method for more dynamic behavior, but I'm concerned about writing code that becomes too verbose when perhaps there's a better/tighter way to handle this. Can anyone advise me?
Many thanks!
Mike Chandler
Re: Fundamental forms/models issue
Posted by Carl-Eric Menzel <cm...@wicketbuch.de>.
On Mon, 18 Feb 2013 17:47:16 +0000
Michael Chandler <Mi...@onassignment.com> wrote:
> Good morning/afternoon everyone.
>
> I'm having a basic problem fully deciphering how to best manage my
> forms, specifically related to Models that are attached to forms.
> Since a Wicket WebPage has it's constructor invoked only one time in
> the application lifecycle, I'm failing to fully understand how to
> present a form that has a model bound to it without inadvertently
> sharing that instance of the Model with every user of the
> application. It seems like a fundamental issue that I'm failing to
> fully grasp and could use some input.
There is the mistake: Page instances are *not* shared. Every user has
their own instances. There can be plenty of instances of any page at
any given time.
Pages get constructed any time *you* do it (by calling new MyPage(...))
or Wicket does it (when the user first gets to a bookmarkable page,
Wicket will construct a fresh instance by using either the no-arg
constructor or the PageParameters constructor).
Carl-Eric
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
RE: Fundamental forms/models issue
Posted by Hans Lesmeister 2 <ha...@lessy-software.de>.
Hi Michael,
Michael Chandler wrote
> To conclude, my issues stemmed from not properly linking from page to
> page. I did a lot of this:
>
> BookmarkablePageLink
> <String>
> ("link", MyClass.class);
>
> Instead of this:
>
> Link
> <String>
> ("link") {
> public void onClick() {
> setResponsePage(new MyClass());
> }
> }
>
> Oopsy!
Oopsy is not necessary :-) Depending on the Use Case, both ways are fully
okay.
If you pass MyClass.class then, as stated before, Wicket creates a fresh
page for you. If you pass an instance, then wicket uses that instance. The
2nd approach can for instance be used if you have state (i.e. a model or
something else) to pass to the new page, but not necessarily.
-----
--
Regards,
Hans
http://cantaa.de
--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Fundamental-forms-models-issue-tp4656511p4656537.html
Sent from the Users forum mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
RE: Fundamental forms/models issue
Posted by Michael Chandler <Mi...@onassignment.com>.
To conclude, my issues stemmed from not properly linking from page to page. I did a lot of this:
BookmarkablePageLink<String>("link", MyClass.class);
Instead of this:
Link<String>("link") {
public void onClick() {
setResponsePage(new MyClass());
}
}
Oopsy!
I can see how the former might benefit me down the road, but not for general use.
Thanks again everyone.
Mike
-----Original Message-----
From: Michael Chandler [mailto:Michael.Chandler@onassignment.com]
Sent: Monday, February 18, 2013 10:35 AM
To: users@wicket.apache.org
Subject: RE: Fundamental forms/models issue
Oh my gosh, Paul. You just nailed my problem I think.
I'm doing this:
setResponsePage(MyPage.class);
Instead of this:
setResponsePage(new MyPage());
Thanks for the link. Clearly, I have more reading to do!
Thanks all!
Mike
-----Original Message-----
From: Paul Bors [mailto:paul@bors.ws]
Sent: Monday, February 18, 2013 10:08 AM
To: users@wicket.apache.org
Subject: Re: Fundamental forms/models issue
What are you really trying to accomplish here?
I think you're on the right path only one thing I would mention, the page constructor is not called only once per application life cycle. Same page can be constructed multiple times if you have a link going to that page and you calll setResponsePage(new MyPage()).
More on detachable models:
https://cwiki.apache.org/WICKET/detachable-models.html
~ Thank you,
Paul Bors
On Mon, Feb 18, 2013 at 12:47 PM, Michael Chandler < Michael.Chandler@onassignment.com> wrote:
> Good morning/afternoon everyone.
>
> I'm having a basic problem fully deciphering how to best manage my
> forms, specifically related to Models that are attached to forms.
> Since a Wicket WebPage has it's constructor invoked only one time in
> the application lifecycle, I'm failing to fully understand how to
> present a form that has a model bound to it without inadvertently
> sharing that instance of the Model with every user of the application.
> It seems like a fundamental issue that I'm failing to fully grasp and could use some input.
>
> As an example, I have the following in my constructor:
>
> LoadableDetachableModel<Job> jobModel = new
> LoadableDetachableModel<Job>() {
>
> private static final long serialVersionUID = 1L;
>
> @Override
> protected Job load() {
> Job job = (Job) EntityFactory.getInstance().getBean("job");
>
> // if we're editing an existing job, load the object
> if (jobId >= 1) {
> job.load(jobId);
> }
>
> return job;
> }
>
> };
>
> I later create a form and after adding it in my constructor, bind the
> model to it as follows:
>
> jobForm.setModel(new CompoundPropertyModel<Job>(jobModel));
>
> As you can imagine, every user session from this point on now has that
> instance of a Job object bound to that form due to these declarations
> being in the page constructor. I have come a long way on my own, but
> I'm at a point where I clearly do not have a full grasp of how to best
> approach this. I suspect I can potentially override an instance of a
> Model's
> getObject() method for more dynamic behavior, but I'm concerned about
> writing code that becomes too verbose when perhaps there's a
> better/tighter way to handle this. Can anyone advise me?
>
> Many thanks!
>
> Mike Chandler
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Fundamental forms/models issue
Posted by Paul Bors <pa...@bors.ws>.
Glad I could help, below is the API for Wicket 5's setResponsePage:
http://wicket.apache.org/apidocs/1.5/org/apache/wicket/Component.html
<C extends IRequestablePage<http://wicket.apache.org/apidocs/1.5/org/apache/wicket/request/component/IRequestablePage.html>>
void*setResponsePage<http://wicket.apache.org/apidocs/1.5/org/apache/wicket/Component.html#setResponsePage(java.lang.Class)>
*(java.lang.Class<C> cls)
Sets the page that will respond to this request<C extends
IRequestablePage<http://wicket.apache.org/apidocs/1.5/org/apache/wicket/request/component/IRequestablePage.html>>
void*setResponsePage<http://wicket.apache.org/apidocs/1.5/org/apache/wicket/Component.html#setResponsePage(java.lang.Class,
org.apache.wicket.request.mapper.parameter.PageParameters)>*(java.lang.Class<C>
cls, PageParameters<http://wicket.apache.org/apidocs/1.5/org/apache/wicket/request/mapper/parameter/PageParameters.html>parameters)
Sets the page class and its parameters that will respond to this requestvoid
*setResponsePage<http://wicket.apache.org/apidocs/1.5/org/apache/wicket/Component.html#setResponsePage(org.apache.wicket.Page)>
*(Page <http://wicket.apache.org/apidocs/1.5/org/apache/wicket/Page.html>page)
Sets the page that will respond to this request
What that dosen't mention is what page instance is used or when/how a new
Page is constructed.
Give the Page maps a read to grasp that concept:
https://cwiki.apache.org/WICKET/page-maps.html
~ Thank you,
Paul Bors
PS: You could try purchasing an older version of Wicket in Action or some
other book too, that's how I picked up Wicket :)
http://wicket.apache.org/learn/books/
On Mon, Feb 18, 2013 at 1:34 PM, Michael Chandler <
Michael.Chandler@onassignment.com> wrote:
> Oh my gosh, Paul. You just nailed my problem I think.
>
> I'm doing this:
>
> setResponsePage(MyPage.class);
>
> Instead of this:
>
> setResponsePage(new MyPage());
>
> Thanks for the link. Clearly, I have more reading to do!
>
> Thanks all!
>
> Mike
>
> -----Original Message-----
> From: Paul Bors [mailto:paul@bors.ws]
> Sent: Monday, February 18, 2013 10:08 AM
> To: users@wicket.apache.org
> Subject: Re: Fundamental forms/models issue
>
> What are you really trying to accomplish here?
>
>
>
> I think you're on the right path only one thing I would mention, the page
> constructor is not called only once per application life cycle. Same page
> can be constructed multiple times if you have a link going to that page and
> you calll setResponsePage(new MyPage()).
>
>
>
> More on detachable models:
>
> https://cwiki.apache.org/WICKET/detachable-models.html
>
>
>
> ~ Thank you,
>
> Paul Bors
>
>
>
> On Mon, Feb 18, 2013 at 12:47 PM, Michael Chandler <
> Michael.Chandler@onassignment.com> wrote:
>
> > Good morning/afternoon everyone.
> >
> > I'm having a basic problem fully deciphering how to best manage my
> > forms, specifically related to Models that are attached to forms.
> > Since a Wicket WebPage has it's constructor invoked only one time in
> > the application lifecycle, I'm failing to fully understand how to
> > present a form that has a model bound to it without inadvertently
> > sharing that instance of the Model with every user of the application.
> > It seems like a fundamental issue that I'm failing to fully grasp and
> could use some input.
> >
> > As an example, I have the following in my constructor:
> >
> > LoadableDetachableModel<Job> jobModel = new
> > LoadableDetachableModel<Job>() {
> >
> > private static final long serialVersionUID = 1L;
> >
> > @Override
> > protected Job load() {
> > Job job = (Job) EntityFactory.getInstance().getBean("job");
> >
> > // if we're editing an existing job, load the object
> > if (jobId >= 1) {
> > job.load(jobId);
> > }
> >
> > return job;
> > }
> >
> > };
> >
> > I later create a form and after adding it in my constructor, bind the
> > model to it as follows:
> >
> > jobForm.setModel(new CompoundPropertyModel<Job>(jobModel));
> >
> > As you can imagine, every user session from this point on now has that
> > instance of a Job object bound to that form due to these declarations
> > being in the page constructor. I have come a long way on my own, but
> > I'm at a point where I clearly do not have a full grasp of how to best
> > approach this. I suspect I can potentially override an instance of a
> > Model's
> > getObject() method for more dynamic behavior, but I'm concerned about
> > writing code that becomes too verbose when perhaps there's a
> > better/tighter way to handle this. Can anyone advise me?
> >
> > Many thanks!
> >
> > Mike Chandler
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>
RE: Fundamental forms/models issue
Posted by Michael Chandler <Mi...@onassignment.com>.
Oh my gosh, Paul. You just nailed my problem I think.
I'm doing this:
setResponsePage(MyPage.class);
Instead of this:
setResponsePage(new MyPage());
Thanks for the link. Clearly, I have more reading to do!
Thanks all!
Mike
-----Original Message-----
From: Paul Bors [mailto:paul@bors.ws]
Sent: Monday, February 18, 2013 10:08 AM
To: users@wicket.apache.org
Subject: Re: Fundamental forms/models issue
What are you really trying to accomplish here?
I think you're on the right path only one thing I would mention, the page constructor is not called only once per application life cycle. Same page can be constructed multiple times if you have a link going to that page and you calll setResponsePage(new MyPage()).
More on detachable models:
https://cwiki.apache.org/WICKET/detachable-models.html
~ Thank you,
Paul Bors
On Mon, Feb 18, 2013 at 12:47 PM, Michael Chandler < Michael.Chandler@onassignment.com> wrote:
> Good morning/afternoon everyone.
>
> I'm having a basic problem fully deciphering how to best manage my
> forms, specifically related to Models that are attached to forms.
> Since a Wicket WebPage has it's constructor invoked only one time in
> the application lifecycle, I'm failing to fully understand how to
> present a form that has a model bound to it without inadvertently
> sharing that instance of the Model with every user of the application.
> It seems like a fundamental issue that I'm failing to fully grasp and could use some input.
>
> As an example, I have the following in my constructor:
>
> LoadableDetachableModel<Job> jobModel = new
> LoadableDetachableModel<Job>() {
>
> private static final long serialVersionUID = 1L;
>
> @Override
> protected Job load() {
> Job job = (Job) EntityFactory.getInstance().getBean("job");
>
> // if we're editing an existing job, load the object
> if (jobId >= 1) {
> job.load(jobId);
> }
>
> return job;
> }
>
> };
>
> I later create a form and after adding it in my constructor, bind the
> model to it as follows:
>
> jobForm.setModel(new CompoundPropertyModel<Job>(jobModel));
>
> As you can imagine, every user session from this point on now has that
> instance of a Job object bound to that form due to these declarations
> being in the page constructor. I have come a long way on my own, but
> I'm at a point where I clearly do not have a full grasp of how to best
> approach this. I suspect I can potentially override an instance of a
> Model's
> getObject() method for more dynamic behavior, but I'm concerned about
> writing code that becomes too verbose when perhaps there's a
> better/tighter way to handle this. Can anyone advise me?
>
> Many thanks!
>
> Mike Chandler
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Fundamental forms/models issue
Posted by Paul Bors <pa...@bors.ws>.
What are you really trying to accomplish here?
I think you're on the right path only one thing I would mention, the page
constructor is not called only once per application life cycle. Same page
can be constructed multiple times if you have a link going to that page
and you calll setResponsePage(new MyPage()).
More on detachable models:
https://cwiki.apache.org/WICKET/detachable-models.html
~ Thank you,
Paul Bors
On Mon, Feb 18, 2013 at 12:47 PM, Michael Chandler <
Michael.Chandler@onassignment.com> wrote:
> Good morning/afternoon everyone.
>
> I'm having a basic problem fully deciphering how to best manage my forms,
> specifically related to Models that are attached to forms. Since a Wicket
> WebPage has it's constructor invoked only one time in the application
> lifecycle, I'm failing to fully understand how to present a form that has a
> model bound to it without inadvertently sharing that instance of the Model
> with every user of the application. It seems like a fundamental issue that
> I'm failing to fully grasp and could use some input.
>
> As an example, I have the following in my constructor:
>
> LoadableDetachableModel<Job> jobModel = new LoadableDetachableModel<Job>()
> {
>
> private static final long serialVersionUID = 1L;
>
> @Override
> protected Job load() {
> Job job = (Job) EntityFactory.getInstance().getBean("job");
>
> // if we're editing an existing job, load the object
> if (jobId >= 1) {
> job.load(jobId);
> }
>
> return job;
> }
>
> };
>
> I later create a form and after adding it in my constructor, bind the
> model to it as follows:
>
> jobForm.setModel(new CompoundPropertyModel<Job>(jobModel));
>
> As you can imagine, every user session from this point on now has that
> instance of a Job object bound to that form due to these declarations being
> in the page constructor. I have come a long way on my own, but I'm at a
> point where I clearly do not have a full grasp of how to best approach
> this. I suspect I can potentially override an instance of a Model's
> getObject() method for more dynamic behavior, but I'm concerned about
> writing code that becomes too verbose when perhaps there's a better/tighter
> way to handle this. Can anyone advise me?
>
> Many thanks!
>
> Mike Chandler
>
>
Re: Fundamental forms/models issue
Posted by Sven Meier <sv...@meiers.net>.
> Since a Wicket WebPage has it's constructor invoked only one time in the application lifecycle
Usually a new page instance is created each time you visit a bookmarkable url. WebPages are never shared between clients.
Sven
On 02/18/2013 06:47 PM, Michael Chandler wrote:
> Good morning/afternoon everyone.
>
> I'm having a basic problem fully deciphering how to best manage my forms, specifically related to Models that are attached to forms. Since a Wicket WebPage has it's constructor invoked only one time in the application lifecycle, I'm failing to fully understand how to present a form that has a model bound to it without inadvertently sharing that instance of the Model with every user of the application. It seems like a fundamental issue that I'm failing to fully grasp and could use some input.
>
> As an example, I have the following in my constructor:
>
> LoadableDetachableModel<Job> jobModel = new LoadableDetachableModel<Job>() {
>
> private static final long serialVersionUID = 1L;
>
> @Override
> protected Job load() {
> Job job = (Job) EntityFactory.getInstance().getBean("job");
>
> // if we're editing an existing job, load the object
> if (jobId >= 1) {
> job.load(jobId);
> }
>
> return job;
> }
>
> };
>
> I later create a form and after adding it in my constructor, bind the model to it as follows:
>
> jobForm.setModel(new CompoundPropertyModel<Job>(jobModel));
>
> As you can imagine, every user session from this point on now has that instance of a Job object bound to that form due to these declarations being in the page constructor. I have come a long way on my own, but I'm at a point where I clearly do not have a full grasp of how to best approach this. I suspect I can potentially override an instance of a Model's getObject() method for more dynamic behavior, but I'm concerned about writing code that becomes too verbose when perhaps there's a better/tighter way to handle this. Can anyone advise me?
>
> Many thanks!
>
> Mike Chandler
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org