Best Practices
- XWiki Application Organization
- Check for Object existence in Class Sheets documents
- Handling errors when using xredirect for non-Javascript UIs
XWiki Application Organization
This Best Practices document explains how to best organize an XWiki Application.
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
#else
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.
$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 ($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:
1.it's already there in both the UI (for sending it as parameter) and the background service (received as parameter)
2.it acts like a namespace, ensuring that the error will only be displayed for the current page/request.
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.