0

I would like to apply a variable in the session scoped using before the view is displayed, than this view will use this variable.

Here is the link:

<h:link value="#{msg.persondeactivate}" outcome="persondeactivate" />

Here is the faces-config.xml

<navigation-rule>
    <navigation-case>
        <from-outcome>persondeactivate</from-outcome>
        <to-view-id>/deactivatePerson.xhtml</to-view-id>
    </navigation-case>
</navigation-rule>

Here is the view (deactivatePerson.xhtml):

...<h:outputText value="#{msg.personIsDeactivate}" rendered="#{controller.personDeactivated}" style="color:green" />... <h:commandButton action="#{controller.deaktivieren}" value="#{msg.deactivate}"></h:commandButton>...

Here is the managed bean:

@ManagedBean @SessionScoped public class Controller { ... private boolean personDeactivated = false; public String deaktivieren(){ personDeactivated = false;
    // Deactivate process personDeactivated = true; return "persondeactivate";} ... }

I want that the variable personDeactivated is set to false before the view (deactivatePerson.xhtml) for the second time by is called.

It does not work.

Can someone please tell me what is wrong?

Thanks in advance.

Lukem
  • 123
  • 2
  • 3
  • 10

1 Answers1

0

You can use <f:event type="preRenderView"> to invoke a backing bean listener method before the view is rendered.

<f:event type="preRenderView" listener="#{controller.onPreRenderView}" />

with

public void onPreRenderView() {
    if (!FacesContext.getCurrentInstance().isPostback()) {
        // Do your job here when the view is been freshly requested by GET.
    }
    else {
        // Do your job here when a POST request is been performed by command link/button.
    }
}

Unrelated to the concrete problem, I have the impression that the Controller is actually in the wrong scope. A much better solution would be to make it @ViewScoped instead. This way the bean instance will be freshly created on every new GET request and live as long as you're POSTbacking to the very same view (and thus you won't encounter inconsitenties and unintuitive behaviour when having the same page open in multiple browser tabs/windows in the same session which would share the very same session scoped bean!). See also How to choose the right bean scope?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Many thanks BalusC for your quick response and the link. But I get the following error message when I insert the method in my session scoped: _The method IsPostBack () is undefined for the type FacesContext_ What does that mean? – Lukem Apr 04 '12 at 17:20
  • That just means that the method with the given name doesn't exist in the given class. Just fix the method name. – BalusC Apr 04 '12 at 17:28
  • I've been looking into the JSF 2.0 API for the right method, but I have not found. can it be that I need a special library? – Lukem Apr 11 '12 at 16:02
  • No, you had two typos in the method name. Java is case sensitive. – BalusC Apr 11 '12 at 23:00
  • So I can not find the two typos in the method name. I have written the method exactly as you wrote it: `if(!FacesContext.getCurrentInstance().isPostback())` Can you please tell me the two typos in the method name? Thank you. – Lukem Apr 12 '12 at 08:32