【问题标题】:How to access servlet attribute in GWT server side code?如何在 GWT 服务器端代码中访问 servlet 属性?
【发布时间】:2013-06-15 09:05:20
【问题描述】:

我已将我的 GWT 应用程序组装为我在嵌入式 Jetty 中运行的 war 文件-

String confFile = System.getProperty("configFilename");
config = new XMLConfiguration(configFilename);
Server server = new Server(8080);
WebAppContext webapp = new WebAppContext();
webapp.setAttribute("config", config);      
webapp.setContextPath("/");
webapp.setWar("file.war");
server.setHandler(webapp);
server.start();
server.join();

我正在尝试在 GWT 服务器端代码中访问我的“配置”对象-

public class MyServiceImpl extends RemoteServiceServlet implements
        MyService {

    config = (XMLConfiguration) this.getThreadLocalRequest().getAttribute("config");

}

在这里,config 始终为空。

我做错了什么?我试过config =(XMLConfiguration) this.getServletContext().getAttribute("config"); 但这也不起作用 - 我收到错误 -

org.apache.commons.configuration.XMLConfiguration cannot be cast to org.apache.commons.configuration.XMLConfiguration

【问题讨论】:

  • 该错误告诉我,您必须将 XMLConfiguration 类放在 2 个单独的 jar 中。一个可能部署为 Jetty 的一部分,另一个部署为您的应用程序的一部分。
  • @LavieTobey - 这是正确的
  • 你在使用 maven 进行依赖管理吗?
  • 尝试专门导入该 jar 并将其标记为已提供,看看该演员是否有效。
  • 刚试过-没用:(同样的错误

标签: java gwt servlets jetty embedded-jetty


【解决方案1】:

尝试调整您的 maven 依赖项以显式包含包含 XMLConfiguration 类的 jar,并将其标记为已提供。这将删除您的类路径中的重复项并解决问题。

【讨论】:

    【解决方案2】:

    你必须从ServletContext而不是HttpServletRequest获取属性

    在你的 RPC 方法中试试这个:

    public class MyServiceImpl extends RemoteServiceServlet implements MyService {
    
      @Override
      public void MyMethod() {
         this.getThreadLocalRequest()
             .getSession().getServletContext()
             .getAttribute("config");
      }
    
    }
    

    【讨论】:

    • 我试过了 - 我收到错误 org.apache.commons.configuration.XMLConfiguration cannot be cast to org.apache.commons.configuration.XMLConfiguration
    • 很奇怪,同一个类不能对自己进行种姓。似乎您在启动码头时使用了不同版本的 XMLConfiguration 类,然后在您的 .war
    • 检查你的依赖树并寻找commons-configuration,proly你有这个库的两个版本。 mvn dependency:tree
    猜你喜欢
    • 2014-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多