Malin
Malin

Reputation: 697

How to set a viewScope variable in Java

In my XPages application I want to steer the bahaviour of an XPage when performing calculations in the back-end in java.

I would like to control the behaviour by setting viewScope variables to calculate the rendered property of some controls.

Can someone guide me how I can achieve this? Google has not been my friend so far...

Upvotes: 0

Views: 801

Answers (3)

John Dalsgaard
John Dalsgaard

Reputation: 2807

Malin,

I suggest you use managed beans as Howard recommends. It is very convenient when you work with Java in XPages.

1. Create a bean

It's just an ordinary java bean - with a constructor without arguments. For example:

public class ExporterBean implements Serializable {
    private static final long serialVersionUID = 1L;
    private int numberOfDocs = 1L;

    public ExporterBean() {
        System.out.println("Instantiating ExporterBean");
    }

    public int getNumberOfDocs() {
        return numberOfDocs;
    }

    public void setNumberOfDocs(int numberOfDocs) {
        this.numberOfDocs = numberOfDocs;
    }
}

Please note that you should implement Serializable for all your beans. View-scope will force you to do so - but not the others (but it can bite you later depending on whether you keep beans in memory or serialize them to disk)

2. Define it as a managed bean

You do that in the file faces-config.xml under Application Configuration in the navigation of Domino Designer. You need to add something like:

  <managed-bean>
    <managed-bean-name>Exporter</managed-bean-name>
    <managed-bean-class>dk.myapp.bean.ExporterBean</managed-bean-class>
    <managed-bean-scope>view</managed-bean-scope>
  </managed-bean>

The scope can be: request, view, session, or application. Once you have done that then you can just reference the bean directly in your XPage using the name you specified in faces-config.xml.

In my example it could be:

<xp:text escape="false" value="#{Exporter.numberOfDocs}"/>

Hope this helps.

/John

Upvotes: 2

Howard
Howard

Reputation: 1503

Why not just just managed bean properties for that? In my apps I just about never use scoped variables any more.

Upvotes: 1

Patrick Kwinten
Patrick Kwinten

Reputation: 2048

try

ExtLibUtil.getViewScope().put("variableKey", "variableValue");

Upvotes: 2

Related Questions