我做了一个应用程序,因为我应该维护一个登录用户会话,我创建了一个登录拦截器来维护它。我继承了做hibernate会话维护的目的。当我使用Login Interceptor和“hibernate-default”时,它会抛出Nullpointer异常。这里hibernate会话显示为NULL。如何维护hibernate会话。
下面是我的struts.xml
<package name="helloworld" extends="hibernate-default">
<interceptors>
<interceptor name="mylogging" class="Demo.AuthenticationInterceptor"></interceptor>
<interceptor-stack name="loggingStack">
<interceptor-ref name="mylogging" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="loggingStack"></default-interceptor-ref>
<action name="signin" method="checkLogin" class="Demo.LoginForm">
<result name="input">/login.jsp</result>
<result name="success" type="redirect">/list.jsp</result>
<result name="failure">/error.jsp</result>
</action>
<action name="listUser" method="list" class="Demo.UserAction">
<interceptor-ref name="basicStackHibernate"/>
<result name="success">/register1.jsp</result>
<result name="login">/login.jsp</result>
</action>
</package>
</struts>
我用于每个操作的维护会话,但我的会话显示为NULL。
我的AuthenticationInterceptor.java
public class AuthenticationInterceptor implements Interceptor
{
public String intercept(ActionInvocation ai) throws Exception
{
System.out.println("inside the interceptor()......new");
Map session1 = ai.getInvocationContext().getSession();
String name = (String) session1.get("name");
System.out.println("inside the session or loginaction=" + name);
if ((session1.get("name") != null) || (session1.get("name") == null))
{
System.out.println("inside the session or loginaction ");
return ai.invoke();
} else {
return "login";
}
}
}
例外是:
Session =null
java.lang.NullPointerException
at Demo.UserDAOImpl.listUser(UserDAOImpl.java:57)
at Demo.UserAction.list(UserAction.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
null
at java.lang.reflect.Method.invoke(Method.java:606)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:440)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:279)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
答案 0 :(得分:0)
使用basicStackHibernate拦截器因为执行List,Edit和Delete操作Hibernate访问(Session和/或Transaction)。
<interceptor-ref name="defaultStackHibernate" />