<f:viewParam> 使用 <ui:composition template> 时不工作

<f:viewParam> not working when using <ui:composition template>

我想创建一个包含请求参数和 requestScoped bean 的主从屏幕,但视图参数未填充。

调用重定向的link:

<h:form>
        <p:dataTable var="visit" value="#{visitBean.findAllVisits()}">
            <p:column headerText="mdh">
                <p:commandLink action="#{visitDetailBean.seeVisitDetails(visit)}">
                    <h:graphicImage library="images" name="details.png"/>
                </p:commandLink>
            </p:column>
            ....

背后的方法:

public String seeVisitDetails(Visit visit) throws IOException {
   return "/pages/mdh-details.xhtml?visitId=" + visit.getId()+ ";faces-redirect=true";
}

详情xhtml页面:

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:f="http://java.sun.com/jsf/core">

<f:metadata>
  <f:viewParam name="visitId" value="#{visitDetailBean.currentVisitId}" />
</f:metadata>

<ui:composition template="/templates/masterLayout.xhtml">
  <ui:define name="content">
    <h:outputText value="#{visitDetailBean.currentVisit.name}"/>
    test
  </ui:define>
</ui:composition>

最后是细节 Bean:

private long currentVisitId;

public void setCurrentVisitId(long currentVisitId) {
    this.currentVisitId = currentVisitId;
}

public long getCurrentVisitId() {
    return currentVisitId;
}

public Visit getCurrentVisit() {
    return visitService.findVisit(currentVisitId);
}

currentVisitId一直是0..其实我真的找不到了

使用模板时,<ui:composition><ui:define> 之外的任何内容都会被 忽略 。这包括 <f:metadata>.

将其移动到 <ui:composition><ui:define> 内。

例如

<ui:composition template="/templates/masterLayout.xhtml">
  <ui:define name="metadata">
    <f:metadata>
      <f:viewParam ... />
    </f:metadata>
  </ui:define>

  <ui:define name="content">
    ...
  </ui:define>
</ui:composition>

另请参阅:

  • When using <ui:composition> templating, where should I declare the <f:metadata>?

与具体问题无关,最好将masterLayout.xhtml放在/WEB-INF文件夹中,防止直接访问。

<ui:composition template="/WEB-INF/templates/masterLayout.xhtml">

另请参阅:

  • Which XHTML files do I need to put in /WEB-INF and which not?