2012-08-07 206 views
0

我有一個使用struts2和hibernate的應用程序。限制用戶訪問未經授權的頁面。我嘗試過實施struts攔截器。但是在struts.xml文件中提到了攔截器標籤後,我的java頁面中的httpsession對象開始拋出java.lang.NullPointerException。我想知道是否在使用hibernate時不能使用struts攔截器?或者有沒有什麼叫做hibernate攔截器來實現我的目標。struts 2攔截器與hibernate攔截器不同嗎?

堆棧跟蹤

java.lang.NullPointerException 
    at com.uac.dao.RoleDaoImpl.listRoles(RoleDaoImpl.java:308) 
    at com.uac.biz.RoleAction.viewRoles(RoleAction.java:193) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    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) 
    at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:163) 
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) 
    at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:249) 
    at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) 
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) 
    at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) 
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195) 
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) 
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195) 
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) 
    at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:148) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) 
    at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:93) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) 
    at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) 
    at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) 
    at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:128) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) 
    at org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:104) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) 
    at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) 
    at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) 
    at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138) 
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) 
    at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:148) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) 
    at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) 
    at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:128) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) 
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) 
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52) 
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:468) 
    at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) 
    at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:76) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602) 
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) 
    at java.lang.Thread.run(Unknown Source) 

struts.xml中

<interceptors> 
    <interceptor name="authentication" class="com.uac.biz.AuthenticationInterceptor"> </interceptor> 
    <interceptor-stack name="uac_auth"> 
     <interceptor-ref name="authentication" ></interceptor-ref> 
     <interceptor-ref name="defaultStack"></interceptor-ref> 
    </interceptor-stack> 
</interceptors>...... 
...................... 
<action name="addUserRedirect" class="com.uac.biz.RoleAction" method="viewRoles"> 
    <interceptor-ref name="uac_auth" /> 
    <result name="success">/addUser.jsp</result> 
</action> 

一段代碼使用會話對象

roleList=new ArrayList(); 
System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXX-----enterd into listRoleeees"); 
//Criteria roles=session.createCriteria(RolePojo.class); 
**Query query= session.createQuery(" from RolePojo");** 
result=roles.list(); 
System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXX-----after seesion"); 
for(Iterator it=result.iterator();it.hasNext();){ 
RolePojo rolepojo=(RolePojo) it.next(); 
roleList.add(new Pojo(rolepojo.getRoleId(),rolepojo.getRoleName())); 
+1

不知道你實際做了什麼,診斷是不可能的。當然,Struts 2攔截器可以在有或沒有Hibernate的情況下使用。這很可能是你配置了錯誤的東西,或者不適當地實施了攔截器。 – 2012-08-07 13:33:15

+0

你可以發佈一些代碼或你的堆棧跟蹤? – 2012-08-07 14:01:11

+0

嗨,我已經粘貼stacktrace,struts.xml和下面的一段代碼。 Hibernate會話對象是否與攔截器一起工作? – Fayaz 2012-08-08 12:45:27

回答

-1

你可以試試Servlet Filter來過濾請求的URL

1

你當然可以一起使用Struts2和Hibernate。 Struts2攔截器是處理您正在尋找的訪問安全性的好方法。

在下面鏈接的問題中,我提供了一個答案,應該爲您提供一個如何使用攔截器來提供此功能的出發點。

Sessions in struts2 application

至於您所遇到的NullPointerException異常,你就需要提供堆棧跟蹤和你的struts.xml的代碼示例,其中NPE發生的Java代碼。

+0

您需要粘貼到您的堆棧跟蹤 – Quaternion 2012-08-08 01:54:37

+0

@Fayaz可隨時編輯stackoverflow上的問題和答案以提供更多信息。請編輯你的問題,並在那裏粘貼堆棧跟蹤(格式很好)。就目前來看,這是完全不可讀的。 – 2012-08-08 13:51:46

+0

HI史蒂文,我很抱歉...我必須這樣做,因爲字符限制..我發現如果我可以在一個鏡頭後發佈...... – Fayaz 2012-08-08 18:20:10