Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Instead of injecting the Spring beans via EL in a <code>@ManagedProperty</code> annotation (executed on the ManagedBean initialization), obtain the beans evaluating the EL at runtime.</p> <p>With this approach, this is what the JSF beans should look like:</p> <pre><code>@ManagedBean @ViewScoped public class SomeMB implements Serializable { private static final long serialVersionUID = 1L; private static SomeService someService() { return SpringJSFUtil.getBean("someService"); } // ... </code></pre> <p>And the utility class <em>SpringJSFUtil.java</em> that gets the bean via EL:</p> <pre><code>import javax.faces.context.FacesContext; public class SpringJSFUtil { public static &lt;T&gt; T getBean(String beanName) { if (beanName == null) { return null; } return getValue("#{" + beanName + "}"); } @SuppressWarnings("unchecked") private static &lt;T&gt; T getValue(String expression) { FacesContext context = FacesContext.getCurrentInstance(); return (T) context.getApplication().evaluateExpressionGet(context, expression, Object.class); } } </code></pre> <p>This eliminates the Spring bean property (at the cost of doing a few more EL evaluations), thus avoiding all the serialization issues of having the property in first place.</p> <p><strong>The same approach, using <a href="http://omnifaces.org" rel="nofollow">OmniFaces</a>:</strong></p> <p>In my actual code, I use the <code>evaluateExpressionGet(String expression)</code> method of an <a href="http://showcase.omnifaces.org/utils/Faces" rel="nofollow">utility class</a> available from <a href="http://omnifaces.org" rel="nofollow">OmniFaces</a>. So, for those of you who use it too, this is what my code really look like:</p> <pre><code>import static org.omnifaces.util.Faces.evaluateExpressionGet; @ManagedBean @ViewScoped public class SomeMB implements Serializable { private static final long serialVersionUID = 1L; private static SomeService someService() { return evaluateExpressionGet("#{someService}"); } // ... </code></pre> <p><sub>Notice that here the method gets the full EL ("#{expression}"), not just the Spring bean name (or you would get a ClassCastException).</sub></p>
    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.
    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