首页 > 解决方案 > 如何从 Scout 客户端代码访问请求 HttpSession?

问题描述

例如,从 Scout Form execStore() 方法中,在执行任何服务器服务之前,我喜欢获取 HttpSession 并最终从其属性存储中获取自定义数据。

标签: eclipse-scout

解决方案


正如评论中提到的,Eclipse Scout 将 UI 层(HTML 呈现 - 或旧版本中的 Swing 客户端)与客户端模型分开。虽然 UI 层知道 HttpSession,但表单所在的客户端模型却不知道。

但是,您可以将相关属性放在 ServerSession(后端)上并将它们同步到 ClientSession(模型),反之亦然 - 取决于您的属性来自何处。

这个草图应该让你开始:

  1. 在您的 Client/ServerSession 类 ( extends AbstractServerSession) 中添加一个 getter 和 setter。
  2. 如果-且仅当-您需要将值同步到客户端,则像这样实现getter / setter(整数属性的示例):
  public Integer getMyProperty() {
    return getSharedContextVariable("myProperty", Integer.class);
  }

  public void setMyProperty(Integer newValue) {
    setSharedContextVariable("myProperty", Integer.class, newValue);
  }
  1. 您需要教应用程序将数据传输到您的客户端或服务器会话。
  2. 如果您的数据来自后端(例如来自数据库):您最好的猜测是覆盖org.eclipse.scout.rt.server.context.HttpServerRunContextProducer. 在您的 .server-part 中创建此类的子类,并添加@Replace注释。您实现它的最佳位置可能在方法中public IServerSession getOrCreateScoutSession(HttpServletRequest req, ServerRunContext serverRunContextForSessionStart, String scoutSessionId)
  3. 如果您的数据来自 UI 端(例如由 SAML 传递):这更复杂,我只提示从哪里开始查找:org.eclipse.scout.rt.ui.html.UiSession.createAndStartClientSession(Locale, UserAgent, Map<String, String>)关于如何创建 ClientSession 以及您是否可以在此位置访问您的数据。

推荐阅读