Execution Context Property Declaration

The declaration is currently optional, the property will be implicitly declared when set the first time. The declaration can be made in an ExecutionContextInitializer component. The initialize method of all such components that are available in the classpath and have been specified in a components.txt file will be called when the execution context is first initialized during the request.

In the above example you can see that it is possible to set various attributes on the property when it is beeing declared. The attributes currently supported are listed in the table below.

During a request cycle, some components may activate a clean or a cloned execution context by pushing to the execution context stack. You can control how you want your property to be managed in these situations by setting attributes.

Attribute

Parameters

Description

cloneValue()

-

Indicate that you want the value to be cloned when the execution context is cloned or when your property is inherited from a parent execution context

makeFinal()

-

Indicate that an exception should be thrown if someone tries to replace the initial value object

inherited()

-

Indicate that the property should be inherited by any new execution context that is pushed within the current request

nonNull()

-

Indicate that an exception should be thrown if null is set as the property value

type(Class<?> type)

type

the class that the value object should be typechecked against

Set a type for performing typechecking when updating the value object.

From a wiki page it's much easier to use the older XWiki Context (which is supposed to be fully replaced by the Execution Context at some point in the future).

For example:

{{velocity}}$xcontext.put("mykey", value){{/velocity}}

This requires Programming Rights.

It's also possible to save data in the HTTP Request even though it's recommended to use the Execution Context instead (since the Execution Context is independent of the execution environment and will work everywhere: Servlets, Portlets, JavaSE, etc).

For example from Velocity in a wiki page:

{{velocity}}$request.setAttribute("mykey", value){{/velocity}}

Servlet Session

If the data should last a little longer (i.e. for example span more than 1 request), you could save it in the Servlet Session (note that in this case your code will only work in a Servlet Environment).

For example, from Java in a Component:

importorg.xwiki.container.*;

@Inject Container container;...Request request = container.getRequest();// Note that this is a bit of a hack and the notion of Session exists in the Container class (getSession()) but the Session interface is empty at the moment, making it useless)if(request instanceof ServletRequest){ HttpServletRequest servletRequest =(ServletRequest) request; HttpSession session = servletRequest.getSession(); session.setAttribute("mykey", myvalue);}

Servlet Context

If the Data should last as long as the web application is up, you could use the Servlet Context. It's not very easy to access it but it's possible (note that in this case your code will only work in a Servlet Environment).