You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by armhold <ar...@gmail.com> on 2012/04/09 16:24:34 UTC
a model for passing data between pages
I have a use case where several pages of a flow need to edit a
DTO. Changes to the DTO (which may be an existing JPA Entity, or a
not-yet-persisted one) are saved at the end of the flow (assuming the
user clicks "save").
The user is allowed to navigate back-and-forth among the pages, so the
obvious trick of passing the modified DTO to the next page via its
constructor won't work- the back button will lead to a page with a
potentially stale DTO.
I'm thinking of storing the DTO in the user's session as a detached
entity, and using a model like the following on the various pages:
public class SessionModel<T extends Serializable> implements IModel<T>
{
protected String key;
public SessionModel(String key)
{
this.key = key;
}
public SessionModel(String key, T object)
{
this.key = key;
setObject(object);
}
public T getObject()
{
return (T) Session.get().getAttribute(key);
}
public void setObject(T object)
{
Session.get().setAttribute(key, object);
}
public void detach()
{
// no-op
}
}
Does this seem like a reasonable approach? Is there an existing model
that I've overlooked that does this already? Is there a better way to
accomplish the multi-page flow without stuffing objects into the
session?
Thanks
--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/a-model-for-passing-data-between-pages-tp4542878p4542878.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: a model for passing data between pages
Posted by armhold <ar...@gmail.com>.
Hi Dan,
Thanks, I didn't know about MetaDataKey- that cleans it up nicely:
public class SessionModel<T extends Serializable> implements IModel<T>
{
protected MetaDataKey<T> key;
public SessionModel(MetaDataKey<T> key)
{
this.key = key;
}
public SessionModel(MetaDataKey<T> key, T object)
{
this.key = key;
setObject(object);
}
public T getObject()
{
return Session.get().getMetaData(key);
}
public void setObject(T object)
{
Session.get().setMetaData(key, object);
}
public void detach()
{
// no-op
}
}
And then in MyDTO:
public static final MetaDataKey KEY = new MetaDataKey<MyDTO>() { };
--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/a-model-for-passing-data-between-pages-tp4542878p4545388.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: a model for passing data between pages
Posted by Dan Retzlaff <dr...@gmail.com>.
You can get type safety and loose-coupling with o.a.w.Session's metadata
support. This is our constructor for such a model:
SessionMetaDataModel(T defaultObject, MetaDataKey<T> key)
The implementation just uses Session#setMetaData() and
Session#getMetaData().
On Mon, Apr 9, 2012 at 5:34 PM, Carl-Eric Menzel <cm...@wicketbuch.de>wrote:
> On Mon, 9 Apr 2012 16:23:38 -0700 (PDT)
> armhold <ar...@gmail.com> wrote:
>
> > SessionModel<MyDTO> model = new SessionModel<MyDTO>(MyDTO.KEY, new
> > MyDTO());
> >
> > Not typesafe, as you pointed out, but fairly concise.
>
> If you have a lot of different types where you use that, this map-like
> approach is probably better, yes.
>
> > > Alternatively you might want to look at the wicket-seam integration
> > > and use Seam conversations.
> >
> > Actually we started out using Wicket-CDI + Seam, expressly because of
> > the conversation support, but ran into issues making it work on
> > WebSphere... a topic for another day. :-)
>
> Heh. WebSphere, a fountain of constant joy :-)
>
> Carl-Eric
> www.wicketbuch.de
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>
Re: a model for passing data between pages
Posted by Carl-Eric Menzel <cm...@wicketbuch.de>.
On Mon, 9 Apr 2012 16:23:38 -0700 (PDT)
armhold <ar...@gmail.com> wrote:
> SessionModel<MyDTO> model = new SessionModel<MyDTO>(MyDTO.KEY, new
> MyDTO());
>
> Not typesafe, as you pointed out, but fairly concise.
If you have a lot of different types where you use that, this map-like
approach is probably better, yes.
> > Alternatively you might want to look at the wicket-seam integration
> > and use Seam conversations.
>
> Actually we started out using Wicket-CDI + Seam, expressly because of
> the conversation support, but ran into issues making it work on
> WebSphere... a topic for another day. :-)
Heh. WebSphere, a fountain of constant joy :-)
Carl-Eric
www.wicketbuch.de
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: a model for passing data between pages
Posted by armhold <ar...@gmail.com>.
Hi duesenklipper.
> I'd rather use a custom Session subclass with typesafe getters and
> setters:
> class MySession extends WebSession {
> private MyDTO dtoForFlow;
> public MyDTO getDtoForFlow() {
> return dtoForFlow;
> }
Typesafety via custom sessions is nice, but I think that would require
custom model classes for each kind of DTO in order to reach into the
session, like:
public class MyDTOModel implements IModel<MyDTO>
{
public MyDTO getObject()
{
return ((MySession) Session.get()).getDtoForFlow();
}
public void setObject(MyDTO object)
{
((MySession) Session.get()).setDtoForFlow(object);
}
// ...
}
vs:
SessionModel<MyDTO> model = new SessionModel<MyDTO>(MyDTO.KEY, new MyDTO());
Not typesafe, as you pointed out, but fairly concise.
> Alternatively you might want to look at the wicket-seam integration
> and use Seam conversations.
Actually we started out using Wicket-CDI + Seam, expressly because of
the conversation support, but ran into issues making it work on
WebSphere... a topic for another day. :-)
Thanks for your help.
--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/a-model-for-passing-data-between-pages-tp4542878p4544145.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: a model for passing data between pages
Posted by Carl-Eric Menzel <cm...@wicketbuch.de>.
On Mon, 9 Apr 2012 07:24:34 -0700 (PDT)
armhold <ar...@gmail.com> wrote:
> I'm thinking of storing the DTO in the user's session as a detached
> entity, and using a model like the following on the various pages:
That is perfectly valid approach, and one that we've been using for
some usecases. However, instead of this:
> return (T) Session.get().getAttribute(key);
I'd rather use a custom Session subclass with typesafe getters and
setters:
class MySession extends WebSession {
private MyDTO dtoForFlow;
public MyDTO getDtoForFlow() {
return dtoForFlow;
}
// ...setter too...
public static MySession get() {
return (MySession) Session.get();
}
}
Alternatively you might want to look at the wicket-seam integration and
use Seam conversations.
Carl-Eric
www.wicketbuch.de
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org