h:commandLink not working when inside a list

落花浮王杯 提交于 2020-01-04 09:07:21

问题


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:


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:

  • commandButton/commandLink/ajax action/listener method not invoked or input value not updated


来源:https://stackoverflow.com/questions/6844830/hcommandlink-not-working-when-inside-a-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!