You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@wicket.apache.org by Brad Gardner <bg...@gmail.com> on 2008/11/08 19:44:50 UTC
Class Cast Exception on WebSession
I'm brand new to wicket and going through the book "Wicket in Action". I've
ran into a problem that I can't seem to get around, maybe I am over thinking
it. I have a class I wrote that extends
org.apache.protocol.http.WebSession, and I am getting a ClassCastException
when I try to return my session. This is the cheese store example from the
book if anyone is familiar with it. Here is the code in question:
*From my class that extends WebPage:
*
public CheeseStoreSession getCheeseStoreSession()
{
return (CheeseStoreSession) getSession(); *//exception occurs right
here*
}
I've confirmed that the getSession method is returning an object of type
WebSession.
*
My custom session class looks like this:*
package com.manning.example;
import org.apache.wicket.Request;
import org.apache.wicket.Response;
import org.apache.wicket.Session;
import org.apache.wicket.protocol.http.WebSession;
public class CheeseStoreSession extends WebSession
{
private Cart cart = new Cart();
protected CheeseStoreSession(Request request)
{
super(request);
}
public Cart getCart()
{
return cart;
}
public Session newSession(Request request, Response response)
{
return new CheeseStoreSession(request);
}
}
I am having difficulty understanding the purpose of the ClassCastException
that I am getting. Here is the exception:
WicketMessage: Error calling method: public com.manning.example.Cart
com.manning.example.CheeseStorePage.getCart() on object: [Page class =
com.manning.example.Index, id = 13, version = 0]
Root cause:
java.lang.ClassCastException: org.apache.wicket.protocol.http.WebSession
at
com.manning.example.CheeseStorePage.getCheesrSession(CheeseStorePage.java:27)
at com.manning.example.CheeseStorePage.getCart(CheeseStorePage.java:33)
at java.lang.reflect.Method.invoke(Method.java:585)
at
org.apache.wicket.util.lang.PropertyResolver$MethodGetAndSet.getValue(PropertyResolver.java:1046)
at
org.apache.wicket.util.lang.PropertyResolver.getObjectAndGetSetter(PropertyResolver.java:242)
at
org.apache.wicket.util.lang.PropertyResolver.getValue(PropertyResolver.java:87)
at
org.apache.wicket.model.AbstractPropertyModel.getObject(AbstractPropertyModel.java:113)
at org.apache.wicket.Component.getModelObject(Component.java:1559)
at
org.apache.wicket.markup.html.list.ListView.getViewSize(ListView.java:217)
at org.apache.wicket.markup.html.list.ListView.onPopulate(ListView.java:524)
at
org.apache.wicket.markup.repeater.AbstractRepeater.onBeforeRender(AbstractRepeater.java:127)
at org.apache.wicket.Component.internalBeforeRender(Component.java:1003)
...
...
Can someone point me in the right direction for diagnosing this?
--
Brad Gardner
Re: Class Cast Exception on WebSession
Posted by Igor Vaynberg <ig...@gmail.com>.
you have to declare it on the application.
-igor
On Sat, Nov 8, 2008 at 11:11 AM, Brad Gardner <bg...@gmail.com> wrote:
> Thank you!, this fixed the problem. I'm not quite sure I understand the
> problem though. Why does the newSession method need to be specified twice?
> Or did I just have it declared in the wrong class?
>
> On Sat, Nov 8, 2008 at 1:55 PM, Timo Rantalaiho <Ti...@ri.fi>wrote:
>
>> On Sat, 08 Nov 2008, Brad Gardner wrote:
>> > public Session newSession(Request request, Response response)
>> > {
>> > return new CheeseStoreSession(request);
>> > }
>>
>> You should have something like this in your Application
>> class, too, so that your CheeseStoreSession will be used.
>>
>> Best wishes,
>> Timo
>>
>> --
>> Timo Rantalaiho
>> Reaktor Innovations Oy <URL: http://www.ri.fi/ >
>>
>
>
>
> --
> Brad Gardner
>
Re: Class Cast Exception on WebSession
Posted by Brad Gardner <bg...@gmail.com>.
Thank you!, this fixed the problem. I'm not quite sure I understand the
problem though. Why does the newSession method need to be specified twice?
Or did I just have it declared in the wrong class?
On Sat, Nov 8, 2008 at 1:55 PM, Timo Rantalaiho <Ti...@ri.fi>wrote:
> On Sat, 08 Nov 2008, Brad Gardner wrote:
> > public Session newSession(Request request, Response response)
> > {
> > return new CheeseStoreSession(request);
> > }
>
> You should have something like this in your Application
> class, too, so that your CheeseStoreSession will be used.
>
> Best wishes,
> Timo
>
> --
> Timo Rantalaiho
> Reaktor Innovations Oy <URL: http://www.ri.fi/ >
>
--
Brad Gardner
Re: Class Cast Exception on WebSession
Posted by Timo Rantalaiho <Ti...@ri.fi>.
On Sat, 08 Nov 2008, Brad Gardner wrote:
> public Session newSession(Request request, Response response)
> {
> return new CheeseStoreSession(request);
> }
You should have something like this in your Application
class, too, so that your CheeseStoreSession will be used.
Best wishes,
Timo
--
Timo Rantalaiho
Reaktor Innovations Oy <URL: http://www.ri.fi/ >