我目前正在开发一个组织框架,使用IBM WebSphere 7从Webworks迁移到Struts 2.我按照下面列出的步骤进行操作:
1)导入struts2-core-2.3.16.jar
以及其他依赖项JAR。
2)将xwork.xml
重命名为struts.xml
,并将web.xml
中的设置更改为使用StrutsPrepareAndExecuteFilter
。
3)在拦截器和控制器中分别用struts2代码和xwork代码替换webwork JAR代码到xwork2代码。
服务器启动正常,部署正常。当我启动应用程序时,我得到一个NullPointerException
,因为其中一个拦截器中的代码尝试检索会话信息并获得空响应。我对其进行了调试,发现会话信息在ActionContext
或ActionInvocation
实例中不可用。可能的原因是什么?
堆栈跟踪:(出于保密目的,我隐藏了组织的名称)
java.lang.NullPointerException
com.somecompany.merchandiseplanning.controller.AbstractAuthorizationInterceptor.getBusinessServicesString(AbstractAuthorizationInterceptor.java:57)
com.somecompany.merchandiseplanning.controller.AbstractAuthorizationInterceptor.initializeBusinessServices(AbstractAuthorizationInterceptor.java:63)
com.somecompany.merchandiseplanning.controller.AbstractAuthorizationInterceptor.intercept(AbstractAuthorizationInterceptor.java:35)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:562)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:188)
com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:116)
com.ibm.ws.webcontainer.filter.WebAppFilterChain._doFilter(WebAppFilterChain.java:77)
com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:908)
com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:997)
com.ibm.ws.webcontainer.extension.DefaultExtensionProcessor.invokeFilters(DefaultExtensionProcessor.java:1043)
com.ibm.ws.webcontainer.extension.DefaultExtensionProcessor.handleRequest(DefaultExtensionProcessor.java:963)
com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3933)
com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:276)
com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:931)
com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1583)
com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:186)
com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:445)
com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:504)
com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:301)
com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:275)
com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1604)
包含在错误堆栈中并尝试检索会话的代码片段是:
private String getBusinessServicesString(){
Map session = ActionContext.getContext().getSession(); //Error : No session retrived
UserInfo userInfo = (UserInfo) session.get("userinfo");
return userInfo.getUserLoginId() + "_BUSINESS_SERVICES";
}
我尝试用
替换它Map session = actionInvocation.getInvocationContext().getSession();
actionInvocation
是在拦截器的拦截方法中传递的ActionInvocation
的实例,但我在那里得到了相同的空响应。
任何帮助将不胜感激!
答案 0 :(得分:0)
只有当struts2过滤器启动操作时,才会从动作类中的ActionContext获取会话。
在这种情况下,在您的类中实现SessionAware
接口(包含方法getBusinessServicesString()
)并使用接口的session属性来获取会话。
假设ABC是你的行动类,那么它可以按照以下方式完成。
public class ABC extends ActionSupport implements SessionAware {
private Map session;
public void setSession(Map session) {
this.session = session;
}
public Map getSession() {
return session;
}
private String getBusinessServicesString(){
session = ActionContext.getContext().getSession();
UserInfo userInfo = (UserInfo) session.get("userinfo");
return userInfo.getUserLoginId() + "_BUSINESS_SERVICES";
}
}