Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>@ViewScoped</code> annotation ties the bean to a specific JSF view which in turn relies on the presence of <code>FacesContext#getViewRoot()</code> and subsequently <code>UIViewRoot#getViewMap()</code>. None of both are available in a "plain vanilla" servlet. There's no means of a JSF view inside a plain servlet request, let alone a JSF context. So, unfortunately, this behavior is "by design".</p> <p>You've basically 2 options:</p> <ol> <li><p>Store the desired shared information in the session scope which is keyed by an unique key which is passed around as HTTP request parameter so that both the JSF managed bean and the servlet can grab it from the session scope.</p> <p>E.g. in JSF backing bean:</p> <pre class="lang-java prettyprint-override"><code>dataId = UUID.randomUUID().toString(); externalContext.getSessionMap().put(dataId, data); </code></pre> <p>In JSF view:</p> <pre class="lang-html prettyprint-override"><code>&lt;h:outputScript&gt;var dataId = "#{bean.dataId}";&lt;/h:outputScript&gt; </code></pre> <p>In JavaScript:</p> <pre class="lang-js prettyprint-override"><code>function loadData() { $.get("servletURL", { dataId: dataId }, function(response) { // ... }); } </code></pre> <p>In servlet:</p> <pre class="lang-java prettyprint-override"><code>String dataId = request.getParameter("dataId"); Data data = (Data) session.getAttribute(dataId); </code></pre> <hr></li> <li><p>Use a true JSF backing bean instead of a plain vanilla servlet. You can definitely use <code>&lt;p:remoteCommand&gt;</code> for that. You can use <code>RequestContext#addCallbackParam()</code> in action(listener) method to "pass" ("print" is technically more correct) a JSON object from Java to JS and finally use <code>oncomplete</code> attribute to process it. Given that you're using OmniFaces, the <a href="http://showcase.omnifaces.org/components/commandScript" rel="nofollow"><code>&lt;o:commandScript&gt;</code></a> and <a href="http://showcase.omnifaces.org/utils/Ajax" rel="nofollow"><code>Ajax#data()</code></a> offers the same functionality. The <code>Ajax#data()</code> has the additional advantage that it automatically converts from Java to JSON so that you don't need to do it yourself.</p> <p>E.g. in JSF view:</p> <pre class="lang-html prettyprint-override"><code>&lt;o:commandScript name="loadData" action="#{bean.loadData}" oncomplete="processData()" /&gt; </code></pre> <p>In JSF backing bean:</p> <pre><code>public void loadData() { Ajax.data(data); } </code></pre> <p>In JavaScript:</p> <pre class="lang-js prettyprint-override"><code>function processData() { var data = OmniFaces.Ajax.data; // ... } </code></pre></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload