Changes for page Creating XWiki Components

Last modified by Simon Urli on 2023/10/10

<
From version < 59.4 >
edited by beldaz
on 2014/11/28
To version < 60.1 >
edited by Vincent Massol
on 2014/11/28
>
Change comment: There is no comment for this version

Summary

Details

Page properties
Author
... ... @@ -1,1 +1,1 @@
1 -XWiki.beldaz
1 +XWiki.VincentMassol
Content
... ... @@ -346,7 +346,7 @@
346 346  
347 347  Note that the XWiki context is deprecated. It was an older way of keeping track of the current request, which had to be passed around from method to method, looking like a [[ball and chain>>http://en.wikipedia.org/wiki/Ball_and_chain]] present everywhere in the code.
348 348  
349 -In the component world, the current request information is held in an **[[execution context>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core-context/apidocs/org/xwiki/context/ExecutionContext.html]]**. This is actually more powerful than the old XWiki context, as it is a generic execution context, and you can create one anytime you want and use it anyway you want. And you don't have to manually pass it around with all method calls, as execution contexts are managed by the **[[Execution component>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core-context/apidocs/org/xwiki/context/Execution.html]]**, which you can use just like any other XWiki component.
349 +In the component world, the current request information is held in an **[[Execution Context>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core-context/apidocs/org/xwiki/context/ExecutionContext.html]]**. This is actually more powerful than the old XWikiContext, as it is a generic execution context, hold in a ThreadLocal variable, and you can create one anytime you want and use it anyway you want. And you don't have to manually pass it around with all method calls, as execution contexts are managed by the **[[Execution component>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core-context/apidocs/org/xwiki/context/Execution.html]]**, which you can use just like any other XWiki component.
350 350  
351 351  In short, if you want to get access to the execution context (which holds context information inserted by the new components), you must declare an injection point on the ##Execution## component (located in the ##xwiki-commons-context## module), and then you can write:
352 352  
... ... @@ -364,19 +364,31 @@
364 364   }
365 365  {{/code}}
366 366  
367 -If you still need to access the old XWiki context, then you can get a reference to it from the execution context, but you should not cast it to an ##XWikiContext##, which would pull the whole xwiki-core as a dependency, but to a ##Map##. You won't be able to access all the properties, like the current user name or the URL factory, but you can access anything placed in the internal map of the XWikiContext.
367 +All that said, we're still in a transition phase and a lot of information is still available only through the old XWikiContext and has not yet been moved to the ExecutioncContext (the current user for example just to mention one). Thus you may still need to access the old XWiki Context. You can get a reference to it from the Execution Context. If you can you should try to not cast it to an ##XWikiContext##, which would pull the whole ##xwiki-platform-oldcore## as a dependency, but to a ##Map##. Doing it this way, you won't be able to access all the properties, like the current user name or the URL factory, but you can access anything placed in the internal map of the XWikiContext.
368 368  
369 369  {{code}}
370 370  private void workWithTheContext()
371 - {
372 - ExecutionContext context = execution.getContext();
373 - Map<Object, Object> xwikiContext = (Map<Object, Object>) context.getProperty("xwikicontext");
374 - // Do something with the XWiki context
375 - }
371 +{
372 + ExecutionContext context = execution.getContext();
373 + Map<Object, Object> xwikiContext = (Map<Object, Object>) context.getProperty("xwikicontext");
374 + // Do something with the XWiki context
375 +}
376 376  {{/code}}
377 377  
378 -If you want not just to use the execution context, but to make something available in every execution context, you can create an implementation of the [[ExecutionContextInitializer>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core-context/apidocs/org/xwiki/context/ExecutionContextInitializer.html]] component, and populate newly created execution contexts, just like with [[velocity contexts>>#HFromwikipages]].
378 +If you need to access typed information then you'll obviously need to cast it to XWikiContext as in:
379 379  
380 +{{code}}
381 +@Inject
382 +private Execution execution;
383 +...
384 +public XWikiContext getXWikiContext()
385 +{
386 + return (XWikiContext) this.execution.getContext().getProperty("xwikicontext");
387 +}
388 +{{/code}}
389 +
390 +If you want not just to use the Execution Context, but to make something available in every execution context, you can create an implementation of the [[ExecutionContextInitializer>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core-context/apidocs/org/xwiki/context/ExecutionContextInitializer.html]] component, and populate newly created execution contexts, just like with [[velocity contexts>>#HFromwikipages]].
391 +
380 380  == Code outside components ==
381 381  
382 382  You can use external libraries as in any other maven module, just declare the right dependencies in your module's ##pom.xml##.

Get Connected