<p>There are a couple of things that I am having a difficult time understanding with regards to developing custom components in JSF.  For the purposes of these questions, you can assume that all of the custom controls are using valuebindings/expressions (not literal bindings), but I'm interested in explanations on them as well.</p>

<ol>
<li>Where do I set the value for the valuebinding?  Is this supposed to happen in decode?  Or should decode do something else and then have the value set in encodeBegin?</li>
<li>Read from the Value Binding - When do I read data from the valuebinding vs. reading it from submittedvalue and putting it into the valuebinding?  </li>
<li>When are action listeners on forms called in relation to all of this?  The JSF lifecycle pages all mention events happening at various steps, but its not completely clear to me when just a simple listener for a commandbutton is being called</li>
</ol>

<p>I've tried a few combinations, but always end up with hard to find bugs that I believe are coming from basic misunderstandings of the event lifecycle.</p>

[{'answer_id': 34371, 'author': 'Peter Hilton', 'author_id': 2670, 'author_profile': 'https://Stackoverflow.com/users/2670', 'pm_score': 2, 'selected': False, 'text': '<p>Action listeners, such as for a <em>CommandButton</em>, are called during the <em>Invoke Application</em> phase, which is the last phase before the final <em>Render Response</em> phase. This is shown in <a href="http://www.ibm.com/developerworks/library/j-jsf2/#figure1" rel="nofollow noreferrer">The JSF Lifecycle - figure 1</a>.</p>\n'}, {'answer_id': 34454, 'author': 'McDowell', 'author_id': 304, 'author_profile': 'https://Stackoverflow.com/users/304', 'pm_score': 5, 'selected': True, 'text': '<p>There is a pretty good diagram in the <a href="http://java.sun.com/javaee/javaserverfaces/download.html" rel="noreferrer">JSF specification</a> that shows the request lifecycle - essential for understanding this stuff.</p>\n\n<p>The steps are:</p>\n\n<ul>\n<li><strong>Restore View</strong>. The UIComponent tree is rebuilt.</li>\n<li><strong>Apply Request Values</strong>. Editable components should implement EditableValueHolder. This phase walks the component tree and calls the <em>processDecodes</em> methods. If the component isn\'t something complex like a UIData, it won\'t do much except call its own <em>decode</em> method. The <em>decode</em> method doesn\'t do much except find its renderer and invokes its <em>decode</em> method, passing itself as an argument. It is the renderer\'s job to get any submitted value and set it via <em>setSubmittedValue</em>.</li>\n<li><strong>Process Validations</strong>. This phase calls <em>processValidators</em> which will call <em>validate</em>. The <em>validate</em> method takes the submitted value, converts it with any converters, validates it with any validators and (assuming the data passes those tests) calls <em>setValue</em>. This will store the value as a local variable. While this local variable is not null, it will be returned and not the value from the value binding for any calls to <em>getValue</em>.</li>\n<li><strong>Update Model Values</strong>. This phase calls <em>processUpdates</em>. In an input component, this will call <em>updateModel</em> which will get the <em>ValueExpression</em> and invoke it to set the value on the model.</li>\n<li><strong>Invoke Application</strong>. Button event listeners and so on will be invoked here (as will navigation if memory serves).</li>\n<li><strong>Render Response</strong>. The tree is rendered via the renderers and the state saved.</li>\n<li>If any of these phases fail (e.g. a value is invalid), the lifecycle skips to Render Response.</li>\n<li>Various events can be fired after most of these phases, invoking listeners as appropriate (like value change listeners after Process Validations).</li>\n</ul>\n\n<p>This is a somewhat simplified version of events. Refer to the specification for more details.</p>\n\n<p>I would question why you are writing your own UIComponent. This is a non-trivial task and a deep understanding of the JSF architecture is required to get it right. If you need a custom control, it is better to create a concrete control that extends an exisiting UIComponent (like HtmlInputText does) with an equivalent renderer.</p>\n\n<p>If contamination isn\'t an issue, there is an open-source JSF implementation in the form of Apache MyFaces.</p>\n'}, {'answer_id': 34746, 'author': 'McDowell', 'author_id': 304, 'author_profile': 'https://Stackoverflow.com/users/304', 'pm_score': 2, 'selected': False, 'text': '<blockquote>\n  <p>It is the only framework that I\'ve\n  ever used where component creation is\n  a deep intricate process like this.\n  None of the other web frameworks\n  (whether in the .net world or not)\n  make this so painful, which is\n  completely inexplicable to me.</p>\n</blockquote>\n\n<p>Some of the design decisions behind JSF start to make a little more sense when you consider the goals. JSF was designed to be tooled - it exposes lots of metadata for IDEs. JSF is not a web framework - it is a <a href="http://en.wikipedia.org/wiki/Model_View_Presenter" rel="nofollow noreferrer">MVP</a> framework that can be used as a web framework. JSF is highly extensible and configurable - you can replace 90% of the implementation on a per-application basis.</p>\n\n<p>Most of this stuff just makes your job more complicated if all you want to do is slip in an extra HTML control.</p>\n\n<blockquote>\n  <p>The component is a composition of\n  several inputtext (and other) base\n  components, btw.</p>\n</blockquote>\n\n<p>I\'m assuming JSP-includes/tooling-based page fragments don\'t meet your requirements.</p>\n\n<p>I would consider using your <em>UIComponentELTag.createComponent</em> to create a composite control with a UIPanel base and creating all its children from existing implementations. (I\'m assuming you\'re using JSPs/taglibs and making a few other guesses.) You\'d probably want a custom renderer if none of the existing UIPanel renderers did the job, but renderers are easy.</p>\n'}, {'answer_id': 72753, 'author': 'David Waters', 'author_id': 12148, 'author_profile': 'https://Stackoverflow.com/users/12148', 'pm_score': 1, 'selected': False, 'text': '<p>The best article I\'ve found is <a href="http://blogs.steeplesoft.com/jsf-component-writing-check-list/" rel="nofollow noreferrer">Jsf Component Writing</a>, \nas for 2 where do I read the value for a value binding in your component you have a getter that looks like this</p>\n\n<pre><code>\npublic String getBar() {  \n     if (null != this.bar) {  \n         return this.bar ;  \n     }  \n     ValueBinding _vb = getValueBinding("bar");  \n     return (_vb != null) ? (bar) _vb.getValue(getFacesContext()) : null;  \n}\n</code>  \n</pre>\n\n<p>how did this get into the getValueBinding?\nIn your tag class setProperties method</p>\n\n<pre><code>  if (bar!= null) {  \n         if (isValueReference(bar)) {  \n             ValueBinding vb = Util.getValueBinding(bar);  \n             foo.setValueBinding("bar", vb);  \n         } else {  \n             throw new IllegalStateException("The value for \'bar\' must be a ValueBinding.");  \n         }  \n     }  \n</code></pre>\n'}]
['https://Stackoverflow.com/questions/33476', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1432/']