GAE 上的 Struts 2:无法在拦截器内设置会话值
Posted
技术标签:
【中文标题】GAE 上的 Struts 2:无法在拦截器内设置会话值【英文标题】:Struts 2 on GAE : Unable to set session values inside interceptor 【发布时间】:2014-01-12 17:56:09 【问题描述】:我正在编写一个 Struts 2 拦截器,我在其中检查用户是否有效。如果无效,则将用户重定向到登录页面。但在此之前,我试图检索用户访问到这里的 URL,并将其放入会话变量中,以便在登录完成后我可以将他重定向回那里。但是当我尝试在会话中输入一个值时,它会抛出以下异常:
java.lang.NoClassDefFoundError: Could not initialize class freemarker.template.Configuration
at org.apache.struts2.views.freemarker.FreemarkerManager.createConfiguration(FreemarkerManager.java:322)
at org.apache.struts2.views.freemarker.FreemarkerManager.init(FreemarkerManager.java:273)
at org.apache.struts2.views.freemarker.FreemarkerManager.getConfiguration(FreemarkerManager.java:260)
at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:865)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:574)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:123)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectRequest(DevAppServerModulesFilter.java:368)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:351)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:116)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.__handle(ContextHandler.java:765)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:97)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:485)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:547)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
注意:它说找不到类,但我可以在构建路径的库列表中看到配置类。 拦截器的一段代码:
public String intercept(ActionInvocation actionInvocation)
throws Exception
System.out.println("inside auth interceptor");
//just to make sure session is created the first time
HttpSession session = ServletActionContext.getRequest().getSession(true);
Map<String, Object> sessionAttributes = ActionContext.getContext().getSession();
User user = (User) sessionAttributes.get("user");
ActionProxy proxy = actionInvocation.getProxy();
String namespace = proxy.getNamespace();
String actionName = proxy.getActionName();
sessionAttributes.put("returnUrl", namespace+(actionName == null || actionName.equals("/") ?"":("/"+actionName)));
//even tried this, but getting same error :
//session.setAttribute("returnUrl", namespace+(actionName == null || actionName.equals("/") ?"":("/"+actionName)));
System.out.println(namespace+(actionName == null || actionName.equals("/") ?"":("/"+actionName)));
if(user == null)
//no user exist. redirect to Login page
return Action.LOGIN;
else
return actionInvocation.invoke();
如您所见,我什至尝试session.setAttribute()
得到同样的错误。无需将任何内容放入会话变量中,代码就会按预期运行。那我做错了什么?
更新 1:我在 web-inf/lib
中确实有所需的 freemarker-2.3.19
,它已添加到构建路径中。
【问题讨论】:
【参考方案1】:部署项目时,jar 文件必须位于类路径中,其中应包含错误类中提到的内容。为确保文件已部署,您可以将其放在web-inf/lib
作为临时措施。您还可以将文件组装到战争档案中以进行整体部署。我说的库文件是
freemarker-2.3.19.jar
如果您使用的是 maven,那么您的 pom.xml
中需要以下依赖项
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.16</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.19</version>
</dependency>
【讨论】:
这是我检查的第一件事。我的web-inf/lib
文件夹中有这个。以上是关于GAE 上的 Struts 2:无法在拦截器内设置会话值的主要内容,如果未能解决你的问题,请参考以下文章