【问题标题】:WebSession ClassCastExceptionWebSession ClassCastException
【发布时间】:2012-07-17 07:58:32
【问题描述】:

我一直在尝试对 Wicket 的 WebSession 进行子类化,以便实现基本的身份验证系统。我已遵循 Wicket 参考库中的指南。当我在我的网页中尝试以下操作时,我得到一个 ClassCastException:

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

这是完整的错误:

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

我已经在网上搜索了几个小时,并尽我所能。我真的很感激一些帮助。另外,如果有更好的方法,请告诉我。我对检票口很陌生。

谢谢。

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;
    }
}

【问题讨论】:

    标签: session wicket classcastexception wicket-1.5


    【解决方案1】:

    我也有自己的会话课程,源自WebSession。我做的一些事情和你不同。

    添加AppSession方法:

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

    你可以试试

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

    而不是您的单线电话。

    你检查过你的 (App)Session 类的包吗?

    (我个人不会为您的App.getHomePage()返回null。)

    这些有帮助吗?

    【讨论】:

    • 多用户和 AppSession 的生命周期不是问题吗?
    • @monksy 否,因为AppSession.get() 返回正在处理其 Web 请求的用户的会话。
    • 这是正确的答案,虽然单线调用没有错。我一直在使用它:) MySession.get().getUsername() 等等。
    猜你喜欢
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    • 2021-11-21
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    相关资源
    最近更新 更多