Note that there are some explanatory texts on larger screens.

plurals
  1. POJSF Templating: f:event works, f:viewParam doesn't
    text
    copied!<p>Following the example of <a href="http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/f/metadata.html" rel="nofollow">the spec</a>, I have a template and a template client.</p> <p><strong>default.xhtml (template):</strong></p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets"&gt; &lt;h:head&gt; &lt;title&gt;...&lt;/title&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt; &lt;h:outputStylesheet name="css/screen.css" /&gt; &lt;/h:head&gt; &lt;h:body&gt; &lt;f:view&gt; &lt;ui:insert name="metadata" /&gt; &lt;div id="container"&gt; &lt;div id="header"&gt; &lt;img src="resources/gfx/logo.png" /&gt; &lt;/div&gt; &lt;div id="content"&gt; &lt;ui:insert name="content" /&gt; &lt;/div&gt; &lt;div id="footer"&gt; &lt;p&gt; This is a project.&lt;br /&gt; &lt;/p&gt; &lt;/div&gt; &lt;/div&gt; &lt;/f:view&gt; &lt;/h:body&gt; &lt;/html&gt; </code></pre> <p><strong>edit.xhtml (template client):</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets" template="/WEB-INF/templates/default.xhtml"&gt; &lt;ui:define name="metadata"&gt; &lt;f:metadata&gt; &lt;f:viewParam name="id" value="#{myBean.id}" /&gt; &lt;f:event type="preRenderView" listener="#{myBean.init}"/&gt; &lt;/f:metadata&gt; &lt;/ui:define&gt; &lt;ui:define name="content"&gt; ... &lt;/ui:define&gt; &lt;/ui:composition&gt; </code></pre> <p><strong>list.xhtml (the caller of the edit view) contains:</strong></p> <pre><code>&lt;h:commandLink action="edit" value="#{msgs.edit}"&gt; &lt;f:param name="id" value="#{myentity.id}" /&gt; &lt;/h:commandLink&gt; </code></pre> <p>The f:event gets called, but the id (viewParam) is not assigned to the bean. The id is however present in the request parameter map and can be retrieved like this:</p> <pre><code>FacesContext ctx = FacesContext.getCurrentInstance(); Map&lt;String, String&gt; parameters = ctx.getExternalContext().getRequestParameterMap(); if (parameters.containsKey("id")) { this.id = Long.valueOf(parameters.get("id")); } </code></pre> <p>But that's what <code>&lt;f:viewParam ...&gt;</code> should take care of (as far as I understood).</p> <p>What might be wrong?</p>
 

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