h:commandLink not working when inside a list

2019-07-21 16:43发布

I have a problem with RichFaces and creating lists of links. If you attempt to use any type of commandLink inside a list (I've tried ui:repeat and rich:list) the action on that link is not called. I've also tried commandButton and the a4j variations of those. I'm using JSF 2, RichFaces 4 on Jboss 6.

<rich:list var="venue" value="#{searchManager.results}" type="definitions" stateVar="status">
  <h:form>
    <h:commandLink value="CLICK IT" immediate="true" action="#{score.selectVenue}" />
  </h:form>
</rich:list>

The position of the form also doesn't matter.

<h:form>
   <rich:list var="venue" value="#{searchManager.results}" type="definitions" stateVar="status">
       <h:commandLink value="CLICK IT" immediate="true" action="#{score.selectVenue}" />
   </rich:list>
</h:form>

If I just have the link by itself (no list) it works.

Any help would be greatly appreciated.

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-07-21 16:54

When you click a command link or press a command button to submit a form, JSF will during the apply request values phase scan the component tree for the command link/button in question so that it can find the action expression associated with it, which is in your case #{score.selectVenue}.

However to be able to ever reach that, you would need to ensure that #{searchManager.results} returns exactly the same list as it did when the form was displayed. Because with an empty result list, there would be no command link/button in the view at all during the apply request values phase of the form submit.

Your #{searchManager} bean seems to be request scoped. Request scoped beans have a lifetime of exactly one request-response cycle. So when you submit the form, you'll get a brand new and another instance of the request scoped bean than it was when the form was displayed. The results property seems not to be preserved during (post)construction of the bean and thus remains empty. So JSF cannot find the command link/button in question and thus cannot find the action expression associated with it and thus cannot invoke it.

As you're using JSF2, an easy fix is to place the bean in the view scope. This way the bean will live as long as you're submitting and navigating to exactly the same view by returning null or void in action methods.

@ManagedBean
@ViewScoped
public class SearchManager {
    // ...
}

See also:

查看更多
登录 后发表回答