对于h:commandLink,使用f:attribute为commandButton而不是f:param

ser*_*nni 7 parameters jsf command button hyperlink

我想根据点击的按钮包含特定页面.

到目前为止h:commandButton,我无法使用f:param,所以看起来我应该使用f:attribute标签.

如果f:param我会像这样编码:

<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>
Run Code Online (Sandbox Code Playgroud)

怎么f:attribuite回事?

谢谢

Bal*_*usC 15

我假设您正在使用JSF 1.x,否则这个问题没有意义.该<f:param><h:commandButton>确实没有传统的JSF 1.x的支持,但由于JSF 2.0受支持.

<f:attribute>可与组合使用actionListener.

<h:commandButton action="connectedFilein" actionListener="#{bean.listener}">
    <f:attribute name="fileId" value="#{fileRecord.fileId}" />
</h:commandButton>
Run Code Online (Sandbox Code Playgroud)

public void listener(ActionEvent event) {
    this.fileId = (Long) event.getComponent().getAttributes().get("fileId");
}
Run Code Online (Sandbox Code Playgroud)

(假设它是Long类型,这是ID的经典之作)


然而,更好的是使用JSF 1.2引入<f:setPropertyActionListener>.

<h:commandButton action="connectedFilein">
    <f:setPropertyActionListener target="#{bean.fileId}" value="#{fileRecord.fileId}" />
</h:commandButton>
Run Code Online (Sandbox Code Playgroud)

或者,当您已经运行了支持Servlet 3.0/EL 2.2的容器(Tomcat 7,Glassfish 3等)并且您web.xml声明符合Servlet 3.0时,您可以将其作为方法参数传递.

<h:commandButton action="#{bean.show(fileRecord.fileId)}" />
Run Code Online (Sandbox Code Playgroud)

public String show(Long fileId) {
    this.fileId = fileId;
    return "connectedFilein";
}
Run Code Online (Sandbox Code Playgroud)

无关的具体问题,我强烈推荐,而不是使用JSTL那些JSF/Facelets标记只要有可能.

<ui:fragment rendered="#{bean.fileId != null}">
    <ui:include src="fileOut.xhtml" id="searchOutResults"/>
</ui:fragment>
Run Code Online (Sandbox Code Playgroud)

(<h:panelGroup>使用JSP而不是Facelets时,A 也是可能的,也是最好的方法)