I actually came across this issue while working on the design of view parameters for JSF 2.0. It's a tricky problem because sometimes you just want the parameter to be discarded if it is null and sometimes you want that null value to clear the property on the model.

If we think about the goal of page parameters, they are there to map an inbound request parameter to the property of a model. The mapping is there to kick things off. The absence of a request parameter cannot be assumed to mean that you want to nullify the model. Otherwise, the page parameters would always be required and thus overstepping their bounds (which is exactly the situation you have with using them w/ a long-running conversation). If you want to clear the model out of the conversation, you should be using a command, whether it be a command button or a page action. Only then can you be sure that setting the value to null is the desired operation. You can easily pass a parameter hint like create=true to tell the page action to clear out any previous model value and start with a fresh form.

Keep in mind that the application seam-gen creates is a starting point, not a design best practice. If the button it creates is not handling the situation properly, then add some extra code to make it work.


On Mon, Dec 8, 2008 at 12:52 PM, Francisco Jose Peredo <franciscoperedo@tabasco.gob.mx> wrote:
The code for the create button in list.xhtml.ftl looks like this:

<s:div styleClass="actionButtons" rendered="${'#'}{empty from}">
        <s:button view="/${editPageName}.xhtml"
                 value="Create ${componentName}">
<#assign idName = componentName + pojo.identifierProperty.name?cap_first>
<#if c2j.isComponent(pojo.identifierProperty)>
<#foreach componentProperty in pojo.identifierProperty.value.propertyIterator>
<#assign cidName = componentName + componentProperty.name?cap_first>
            <f:param name="${cidName}"/>
            <f:param name="${idName}"/>

That generates a <f:param name="${idName}"/> for the primarykey(s). Why is that done? My best guess is that it is to clear the value
of the primary key for the new object that is going to be created.

But the <f:param name="${idName}"/> actually does nothing, because in seam it is impossible to set a parameter to null. This f:params are AFAIK expected to set the method generated in EntityHome.java.ftl:

 public void set${idName}(${idType} id)

But that set is never going to be called for <f:param name="${idName}"/> because the id value is null! And there is code to prevent set${idName}(${idType} id) from being called if the value for the id is going to be null in Pages.applyConvertedValidatedValuesToMode:

private void applyConvertedValidatedValuesToModel(FacesContext facesContext)
      String viewId = getViewId(facesContext);
      for ( Page page: getPageStack(viewId) )
         for ( Param pageParameter: page.getParameters() )
            ValueExpression valueExpression = pageParameter.getValueExpression();
            if (valueExpression!=null)
               Object object = Contexts.getEventContext().get( pageParameter.getName() );
               if (object!=null) //<--- HERE IS THE PROBLEM


Of course, it gives the impression it works, but that is just because our EntityHome is recently created and the value for the Id is initially null. But if we place our EntityHome inside a LRC, and try to use after a previous creation set the value of the Id, we will see that the Id is not reset to null by the <f:param name="${idName}"/>

A workaround I use when the id is Integer is this the -1 value:

<f:param name="${idName}" value="-1"/>

 public void set${idName}(${idType} id)

But that, as it was commented in another null related discussion, a really ugly way to deal with stuff we want to be "undefined". So what can be done to fix this in Seam/seam-gen? (And of course offer a solution that can be used as a "best practice" for dealing with this even in applications that do not use seam-gen).

I propose removing the if (object!=null) from applyConvertedValidatedValuesToModel (and maybe other methods in Pages that avoid dealing with nulls in the same limited way? like perhaps convertAndValidateStringValuesInPageContext? and getStringValuesFromModel? and storeRequestStringValuesInPageContext? and possibly others...).

Now, if ignoring null values for page parameters in this way is not a bug, but a feature, then I propose removing the foreach for the create button list.xhtml.ftl, because it just creates the false impression that  <f:param name="${idName}"/> actually does something.

I already created a related JIRA a while ago (https://jira.jboss.org/jira/browse/JBSEAM-3693) but guess I was not able to correctly explain this problem, I hope to have better luck this time.

Francisco Peredo

Dirección Informática de Servicios Financieros
Dirección General de Modernización e Innovación Gubernamental
Secretaría de Administración y Finanzas
Paseo de la Sierra 435 col. Reforma
C.P. 86086, Villahermosa, Tabasco.
Tel. 52 + 993 + 310 40 00 Ext. 7127

IMPORTANTE: Esta transmisión electrónica, incluyendo sus anexos, archivos insertados o "attachments", puede constituir información confidencial o reservada, en los términos de la Ley de Acceso a la Información Pública del Estado de Tabasco, y estar protegida por el derecho fundamental a la privacidad. Se prohibe el uso de esta información por cualquier persona distinta al receptor intencional o previsto. Si usted ha recibibido esta transmisión electrónica por error, por favor responda inmediatamente al emisor y borre esta información de su sistema. El uso, diseminación, distribución o reproducción de esta transmisión electrónica por receptores no intencionados o no previstos por el emisor, no está autorizada y puede considerarse ilícita en los términos de la legislación penal y civil vigente.

seam-dev mailing list

Dan Allen
Senior Software Engineer, Red Hat | Author of Seam in Action


NOTE: While I make a strong effort to keep up with my email on a daily
basis, personal or other work matters can sometimes keep me away
from my email. If you contact me, but don't hear back for more than a week,
it is very likely that I am excessively backlogged or the message was
caught in the spam filters.  Please don't hesitate to resend a message if
you feel that it did not reach my attention.