Changes for page Creating XWiki Components
Last modified by Simon Urli on 2023/10/10
From version 61.1
edited by Vincent Massol
on 2015/03/02
on 2015/03/02
Change comment:
There is no comment for this version
To version 62.1
edited by Thomas Mortagne
on 2015/06/11
on 2015/06/11
Change comment:
There is no comment for this version
Summary
-
Page properties (2 modified, 0 added, 0 removed)
Details
- Page properties
-
- Author
-
... ... @@ -1,1 +1,1 @@ 1 -XWiki. VincentMassol1 +XWiki.ThomasMortagne - Content
-
... ... @@ -373,25 +373,28 @@ 373 373 } 374 374 {{/code}} 375 375 376 -If you need to access typed information then you'llobviouslyneedtocastitto##XWikiContext##asin:376 +If you need to access typed information then the easiest is to use a Provider the following way: 377 377 378 -{{code}} 378 +{{code language="java"}} 379 379 @Inject 380 -private Executionecution;380 +private Provider<XWikiContext> xwikiContextProvider; 381 381 ... 382 -public XWikiContext getXWikiContext() 383 -{ 384 - return (XWikiContext) this.execution.getContext().getProperty("xwikicontext"); 385 -} 382 +XWikiContext xcontext = this.xwikiContextProvider.get(); 386 386 {{/code}} 387 387 388 - Actually ifyouneedthe ##XWikiContext##object,thenyoucangetitinjecteddirectlyusing:385 +which will always provide a usuable XWikiContext (if there is none in the current ExecutionContext it will create one). 389 389 387 +Since 7.2 if you just want to get one if there is one (i.e. you don't need to automatically create a new one) you can use the "readonly" XWikiContext Provider as in: 388 + 390 390 {{code language="java"}} 391 391 @Inject 391 +@Named("readonly") 392 392 private Provider<XWikiContext> xwikiContextProvider; 393 393 ... 394 394 XWikiContext xcontext = this.xwikiContextProvider.get(); 395 +if (xcontext != null) { 396 +... 397 +} 395 395 {{/code}} 396 396 397 397 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 **{{scm project="xwiki-commons" path="xwiki-commons-core/xwiki-commons-context/src/main/java/org/xwiki/context/ExecutionContextInitializer.java"}}ExecutionContextInitializer{{/scm}}** component, and populate newly created execution contexts, just like with [[velocity contexts>>#HFromwikipages]].