Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A JSF <code>Converter</code> is not suitable for this. You need a custom <a href="http://docs.oracle.com/javaee/6/api/javax/el/ELResolver.html" rel="nofollow"><code>ELResolver</code></a> for this. Extending <a href="http://docs.oracle.com/javaee/6/api/javax/el/BeanELResolver.html" rel="nofollow"><code>BeanELResolver</code></a> and overriding <code>getType()</code>, <code>getValue()</code> and <code>setValue()</code> wherein you grab the concrete <code>ValueHolder</code> implementation should do it. This way you can ultimately end up with expressions like <code>value="#{bean.myStringValueHolder}"</code> without the <code>.data</code>. This way the data coercion/conversion will then automatically be done by JSF/EL. </p> <p>You can register the custom EL resolver on application's startup by <code>Application#addELResolver()</code>:</p> <pre><code>FacesContext.getCurrentInstance().getApplication().addELResolver(new ValueHolderELResolver()); </code></pre> <p>As to figuring the generic type, assuming that <code>ValueHolder</code> is an <code>abstract class</code> and you've concrete implementations like <code>StringValueHolder extends ValueHolder&lt;String&gt;</code> and so forth, then you'll be able to extract the generic type in the constructor of <code>ValueHolder</code>.</p> <pre><code>public abstract class ValueHolder&lt;T extends Comparable&lt;T&gt;&gt; extends Node&lt;T&gt; { private Class&lt;T&gt; type; private T data; @SuppressWarnings("unchecked") public ValueHolder() { this.type = (Class&lt;T&gt;) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; } public Class&lt;T&gt; getType() { return type; } public T getData() { return data; } public void setData(T data) { this.data = data; } public void setData(Object data) { this.data = type.cast(data); } } </code></pre> <p>I only wonder how this entire approach is useful. There's some seemingly unnecessary overhead. What exactly is the functional requirement? It is just eliminating getter/setter boilerplate? Why not just using model objects (javabeans/entities/etc) so that you end up with only one model property in the backing bean? Is it being tired of typing them everytime? A bit decent IDE can autogenerate them. Even more, complete javabean classes can be autogenerated based on a list of property names and a few clicks.</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.
    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