1
votes

We do have a small page which is using the viewParam to convert a given Id to a concrete object. This converter is called on leaving (blur) of an inputText field, which is validated. Why? Could I rework this, so that the converter is not called every time?

This is annoying, because the converter calls the set-method for the corresponding object in the BackingBean and this bean is then null, if the page is called the first time for creating this object.

<f:metadata>
    <f:viewParam name="id" value="#{bean.object}"
                 converter="#{objectConverter}"
                 converterMessage="#{msgs['converter.msg.object']}"/>
    <f:viewAction action="#{bean.init}"/>
</f:metadata>

<p:inputText id="text" value="#{cc.attrs.value}"
             styleClass="inputTextValidated"
             required="#{cc.attrs.required}"
             requiredMessage="#{cc.attrs.requiredmessage}"
             label="text" validatorMessage="#{cc.attrs.msg}" title="#cc.attrs.title}"
             readonly="#{cc.attrs.readOnly}">
            <cc:insertChildren/>
            <p:ajax update="msg_text" event="blur"/>
</p:inputText>
<p>
  <p:message id="msg_text" for="text" display="msg"/>
</p>

If we do not use a converter but just the viewAction to convert the Id to the corresponding object (or create a new object if applicable), everything is fine. Is this the only/correct solution for this problem?

We do use primefaces 6.1 with CDI. The converter is a @Named and @ApplicationScoped bean implementing the Converter Interface.

Using p:fragment around the inputtext-field did not help either.

1

1 Answers

2
votes

That is the nature of how JSF works. If you want to not have it do that I suggest you check out OminFaces ViewParam: http://showcase.omnifaces.org/components/viewParam

From their documentation:

Stateless mode to avoid unnecessary conversion, validation and model updating on postbacks The standard UIViewParameter implementation calls the model setter again after postback. This is not always desired when being bound to a view scoped bean and can lead to performance problems when combined with an expensive converter. To solve this, this component by default stores the submitted value as a component property instead of in the model (and thus in the view state in case the binding is to a view scoped bean).

The standard UIViewParameter implementation calls the converter and validators again on postbacks. This is not always desired when you have e.g. a required="true", but the parameter is not retained on form submit. You would need to retain it on every single command link/button by . To solve this, this component doesn't call the converter and validators again on postbacks.