You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by quizzical <qu...@gmail.com> on 2008/12/05 01:50:38 UTC

@SpringBean with page creation patterns

Hi Everyone,

I'm just getting started with wicket and am learning by playing around with
building an ecommerce application on top of wicket, spring and hibernate.
I've been (Ha!) using @SpringBean to inject my spring dependencies and am
very impressed with it.

One thing I'm wondering about is how to use @SpringBean with a pattern in
the 'wicket in action' book. Specifically, when creating a
bookmarkablePageLink with pageparameters this pattern is used:

public class CheeseDetailsPage extends WebPage {
    // bookmarkable constructor
    public CheeseDetailsPage(PageParameters parameters) {   #1
        this(getCheese(parameters));
    }
    // non-bookmarkable constructor
    public CheeseDetailsPage(Cheese cheese) {         #2
        // do cheesy stuff with the cheese
    }
    /** Retrieves a cheese object based on the ‘name’ parameter. */
    public static Cheese getCheese(PageParameters parameters) { #3
        String name = parameters.getString("name", "");
        if("".equals(name)) return null;
        CheeseDao dao = ...;
        return dao.getCheeseByName(name);
    }
}

I like this pattern but i'm having a problem with injecting my dependencies.
The getCheese() method is static, which means that the CheeseDao would have
to be static. I haven't delved into the code of the @SpringBean proxy or the
wicket serialization code (I have read the wiki on @SpringBean) and I don't
have an amazing understanding of proxies so maybe i'm just being obtuse but
heres my question.

Is there any reason not to make the DAO static?

Cheers in advance

-- 
View this message in context: http://www.nabble.com/%40SpringBean-with-page-creation-patterns-tp20845794p20845794.html
Sent from the Wicket - User 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: @SpringBean with page creation patterns

Posted by jWeekend <jw...@cabouge.com>.
I assume the getCheese method in your code below is declared public static to
keep the example simple and to the point; you wouldn't normally expect to
get your cheeses from a Page class.
If you're comfortable with your page being coupled to the DAO interface
(without, for instance, a service layer in between), you can just inject the
DAO into your Component (Page) using 
http://cwiki.apache.org/WICKET/spring.html Wicket's @SpringBean annotation 
and have a 
    private Cheese getCheese(String name)
method that uses the DAO (refactor out your getting the name form a
PageParameters into a separate method as well). 
Since you are following Wicket In Action, check out the sections on
detachable models and layered architectures.

@SpringBean
private ICheeseDao dao;

Regards - Cemal
http://www.jWeekend.co.uk http://jWeekend.co.uk 




quizzical wrote:
> 
> Hi Everyone,
> 
> I'm just getting started with wicket and am learning by playing around
> with building an ecommerce application on top of wicket, spring and
> hibernate. I've been (Ha!) using @SpringBean to inject my spring
> dependencies and am very impressed with it.
> 
> One thing I'm wondering about is how to use @SpringBean with a pattern in
> the 'wicket in action' book. Specifically, when creating a
> bookmarkablePageLink with pageparameters this pattern is used:
> 
> public class CheeseDetailsPage extends WebPage {
>     // bookmarkable constructor
>     public CheeseDetailsPage(PageParameters parameters) {   #1
>         this(getCheese(parameters));
>     }
>     // non-bookmarkable constructor
>     public CheeseDetailsPage(Cheese cheese) {         #2
>         // do cheesy stuff with the cheese
>     }
>     /** Retrieves a cheese object based on the ‘name’ parameter. */
>     public static Cheese getCheese(PageParameters parameters) { #3
>         String name = parameters.getString("name", "");
>         if("".equals(name)) return null;
>         CheeseDao dao = ...;
>         return dao.getCheeseByName(name);
>     }
> }
> 
> I like this pattern but i'm having a problem with injecting my
> dependencies. The getCheese() method is static, which means that the
> CheeseDao would have to be static. I haven't delved into the code of the
> @SpringBean proxy or the wicket serialization code (I have read the wiki
> on @SpringBean) and I don't have an amazing understanding of proxies so
> maybe i'm just being obtuse but heres my question.
> 
> Is there any reason not to make the DAO static?
> 
> Cheers in advance
> 
> 

-- 
View this message in context: http://www.nabble.com/%40SpringBean-with-page-creation-patterns-tp20845794p20854319.html
Sent from the Wicket - User 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: @SpringBean with page creation patterns

Posted by quizzical <qu...@gmail.com>.
Thanks, that was exactly what I was looking for.
-- 
View this message in context: http://www.nabble.com/%40SpringBean-with-page-creation-patterns-tp20845794p20854804.html
Sent from the Wicket - User 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: @SpringBean with page creation patterns

Posted by Jeremy Thomerson <je...@wickettraining.com>.
I would recommend using a custom model for this.  The advantage is that it
can be used anywhere. Something like:

   // bookmarkable constructor
   public CheeseDetailsPage(PageParameters parameters) {   #1
       this(new CheeseByIDModel(getIDFromParams(parameters)));
   }
   // non-bookmarkable constructor
   public CheeseDetailsPage(IModel<Cheese> cheese) {         #2
       // do cheesy stuff with the cheese
   }
   private statuc int getIDFromParams(PageParameters params) {
      return params.getInteger("foo");
   }

Even better would be to do something very reusable like:
new DomainEntityModel<Cheese>(Cheese.class, getIDFromParameters(parameters))

See http://wicketinaction.com/2008/09/building-a-smart-entitymodel/ for some
ideas about this.



On Thu, Dec 4, 2008 at 6:50 PM, quizzical <qu...@gmail.com> wrote:

>
> Hi Everyone,
>
> I'm just getting started with wicket and am learning by playing around with
> building an ecommerce application on top of wicket, spring and hibernate.
> I've been (Ha!) using @SpringBean to inject my spring dependencies and am
> very impressed with it.
>
> One thing I'm wondering about is how to use @SpringBean with a pattern in
> the 'wicket in action' book. Specifically, when creating a
> bookmarkablePageLink with pageparameters this pattern is used:
>
> public class CheeseDetailsPage extends WebPage {
>    // bookmarkable constructor
>    public CheeseDetailsPage(PageParameters parameters) {   #1
>        this(getCheese(parameters));
>    }
>    // non-bookmarkable constructor
>    public CheeseDetailsPage(Cheese cheese) {         #2
>        // do cheesy stuff with the cheese
>    }
>    /** Retrieves a cheese object based on the 'name' parameter. */
>    public static Cheese getCheese(PageParameters parameters) { #3
>        String name = parameters.getString("name", "");
>        if("".equals(name)) return null;
>        CheeseDao dao = ...;
>        return dao.getCheeseByName(name);
>    }
> }
>
> I like this pattern but i'm having a problem with injecting my
> dependencies.
> The getCheese() method is static, which means that the CheeseDao would have
> to be static. I haven't delved into the code of the @SpringBean proxy or
> the
> wicket serialization code (I have read the wiki on @SpringBean) and I don't
> have an amazing understanding of proxies so maybe i'm just being obtuse but
> heres my question.
>
> Is there any reason not to make the DAO static?
>
> Cheers in advance
>
> --
> View this message in context:
> http://www.nabble.com/%40SpringBean-with-page-creation-patterns-tp20845794p20845794.html
> Sent from the Wicket - User 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
>
>


-- 
Jeremy Thomerson
http://www.wickettraining.com