0

I want to check if someone has permission or not so they can view my link, but I don't understand why when I click in the 'View' link, it will disappear that link and does not process my prepareView(). What's wrong with it ?

<c:if test="#{controller.viewMode == 'OK'}">
    <h:commandLink action="#{controller.prepareView()}" value="View"/>
</c:if>
<h:commandLink action="#{controller.prepareEdit()}" value="Edit"/>
<h:commandLink action="#{controller.destroy()}" value="Delete"/>
Bryan
  • 697
  • 2
  • 8
  • 14
  • To be more consistent I would recommend using some variant of `` way of coding so to not mix JSTL and JSF – JScoobyCed Nov 22 '12 at 06:22
  • Could you please post the bean `Controller`? – Menno Nov 22 '12 at 06:22
  • @JScoobyCed: Last time when you sent your answer, I already tested it by put rendered="#{controller.viewMode == 'OK'}" inside h:commandLink. But the problem of disappearing my 'View' link still occurred – Bryan Nov 22 '12 at 07:11
  • @Auilo: my Controller is quite long, and the problem I think it's not relating to Controller – Bryan Nov 22 '12 at 07:14
  • Thanks JScoobyCed, your solution correctly. When Auilo asked me about Controller, so I checked again and saw that I didn't save my param :-). Once more thanks all for your supports – Bryan Nov 22 '12 at 09:25

1 Answers1

2

That can happen when the value behind #{controller.viewMode} depends on a request scoped condition which has incompatibly changed in the request of the form submit as compared to the request of the form display.

During the restore view phase of processing of the form submit, the <c:if> is re-evaluated again. If the #{controller.viewMode} returns at that moment not "OK" (even though it did that during the initial request of the form display) then the command link disappears in the component tree. Its action would then never be decoded nor invoked.

To fix this, you need to make sure that the #{controller.viewMode} returns exactly the same value during the postback request as it did during the initial request. If your bean is request scoped, you'd basically need to make sure that that property is properly initialized in the (post)constructor of the request scoped bean.

Alternatively, you could also put the bean in the view scope instead, so that the bean instance will live as long as you're interacting with the same view, but that has in turn another problem when used in taghandlers. So when you want to use the view scope, then you'd definitely have to replace the JSTL test by the rendered attribute of the JSF component.

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

with

<h:commandLink value="View" action="#{controller.prepareView}" rendered="#{controller.viewMode == 'OK'}" />

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555