Question

Can someone of you explain me, why the first code works and the other one doesn't?

Working:

<h:commandButton value="#{settingsBean.tmp}" action="editverteilerliste">
    <f:setPropertyActionListener target="#{settingsEditVerteilerlisteBean.aktuelleVerteilerliste}" value="#{settingsBean.tmp}" />
</h:commandButton>

Not Working:

<p:commandLink action="editverteilerliste">
    <f:param name="aktuelleVerteilerliste" value="#{settingsBean.tmp}" />
    <h:outputText value="asdf"/>
</p:commandLink>

settingsBean.tmp is a instance of Verteilerliste

Action editverteilerliste leads to another page which is using the settingsEditVerteilerlisteBean:

@ManagedBean
@RequestScoped
public class settingsEditVerteilerlisteBean implements Serializable {

  private Logger logger = Logger.getLogger(settingsEditVerteilerlisteBean.class);

  public settingsEditVerteilerlisteBean() {
  }

  @PostConstruct
  public void init() {
    logger.info("Postconstructor invoked!");
  }

  @ManagedProperty(value = "#{aktuelleVerteilerliste}")
  private Verteilerliste aktuelleVerteilerliste;

  [Getter and Setter...]
}

The second page:

<h:body>
    <ui:composition template="../template/mainlayout.xhtml">
        <ui:define name="content">
            <h3>Verteilerliste <h:outputText value="#{settingsEditVerteilerlisteBean.aktuelleVerteilerliste.name}"/></h3>
        </ui:define>
    </ui:composition>
</h:body>

I know I just could use the working one but I want to understand why second one doens't work!

Was it helpful?

Solution

The second one doesn't work because your managed property binding is resolved to null, as there is most possibly no element for aktuelleVerteilerliste key in any of the JSF-scoped maps.

To make the second one work you need to tell JSF to look within parameters of a given request, i.e. change to @ManagedProperty(value = "#{param['aktuelleVerteilerliste']}").

To make the distinction more clear, <f:setPropertyActionListener> simply equates target object to value in invoke application phase of JSF lifecycle while the latter approach simply adds a particular parameter to a JSF component, which is command component in your case, and command component parameters can be accessed via EL object param that is a map of current request parameters.


That said, your usage of command links is considered to be a bad practice. They are used to invoke some server-side logic when the form is submitted to perform some business job. In your case you are using them to perform plain navigation. For this you'd be better off using <h:link> components to create simple a elements. This way, if you attach a parameter you'll end up with a query parameter like editverteilerliste?aktuelleVerteilerliste=something that you can get in the view via <f:viewParam>. That way you'll end up with bookmarkable URLs and clear navigation in your application.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top