今天使用Struts2的時候遇到一個怪異的問題,首先在struts.xml中配置的action是沒有問題的,Java類也寫好了。但是運行的時候報
告如下錯誤:
javax.servlet.ServletException: Unable to instantiate Action, cn.edu.cup.actions.JobManager, defined for 'jobList' in namespace '/'null - action - file:/K:/Softwares/%E6%95%99%E5%AD%A6%E7%9B%B8%E5%85%B3
經(jīng)過網(wǎng)上仔細搜索發(fā)現(xiàn),根本原因在于我在方法中使用了 Student s = (Student) session.getAttribute("user"); 其中session是在構(gòu)造函數(shù)中獲取的。但是,通過分析Struts的原理(參見http://t8500071.javaeye.com/blog /348057),由于Action先于ActionContext對象實例之前創(chuàng)建,所以session對象是null,因此,會報告空指針錯誤!
明白了上述原理后,將獲取session的語句從構(gòu)造函數(shù)中刪除,放到方法中,程序終于正常了!
javax.servlet.ServletException: Unable to instantiate Action, cn.edu.cup.actions.JobManager, defined for 'jobList' in namespace '/'null - action - file:/K:/Softwares/%E6%95%99%E5%AD%A6%E7%9B%B8%E5%85%B3
經(jīng)過網(wǎng)上仔細搜索發(fā)現(xiàn),根本原因在于我在方法中使用了 Student s = (Student) session.getAttribute("user"); 其中session是在構(gòu)造函數(shù)中獲取的。但是,通過分析Struts的原理(參見http://t8500071.javaeye.com/blog /348057),由于Action先于ActionContext對象實例之前創(chuàng)建,所以session對象是null,因此,會報告空指針錯誤!
明白了上述原理后,將獲取session的語句從構(gòu)造函數(shù)中刪除,放到方法中,程序終于正常了!
posted @ 2010-10-16 11:19 希望的石頭 閱讀(3572) | 評論 (0) | 編輯 收藏