Best Practices
Where to put code?
Since xwiki allows you to put code both in wiki pages and in Java you might wonder where you should put your code. Here are some general guidelines:
- Don't put "business logic" code in wiki pages. Use Java for that. This gives you nice IDEs, the ability to easily debug the code and the ability to write automated unit tests. Generally speaking it makes it easy on maintenance.
- In general put the minimum amount of scripts in your wiki pages since that makes them harder to maintain.
- The only scripts that you should put in wiki pages (and not in Java code!) are "presentation logic" scripts, i.e. scripts in charge of presenting the data retrieved by using the Java/REST APIs.
Said differently you should use the MVC pattern by separating your Model (what we called "business logic" above) from your View (what we called "presentation logic" above).
XWiki Application Organization
- XWiki Development Team best practices
- Best Practices XWiki Application Organization (proposed by Ludovic Dubost)
Check for Object existence in Class Sheets documents
Class sheet documents should be written using the following construct (this is an example for displaying documents containing XWiki.XWikiUsers objects):
#if(!$obj)
1 User Sheet
This stylesheet must be applied on a document containing a XWiki.XWikiUsers object.
#else
1 $msg.get("userProfile", [$xwiki.getUserName($doc.fullName, false)])
...
#end
Handling errors when using xredirect for non-Javascript UIs
When writing an UI with JavaScript, AJAX takes care of forwarding your action to a background service replying with success or with an error that is then displayed to the user in the same page.
Without JavaScript, we usually use the xredirect query parameter to specify the current page (and state) to which we want to come back after performing an action (by pressing a button, link, submitting a form, etc.).
One common problem when writing UIs without JavaScript in this way is error handling. In other words, how do you handle the situation when the service that you use to perform your action throws an error?
A simplified code for this in the background service that produces the error is:
#if ($success)
#if ($request.action == 'get' || $request.xpage == 'plain')
## JavaScript call here.
Action was successful.
#elseif ("$!request.xredirect" != '')
## No-JavaScript here. Redirect.
$response.sendRedirect($request.xredirect)
#end
#else
#if ($request.action == 'get' || $request.xpage== 'plain')
## JavaScript call here.
Action was NOT successful.
$response.setStatus(403)
#elseif ("$!request.xredirect" != '')
## No-JavaScript here. What now!? Redirect?
#handleErrorHere($request.xredirect)
#end
#end
The idea is that you want to pass the error message to the UI but you don`t have a clear way of doing it, like you have for AJAX calls (response code and response text). A solution is to use the Session in order to pass your error message. You set the error in the service and, in the UI, you read and remove it so that it is only displayed once.
For the background service, it translates to:
#elseif ("$!request.xredirect" != '')
## No-JavaScript here. Redirect and forward error message.
#set ($errorMessageKeyPrefix = "myModule.error.")
$request.session.setAttribute("${errorMessageKeyPrefix}${request.xredirect}", 'Action was NOT successful')
$response.sendRedirect($request.xredirect)
#end
...
On the UI side:
#set ($xredirect = $doc.getURL($context.action, $!{request.queryString}))
#set ($errorMessageKeyPrefix = "myModule.error.")
#set ($errorMessage = $request.session.getAttribute("${errorMessageKeyPrefix}${xredirect}"))
#if ("$!errorMessage" != '')
## Clean the error and display the message.
#set ($discard = $request.session.removeAttribute("${errorMessageKeyPrefix}${xredirect}"))
{{error}}$errorMessage{{/error}}
#end
...
Note that using xredirect's value as session key (prefixed or not) is a good idea because:
- it's already there in both the UI (for sending it as parameter) and the background service (received as parameter)
- it acts like a namespace, ensuring that the error will only be displayed for the current page/request.
Using a prefix as above allows you to have multiple components (wiki macros, gadgets, etc.) in the same page using the same mechanism without collisions.
This method works together with the whole purpose for which we are doing the redirect in the first place (so that the user can refresh the page without re-sending the action or re-posting a form), ensuring that after the first display, on a refresh, the error goes away.