0

I build a simple page that acts as insert and edit:

<f:metadata>
    <f:viewParam name="id" value="#{bean.id}"  />
    <f:viewAction action="#{bean.init}" />
</f:metadata>
...............
<h:form>
    ...........
    <h:commandLink action="#{bean.update}" rendered="#{not empty bean.id}">
        <f:ajax execute="@form" render="@form" />
        Update <i class="fa fa-check"></i>
    </h:commandLink>
    <h:commandLink action="#{bean.save}" rendered="#{empty bean.id}">
       <f:ajax execute="@form" render="@form" />
       Save <i class="fa fa-check"></i>
    </h:commandLink>
</h:form>

So the page checks if it get id parameter in the url, it so, create "update" link, otherwise "save" link.

The rendered attribute works fine, but the update link doesn't sends the ajax request, but the save link works fine.

When I remove rendered attribute, both works just fine! So why the update link not working if the rendered attribute exists?

nrofis
  • 8,975
  • 14
  • 58
  • 113
  • @BalusC I saw that question and I did what you said in your answer option 2, and it still not working. – nrofis Apr 14 '16 at 21:14
  • I display the value of `bean.id` for testing, also I have added `` field to the form for testing. Also I have tried to add `` to the update link, and it still not working... – nrofis Apr 14 '16 at 21:24
  • No, I can see it still have the correct value, but the `update` function not called. Only if I remove the `rendered` attribute it called (and the `bean.id` is correct) – nrofis Apr 14 '16 at 21:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109202/discussion-between-nrofis-and-balusc). – nrofis Apr 14 '16 at 21:28
  • No, it's not `@PostConstract` but it's `@RequestScoped` – nrofis Apr 14 '16 at 21:31
  • The answer tells that the bean must be view scoped for that, or in case you want to use `` that you must use `#{param.id}`. – BalusC Apr 14 '16 at 21:32
  • Why it's must to be view scoped? The `{bean.id}` seems to be correct... – nrofis Apr 14 '16 at 21:34
  • Because a request scoped bean is recreated during postback with all its properties reset to defaults. And `#{bean.id}` refers bean property which is in a request scoped bean only available after update model values phase, which is too late as the command button is decoded during apply request values phase. All also explained in the duplicate. Do note that `#{param}` is an implicit EL variable referring the request parameter map. The `#{param.id}` does basically `request.getParameter("id")`. The `#{param}` is in this case not a bean. – BalusC Apr 14 '16 at 21:35
  • Test it and it worked with `@ViewScoped`. Thank you very much BalusC! – nrofis Apr 14 '16 at 21:36

0 Answers0