Use f:attribute for commandButton instead of f:param for h:commandLink

26,615

I assume that you're using JSF 1.x, otherwise this question didn't make sense. The <f:param> in <h:commandButton> is indeed not supported in legacy JSF 1.x, but it is supported since JSF 2.0.

The <f:attribute> can be used in combination with actionListener.

<h:commandButton action="connectedFilein" actionListener="#{bean.listener}">
    <f:attribute name="fileId" value="#{fileRecord.fileId}" />
</h:commandButton>

with

public void listener(ActionEvent event) {
    this.fileId = (Long) event.getComponent().getAttributes().get("fileId");
}

(Assuming that it's of Long type, which is classic for an ID)


Better is however to use the JSF 1.2 introduced <f:setPropertyActionListener>.

<h:commandButton action="connectedFilein">
    <f:setPropertyActionListener target="#{bean.fileId}" value="#{fileRecord.fileId}" />
</h:commandButton>

Or when you're already running a Servlet 3.0/EL 2.2 capable container (Tomcat 7, Glassfish 3, etc) and your web.xml is declared conform Servlet 3.0, then you could just pass it as method argument.

<h:commandButton action="#{bean.show(fileRecord.fileId)}" />

with

public String show(Long fileId) {
    this.fileId = fileId;
    return "connectedFilein";
}

Unrelated to the concrete problem, I'd strongly recommend to use JSF/Facelets tags instead of JSTL ones whenever possible.

<ui:fragment rendered="#{bean.fileId != null}">
    <ui:include src="fileOut.xhtml" id="searchOutResults"/>
</ui:fragment>

(A <h:panelGroup> is also possible and the best approach when using JSP instead of Facelets)

Share:
26,615
MTPy
Author by

MTPy

JEE,JSP,JSR-168 Portlets,Spring Batch,JPA

Updated on January 07, 2020

Comments

  • MTPy
    MTPy over 4 years

    I would like to include specific page depending upon button clicked.

    As far h:commandButton used,I couldn't use f:param, so it looks like I should use f:attribute tag.

    In case of f:param I would code like this:

    <h:commandLink action="connectedFilein">
        <f:param name="fileId" value="#{fileRecord.fileId}"/>
    <h:commandLink>
    
    <c:if test="#{requestParameters.fileId!=null}">
        <ui:include src="fileOut.xhtml" id="searchOutResults"/>
    </c:if>
    

    What is the f:attribuite case?

    thanks

  • MTPy
    MTPy about 13 years
    any case I should use bean.property declaration this case? request parameters don't play here?I wanted make it simpler
  • BalusC
    BalusC about 13 years
    JSF 2.0 supports <f:param> in <h:commandButton>. So if you can, upgrade it. Else your current best bet is <f:setPropertyActionListener>.
  • MTPy
    MTPy about 13 years
    ok,I see,unfortunately we use JSF 1.2 in our project. thank you