Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2><a href="http://download.oracle.com/javaee/6/javaserverfaces/2.0/docs/pdldocs/facelets/f/viewParam.html" rel="noreferrer"><code>&lt;f:viewParam&gt;</code></a>:</h2> <ul> <li><p>Sets the value during update model values phase only (since it extends <a href="http://download.oracle.com/javaee/6/api/javax/faces/component/UIInput.html" rel="noreferrer"><code>UIInput</code></a>).</p></li> <li><p>The set value is not available during <a href="http://download.oracle.com/javaee/6/api/javax/annotation/PostConstruct.html" rel="noreferrer"><code>@PostConstruct</code></a>, so you need an additional <a href="http://download.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/f/event.html" rel="noreferrer"><code>&lt;f:event type="preRenderView" listener="#{bean.init}" /&gt;</code></a> inside the <a href="http://download.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/f/metadata.html" rel="noreferrer"><code>&lt;f:metadata&gt;</code></a> to do initialization/preloading based on the set values. Since JSF 2.2 you could use <code>&lt;f:viewAction&gt;</code> for that instead.</p></li> <li><p>Allows for nested <a href="http://download.oracle.com/javaee/6/javaserverfaces/2.0/docs/pdldocs/facelets/f/converter.html" rel="noreferrer"><code>&lt;f:converter&gt;</code></a> and <a href="http://download.oracle.com/javaee/6/javaserverfaces/2.0/docs/pdldocs/facelets/f/validator.html" rel="noreferrer"><code>&lt;f:validator&gt;</code></a> for more fine-grained conversion/validation. Even a <a href="http://download.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/h/message.html" rel="noreferrer"><code>&lt;h:message&gt;</code></a> can be attached.</p></li> <li><p>Can be included as GET query string using <code>includeViewParams</code> attribute of <a href="http://download.oracle.com/javaee/6/javaserverfaces/2.0/docs/pdldocs/facelets/h/link.html" rel="noreferrer"><code>&lt;h:link&gt;</code></a> or <code>includeViewParams=true</code> request parameter in any URL.</p></li> <li><p>Can be used on a <a href="http://download.oracle.com/javaee/6/api/javax/faces/bean/RequestScoped.html" rel="noreferrer"><code>@RequestScoped</code></a> bean, but it requires the bean to be <a href="http://download.oracle.com/javaee/6/api/javax/faces/bean/ViewScoped.html" rel="noreferrer"><code>@ViewScoped</code></a> if you want the view parameters to survive any validation failures caused by forms enclosed in the view, otherwise you need to manually retain all request parameters for the subsequent requests by <code>&lt;f:param&gt;</code> in the command components.</p></li> </ul> <p><strong>Example</strong>:</p> <pre class="lang-html prettyprint-override"><code>&lt;f:metadata&gt; &lt;f:viewParam id="user_id" name="id" value="#{bean.user}" required="true" requiredMessage="Invalid page access. Please use a link from within the system." converter="userConverter" converterMessage="Unknown user ID." /&gt; &lt;/f:metadata&gt; &lt;h:message for="user_id" /&gt; </code></pre> <p>with</p> <pre class="lang-java prettyprint-override"><code>private User user; </code></pre> <p>and an <a href="http://download.oracle.com/javaee/6/api/javax/faces/convert/FacesConverter.html" rel="noreferrer"><code>@FacesConverter("userConverter")</code></a>. Invoking page by <a href="http://example.com/context/user.xhtml?id=123" rel="noreferrer">http://example.com/context/user.xhtml?id=123</a> will pass the <code>id</code> parameter through the converter and set the <code>User</code> object as a bean property.</p> <hr> <h2><a href="http://download.oracle.com/javaee/6/api/javax/faces/bean/ManagedProperty.html" rel="noreferrer"><code>@ManagedProperty</code></a>:</h2> <ul> <li><p>Sets the value immediately after bean's construction.</p></li> <li><p>Set value is available during <a href="http://download.oracle.com/javaee/6/api/javax/annotation/PostConstruct.html" rel="noreferrer"><code>@PostConstruct</code></a> which allows easy initialization/preloading of other properties based on the set value.</p></li> <li><p>Doesn't allow for declarative conversion/validation in view.</p></li> <li><p>Managed property of <code>#{param}</code> is not allowed on beans with a broader scope than request scope, so the bean is required to be <a href="http://download.oracle.com/javaee/6/api/javax/faces/bean/RequestScoped.html" rel="noreferrer"><code>@RequestScoped</code></a>.</p></li> <li><p>If you rely a managed property of <code>#{param}</code> being present in the subsequent POST requests, then you need to include it as <code>&lt;f:param&gt;</code> in the <code>UICommand</code> components.</p></li> </ul> <p><strong>Example</strong>:</p> <pre class="lang-java prettyprint-override"><code>@ManagedProperty("#{param.id}") private Long id; private User user; @EJB private UserService userService; @PostConstruct public void init() { user = userService.find(id); } </code></pre> <p>But you have to manage validation yourself whenever <code>user</code> is <code>null</code> by fiddling with <a href="http://download.oracle.com/javaee/6/api/javax/faces/context/FacesContext.html#addMessage%28java.lang.String,%20javax.faces.application.FacesMessage%29" rel="noreferrer"><code>FacesContext#addMessage()</code></a> or something.</p> <hr> <p>You <em>can</em> use them both when both <a href="http://download.oracle.com/javaee/6/api/javax/annotation/PostConstruct.html" rel="noreferrer"><code>@PostConstruct</code></a> and <code>includeViewParams</code> are mandatory. You only won't be able to apply fine-grained conversion/validation anymore.</p> <hr> <h3>See also:</h3> <ul> <li><a href="https://stackoverflow.com/questions/6377798/what-can-fmetadata-and-fviewparam-be-used-for">What can &lt;f:metadata&gt;, &lt;f:viewParam&gt; and &lt;f:viewAction&gt; be used for?</a></li> <li><a href="http://balusc.blogspot.com/2011/09/communication-in-jsf-20.html#ProcessingGETRequestParameters" rel="noreferrer">Communication in JSF 2.0 - Processing GET request parameters</a></li> </ul>
 

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