0

我有一个循环呈现的链接列表。单击我想设置一个 bean 属性的链接selectedIndex。我试图通过在 Ajax 参数上传递它来做到这一点,但完整的表单正在刷新。即使那样,notificationPreferencesBean.retrievePreferences也不会被解雇。这种方法正确吗?

这是我的模板片段:

<ui:repeat value="#{notificationPreferencesBean.userNotificationPreferences}"
   var="userNotificationPreferenceVar" varStatus="idx">
    <li>
        <h:commandLink
           value="#{userNotificationPreferenceVar.notificationClassName.label}"
           styleClass="#{userNotificationPreferenceVar.cssClassName}"
           action="#{notificationPreferencesBean.retrievePreferences}">
           <f:ajax execute="@form" render="@none">
                <f:param value="#{idx}" name="idx" />
           </f:ajax>
        </h:commandLink>
    </li>
</ui:repeat>

这是bean声明:

@ManagedProperty(value = "#{param.idx}")
private Integer selectedIndex;

有趣的是,下面的代码可以 Ajaxfully 工作,但它只是调用notificationPreferencesBean.retrievePreferences并且没有设置 selectedIndex(正如预期的那样,因为我没有传递任何参数)

 <ui:repeat value="#{notificationPreferencesBean.userNotificationPreferences}"
   var="userNotificationPreferenceVar" varStatus="idx">
    <li>
        <h:commandLink
          value="#{userNotificationPreferenceVar.notificationClassName.label}"
          styleClass="#{userNotificationPreferenceVar.cssClassName}"
          action="#{notificationPreferencesBean.retrievePreferences}">
            <f:ajax execute="@form" render="@none" />
        </h:commandLink>
    </li>
</ui:repeat>

还有其他方法吗?我只想在单击链接时设置 selectedIndex 属性。

4

1 回答 1

1

您不需要为此使用注释@ManagedProperty

只需使用f:setPropertyActionListener

<ui:repeat value="#{notificationPreferencesBean.userNotificationPreferences}"
   var="userNotificationPreferenceVar" varStatus="idx">
    <li>
        <h:commandLink
           value="#{userNotificationPreferenceVar.notificationClassName.label}"
           styleClass="#{userNotificationPreferenceVar.cssClassName}"
           action="#{notificationPreferencesBean.retrievePreferences}">
           <f:setPropertyActionListener value="#{idx}"
               target="#{notificationPreferencesBean.selectedIndex}" />
           <f:ajax execute="@form" render="@none" />
        </h:commandLink>
    </li>
</ui:repeat>
于 2012-08-01T18:08:37.213 回答