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

I would like to include a specific page depending on the click of a button.

While h:commandButton being used, I could not use f:param , so it seems to me that I should use the f:attribute tag.

In the case of f:param I would do the 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 f:attribuite case?

thanks

+6
command parameters button jsf hyperlink
source share
1 answer

I assume that you are using JSF 1.x, otherwise this question does not make sense. <f:param> in <h:commandButton> really not supported in legacy JSF 1.x, but is supported with JSF 2.0.

<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> 

from

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

(Assume this is a type of Long , which is classic for ID)


However, it is better to use JSF 1.2 introduced by <f:setPropertyActionListener> .

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

Or, if you are already using a container that supports Servlet 3.0 / EL 2.2 (Tomcat 7, Glassfish 3, etc.), and your web.xml declared compatible with Servlet 3.0, you can simply pass it as an argument to the method.

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

from

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

Unrelated to a specific issue, I highly recommend using JSF / Facelets tags instead of JSTL whenever possible.

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

(A <h:panelGroup> also possible and a better approach when using JSP instead of Facelets)

+14
source share

All Articles