Hi,

Most people use the session approach. Keep an id (private key) as a field
in the Wicket Session and load the real object on demand when needed.

Similar solution is to make your bean Session scoped. The DI framework
cares to extract it from the session for you.

Some people prefer to be as stateless as possible and avoid using the
session if possible. In this case you can use setResponsePage(Class,
PageParameters) and transfer the id as a request parameter from page to
page.

The "problem" with setResponsePage(Page) is that it makes the page stateful
and the "ugly" ?pageId appears in the url. If this doesn't bother you then
it is very simple solution for your problem.



On Tue, Jan 29, 2013 at 2:40 PM, Bill Speirs <bill.spe...@gmail.com> wrote:

> OK... but how do I pass the current UserBean from one page to another? I
> cannot bind it to Guice as I don't know what it is when I'm setting up my
> Guice Module.
>
> Currently I used setRedirectPage(new MyPage(currentUser)). From what I
> understand you're saying I can switch to setRedirectPage(MyPage.class), but
> then how do I set the UserBean "property" for that instance?
>
> Should these types of values be passed via the session instead? So I simply
> use setRedirectPage(MyPage.class), then inside that class get the current
> user from the session?
>
> The overall question is, what is the best practice for passing values to a
> WebPage from another WebPage via setRedirectPage()? Should you create a new
> instance of the page, or let Wicket do it via reflection? And if Wicket is
> doing it, then how do I "inject" the value?
>
> Thanks...
>
> Bill-
>
>
> On Tue, Jan 29, 2013 at 2:50 AM, Martin Grigorov <mgrigo...@apache.org
> >wrote:
>
> > Hi,
> >
> > You can do something like:
> >
> > class MyPage extends WebPage {
> >
> > @Inject private DAO dao;
> >
> > @Inject @Nullable private UserBean currentUser;
> >
> > ... // use dao or currentUser anywhere in your class
> >
> > }
> >
> > And Guice will try to inject these beans if they are defined in a Guice
> > module.
> > See https://code.google.com/p/google-guice/wiki/UseNullable
> >
> >
> > On Tue, Jan 29, 2013 at 2:27 AM, Bill Speirs <bill.spe...@gmail.com>
> > wrote:
> >
> > > Still struggling with how to do this. Martin, I understand that most
> > people
> > > will simply call new PanelA and new PanelB inside their MyPage code
> > instead
> > > of trying to inject them. However, how does one setup a page that
> > requires
> > > some service (DAO for example) and that might optionally require a
> > > UserBean.
> > >
> > > So without using DI, I'd simply have 2 constructors:
> > >
> > > MyPage(DAO myDao)
> > >
> > > MyPage(DAO myDao, UserBean currentUser)
> > >
> > > But I cannot create that second constructor as I don't have anything to
> > > bind (using Guice terms) UserBean to.
> > >
> > > Am I required to make a factory for MyPage at that point? Is it
> bad/wrong
> > > to pass the UserBean into the constructor via a response page:
> > >
> > > setResponsePage(new MyPage(new DAO(), currentUser));
> > >
> > > Thanks...
> > >
> > > Bill-
> > >
> > >
> > > On Fri, Nov 30, 2012 at 3:35 AM, Martin Grigorov <mgrigo...@apache.org
> > > >wrote:
> > >
> > > > Hi,
> > > >
> > > > Most of the time people inject services to their components.
> > > >
> > > > To test just components you can use
> WicketTester#startComponentInPage()
> > > > methods.
> > > >
> > > >
> > > > On Fri, Nov 30, 2012 at 3:18 AM, William Speirs <wspe...@apache.org>
> > > > wrote:
> > > >
> > > > > I'm having trouble understanding how to inject components into a
> page
> > > so
> > > > > that the page will be easy to unit test later. Say I have a page
> that
> > > > > contains two panels. I can easily use constructor injection to
> inject
> > > > these
> > > > > panels into the page:
> > > > >
> > > > > class MyPage extends WebPage {
> > > > > @Inject
> > > > > public MyPage(PanelA a, PanelB b) { ... }
> > > > > }
> > > > >
> > > > > The problem is that all Panels require an id during
> construction.[1]
> > > How
> > > > do
> > > > > I supply the id to my Panels? I could simply construct every PanelA
> > > with
> > > > an
> > > > > id of "panela" and every PanelB with an id of "panel", but that
> > doesn't
> > > > > seem very flexible. What do other people do in this situation? The
> > hope
> > > > > would be to pass mocked panels into the page during unit testing,
> > > > > the separately test each panel.
> > > > >
> > > > > What if instead of a panel it was a button where the onSubmit
> method
> > > must
> > > > > be specified by overriding the method. How does one go about
> > injecting
> > > > such
> > > > > a component so that it's still easy to test later in unit tests?
> > > > >
> > > > > All thoughts and/or best practices are greatly welcomed. For
> > reference
> > > > I'm
> > > > > using Guice as my dependency injection framework and
> > > > > GuiceWebApplicationFactory to inject components into pages.
> > > > >
> > > > > Thanks...
> > > > >
> > > > > Bill-
> > > > >
> > > > > [1]
> > > > >
> > > > >
> > > >
> > >
> >
> http://ci.apache.org/projects/wicket/apidocs/6.0.x/org/apache/wicket/markup/html/panel/Panel.html
> > > > >
> > > >
> > > >
> > > >
> > > > --
> > > > Martin Grigorov
> > > > jWeekend
> > > > Training, Consulting, Development
> > > > http://jWeekend.com <http://jweekend.com/>
> > > >
> > >
> >
> >
> >
> > --
> > Martin Grigorov
> > jWeekend
> > Training, Consulting, Development
> > http://jWeekend.com <http://jweekend.com/>
> >
>



-- 
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com <http://jweekend.com/>

Reply via email to