Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 617
sergionni Avatar asked Mar 07 '11 15:03

sergionni


1 Answers

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)

like image 145
BalusC Avatar answered Sep 28 '22 04:09

BalusC