com.vaadin.ui.UI.getSession()方法的使用及代码示例

x33g5p2x  于2022-01-31 转载在 其他  
字(9.2k)|赞(0)|评价(0)|浏览(220)

本文整理了Java中com.vaadin.ui.UI.getSession()方法的一些代码示例,展示了UI.getSession()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。UI.getSession()方法的具体详情如下:
包路径:com.vaadin.ui.UI
类名称:UI
方法名:getSession

UI.getSession介绍

[英]Gets the application object to which the component is attached.

The method will return null if the component is not currently attached to an application.

Getting a null value is often a problem in constructors of regular components and in the initializers of custom composite components. A standard workaround is to use VaadinSession#getCurrent() to retrieve the application instance that the current request relates to. Another way is to move the problematic initialization to #attach(), as described in the documentation of the method.
[中]获取组件附加到的应用程序对象。
如果组件当前未连接到应用程序,则该方法将返回null。
在常规组件的构造函数和自定义复合组件的初始值设定项中,获取空值通常是一个问题。标准的解决方法是使用VaadinSession#getCurrent()检索当前请求所涉及的应用程序实例。另一种方法是将有问题的初始化移动到#attach(),如方法文档中所述。

代码示例

代码示例来源:origin: com.vaadin/vaadin-server

/**
 * Finds the {@link VaadinSession} to which this connector belongs. If the
 * connector has not been attached, <code>null</code> is returned.
 *
 * @return The connector's session, or <code>null</code> if not attached
 */
protected VaadinSession getSession() {
  UI uI = getUI();
  if (uI == null) {
    return null;
  } else {
    return uI.getSession();
  }
}

代码示例来源:origin: com.vaadin/vaadin-server

public WebBrowser getWebBrowser() {
  return uI.getSession().getBrowser();
}

代码示例来源:origin: com.vaadin/vaadin-server

/**
 * Adds the performance timing data (used by TestBench 3) to the UIDL
 * response.
 *
 * @throws IOException
 */
private void writePerformanceData(UI ui, Writer writer) throws IOException {
  if (!ui.getSession().getService().getDeploymentConfiguration()
      .isProductionMode()) {
    writer.write(String.format(", \"timings\":[%d, %d]",
        ui.getSession().getCumulativeRequestDuration(),
        ui.getSession().getLastRequestDuration()));
  }
}

代码示例来源:origin: com.vaadin/vaadin-server

/**
 * Removes all references and information about connectors marked as
 * unregistered.
 *
 */
private void removeUnregisteredConnectors() {
  GlobalResourceHandler globalResourceHandler = uI.getSession()
      .getGlobalResourceHandler(false);
  for (ClientConnector connector : unregisteredConnectors) {
    removeUnregisteredConnector(connector, globalResourceHandler);
  }
  unregisteredConnectors.clear();
}

代码示例来源:origin: com.vaadin/vaadin-server

/**
 * Sets current instances for the UI and all related classes. The previously
 * defined values can be restored by passing the returned map to
 * {@link #restoreInstances(Map)}.
 *
 * @since 7.1
 *
 * @param ui
 *            The UI
 * @return A map containing the old values of the instances that this method
 *         updated.
 */
public static Map<Class<?>, CurrentInstance> setCurrent(UI ui) {
  Map<Class<?>, CurrentInstance> old = setCurrent(ui.getSession());
  old.put(UI.class, set(UI.class, ui));
  return old;
}

代码示例来源:origin: com.vaadin/vaadin-server

/**
 * Gets a connector by its id.
 *
 * @param connectorId
 *            The connector id to look for
 * @return The connector with the given id or null if no connector has the
 *         given id
 */
public ClientConnector getConnector(String connectorId) {
  ClientConnector connector = connectorIdToConnector.get(connectorId);
  // Ignore connectors that have been unregistered but not yet cleaned up
  if (unregisteredConnectors.contains(connector)) {
    return null;
  } else if (connector != null) {
    return connector;
  } else {
    DragAndDropService service = uI.getSession()
        .getDragAndDropService();
    if (connectorId.equals(service.getConnectorId())) {
      return service;
    }
  }
  return null;
}

代码示例来源:origin: com.vaadin/vaadin-server

/**
 * Returns the location URI of this page, as reported by the browser. Note
 * that this may not be consistent with the server URI the application is
 * deployed in due to potential proxies, redirections and similar.
 *
 * @return The browser location URI.
 * @throws IllegalStateException
 *             if the
 *             {@link DeploymentConfiguration#isSendUrlsAsParameters()} is
 *             set to {@code false}
 */
public URI getLocation() throws IllegalStateException {
  if (location == null && !uI.getSession().getConfiguration()
      .isSendUrlsAsParameters()) {
    throw new IllegalStateException("Location is not available as the "
        + Constants.SERVLET_PARAMETER_SENDURLSASPARAMETERS
        + " parameter is configured as false");
  }
  return location;
}

代码示例来源:origin: com.vaadin/vaadin-server

@Override
public InputStream getThemeResourceAsStream(UI uI, String themeName,
    String resource) {
  VaadinPortletSession session = (VaadinPortletSession) uI.getSession();
  PortletContext portletContext = session.getPortletSession()
      .getPortletContext();
  return portletContext
      .getResourceAsStream("/" + VaadinPortlet.THEME_DIR_PATH + '/'
          + themeName + "/" + resource);
}

代码示例来源:origin: com.vaadin/vaadin-server

/**
 * Adds an initialized UI to this session.
 *
 * @param ui
 *            the initialized UI to add.
 */
public void addUI(UI ui) {
  assert hasLock();
  if (ui.getUIId() == -1) {
    throw new IllegalArgumentException(
        "Can not add an UI that has not been initialized.");
  }
  if (ui.getSession() != this) {
    throw new IllegalArgumentException(
        "The UI belongs to a different session");
  }
  Integer uiId = Integer.valueOf(ui.getUIId());
  uIs.put(uiId, ui);
  String embedId = ui.getEmbedId();
  if (embedId != null) {
    Integer previousUiId = embedIdMap.put(embedId, uiId);
    if (previousUiId != null) {
      UI previousUi = uIs.get(previousUiId);
      assert previousUi != null && embedId.equals(previousUi
          .getEmbedId()) : "UI id map and embed id map not in sync";
      // Will fire cleanup events at the end of the request handling.
      previousUi.close();
    }
  }
}

代码示例来源:origin: com.vaadin/vaadin-server

@Override
public void addAttribute(String name, Resource value)
    throws PaintException {
  if (value == null) {
    throw new NullPointerException();
  }
  ClientConnector ownerConnector = openPaintables.peek();
  ownerConnector.getUI().getSession().getGlobalResourceHandler(true)
      .register(value, ownerConnector);
  ResourceReference reference = ResourceReference.create(value,
      ownerConnector, name);
  addAttribute(name, reference.getURL());
}

代码示例来源:origin: com.vaadin/vaadin-server

/**
 * Generates the initial UIDL message that can e.g. be included in a html
 * page to avoid a separate round trip just for getting the UIDL.
 *
 * @param request
 *            the request that caused the initialization
 * @param uI
 *            the UI for which the UIDL should be generated
 * @return a string with the initial UIDL message
 * @throws IOException
 */
protected String getInitialUidl(VaadinRequest request, UI uI)
    throws IOException {
  try (StringWriter writer = new StringWriter()) {
    writer.write("{");
    VaadinSession session = uI.getSession();
    if (session.getConfiguration().isXsrfProtectionEnabled()) {
      writer.write(getSecurityKeyUIDL(session));
    }
    writer.write(getPushIdUIDL(session));
    new UidlWriter().write(uI, writer, false);
    writer.write("}");
    String initialUIDL = writer.toString();
    getLogger().log(Level.FINE, "Initial UIDL:" + initialUIDL);
    return initialUIDL;
  }
}

代码示例来源:origin: com.vaadin/vaadin-server

ui.getSession().getCommunicationManager()
    .handleConnectorRelatedException(connector, e);

代码示例来源:origin: com.vaadin/vaadin-server

/**
 * Handles the given RPC method invocation for the given connector.
 *
 * @since 7.7
 * @param ui
 *            the UI containing the connector
 * @param connector
 *            the connector the RPC is targeted to
 * @param invocation
 *            information about the rpc to invoke
 */
protected void handleInvocation(UI ui, ClientConnector connector,
    ServerRpcMethodInvocation invocation) {
  try {
    ServerRpcManager.applyInvocation(connector, invocation);
  } catch (RpcInvocationException e) {
    ui.getSession().getCommunicationManager()
        .handleConnectorRelatedException(connector, e);
  }
}

代码示例来源:origin: com.vaadin/vaadin-server

.findErrorHandler(UI.this);
if (errorHandler == null && getSession() == null) {

代码示例来源:origin: com.vaadin/vaadin-server

.getSession().getGlobalResourceHandler(false);
if (globalResourceHandler != null) {
  String uri = globalResourceHandler.getUri(connector,

代码示例来源:origin: com.vaadin/vaadin-server

Lock lockInstance = ui.getSession().getLockInstance();
if (lockInstance instanceof ReentrantLock) {
  if (((ReentrantLock) lockInstance).hasQueuedThreads()) {

代码示例来源:origin: com.vaadin/vaadin-server

VaadinSession parentSession = parentUI.getSession();
if (parentSession != null && !parentSession.hasLock()) {
  String message = "Cannot remove from parent when the session is not locked.";

代码示例来源:origin: com.vaadin/vaadin-server

@Override
public void setPushMode(PushMode pushMode) {
  if (pushMode == null) {
    throw new IllegalArgumentException("Push mode cannot be null");
  }
  VaadinSession session = ui.getSession();
  if (session == null) {
    throw new UIDetachedException(
        "Cannot set the push mode for a detached UI");
  }
  assert session.hasLock();
  if (pushMode.isEnabled()
      && !session.getService().ensurePushAvailable()) {
    throw new IllegalStateException(
        "Push is not available. See previous log messages for more information.");
  }
  PushMode oldMode = getState().mode;
  if (oldMode != pushMode) {
    getState().mode = pushMode;
    if (!oldMode.isEnabled() && pushMode.isEnabled()) {
      // The push connection is initially in a disconnected state;
      // the client will establish the connection
      ui.setPushConnection(new AtmospherePushConnection(ui));
    }
    // Nothing to do here if disabling push;
    // the client will close the connection
  }
}

代码示例来源:origin: com.vaadin/vaadin-server

VaadinSession session = getSession();

代码示例来源:origin: com.vaadin/vaadin-server

/**
 * Load and initialize the mobile drag-drop-polyfill if needed and not yet
 * done so.
 */
private void loadMobileHtml5DndPolyfill() {
  if (mobileHtml5DndPolyfillLoaded) {
    return;
  }
  if (!getPage().getWebBrowser().isTouchDevice()) {
    return;
  }
  mobileHtml5DndPolyfillLoaded = true;
  String vaadinLocation = getSession().getService().getStaticFileLocation(
      VaadinService.getCurrentRequest()) + "/VAADIN/";
  getPage().addDependency(new Dependency(Type.JAVASCRIPT,
      vaadinLocation + ApplicationConstants.MOBILE_DND_POLYFILL_JS));
  getRpcProxy(PageClientRpc.class).initializeMobileHtml5DndPolyfill();
}

相关文章