GAE上的Struts 2:无法在拦截器中设置会话值 [英] Struts 2 on GAE : Unable to set session values inside interceptor

查看:68
本文介绍了GAE上的Struts 2:无法在拦截器中设置会话值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Struts2解释器,在其中检查用户是否有效.如果无效,则将用户重定向到登录"页面.但是在此之前,我试图检索用户访问过的URL并将其放在会话变量中,以便在登录完成后可以将他重定向回那里.但是,当我尝试在会话中放置一个值时,它会抛出以下异常:

I am writing a Struts2 interpreter in which I check if the user is valid or not. If not valid, the user is redirected to the Login page. But before that I'm trying to retrieve the URL, which the user has accessed to come here, and put it in a session variable, so that I could redirect him back there when logging in is done. But when I try to put a value inside the session its throwing me the following exception :

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)

注意:它说找不到类,但是我可以在buildpath的库列表中看到Configuration类.来自拦截器的代码片段:

Note : It says class not found, but I can see the Configuration class inside the list of libraries in the buildpath. The piece of code from interceptor :

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 来得到相同的错误.在不将任何内容放入会话变量的情况下,代码将按预期运行.那我做错了什么?

As you see, i even tried session.setAttribute to get the same error. Without putting anything into the session variable, the code runs as expected. So what is that I'm doing wrong?

更新1 :我确实在 web-inf/lib 中具有必需的 freemarker-2.3.19 ,并且已将其添加到内部版本中小路.

Update 1 : I do have the required freemarker-2.3.19 in web-inf/lib and it is added to the build path.

推荐答案

部署项目时,jar文件必须位于类路径上,该项目应包含错误类中提到的内容.为了确保文件已部署,您可以将其放置在 web-inf/lib 中作为临时措施.您也可以将文件汇编到战争档案中以进行整体部署.我正在谈论的库文件是

The jar file is required to be on classpath when the project is deployed which should contain the mentioned in the error class. To make sure the file is deployed you can place it in web-inf/lib as a temporary measure. You can also assemble a file to the war archive to be deployed at whole. The library file I'm talking about is

freemarker-2.3.19.jar

如果您使用的是maven,则 pom.xml

If you are using maven then the following dependencies required in your 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>

这篇关于GAE上的Struts 2:无法在拦截器中设置会话值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆