WebSession ClassCastException

I tried to subclass Wicket WebSession so that I could implement a basic authentication system. I followed the recommendations in the Wicket help library. When I try to execute the following in my WebPage, I get a ClassCastException:

((AppSession)Session.get()).setUid() 

Here is the complete error:

 java.lang.ClassCastException: org.apache.wicket.protocol.http.WebSession cannot be cast to com.example.webapp.AppSession 

I searched the Internet for many hours and tried everything I could. I would really appreciate help. Also, please let me know if there is a better way to do this. I am really new to the gate.

Thanks.

AppSession.java

 public final class AppSession extends WebSession { private Integer uid; public AppSession(Request request) { super(request); } public final Integer getUid() { return uid; } public final void setUid(Integer uid) { this.uid = uid; } public static AppSession get() { return (AppSession)Session.get(); } } 

App.java

 public class App extends WebApplication { public App() { super(); new AnnotatedMountScanner().scanPackage("com.example.webapp").mount(this); } @Override public Session newSession(Request request, Response response) { return new AppSession(request); } @Override public Class getHomePage() { return null; } } 
+4
source share
1 answer

I also have my own session class descended from WebSession . I do a few things differently from you.

Add the AppSession method:

 /** * Gets the session for the calling thread. * @return * The session for the calling thread. */ public static AppSession get() { return (AppSession)Session.get(); } 

You might want to try

 AppSession ssnSession = AppSession.get(); ssnSession.setUid(...); 

instead of your single line call.

Have you checked the packages for your (App) Session classes?

(I personally do not return null for your App.getHomePage() .)

Does it help?

+1
source

All Articles