Wiki source code of Best Practices

Version 5.2 by Eduard Moraru on 2012/05/24

Hide last authors
Silvia Macovei 3.2 1 {{toc/}}
Vincent Massol 1.1 2
Silvia Macovei 3.2 3 = XWiki Application Organization =
Vincent Massol 1.2 4
Silvia Macovei 3.1 5 This [[Best Practices document>>Best Practices XWiki Application Organization]] explains how to best organize an XWiki Application.
Ludovic Dubost 2.1 6
Silvia Macovei 3.2 7 = Check for Object existence in Class Sheets documents =
Vincent Massol 1.1 8
Vincent Massol 1.3 9 Class sheet documents should be written using the following construct (this is an example for displaying documents containing XWiki.XWikiUsers objects):
Vincent Massol 1.1 10
Eduard Moraru 5.1 11 {{code language="velocity"}}
Vincent Massol 1.5 12 #set($obj = $doc.getObject("XWiki.XWikiUsers"))
Vincent Massol 1.4 13 #if(!$obj)
14 1 User Sheet
Vincent Massol 1.3 15 This stylesheet must be applied on a document containing a XWiki.XWikiUsers object.
Vincent Massol 1.4 16 #else
17 1 $msg.get("userProfile", [$xwiki.getUserName($doc.fullName, false)])
Vincent Massol 1.3 18 ...
Vincent Massol 1.4 19 #end
Silvia Macovei 3.1 20 {{/code}}
Vincent Massol 1.1 21
Eduard Moraru 5.1 22 {{info}}
23 The 'if' tests first for the non existence. This is so that XWiki extract the title from the //1 User Sheet//, which is a proper title to display when viewsing the sheet page, instead of the computed name which will usually display something wrong.
24 {{/info}}
Eduard Moraru 4.1 25
26 = Handling errors when using xredirect for non-Javascript UIs =
27
28 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.
29
30 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.).
31
32 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?
33
34 A simplified code for this in the background service that produces the error is:
35
Eduard Moraru 5.1 36 {{code language="velocity"}}
Eduard Moraru 4.1 37 #handleRequest($success)
38 #if ($success)
Eduard Moraru 5.2 39 #if ($request.action == 'get' || $request.xpage == 'plain')
Eduard Moraru 4.1 40 ## JavaScript call here.
41 Action was successful.
42 #elseif ("$!request.xredirect" != '')
43 ## No-JavaScript here. Redirect.
44 $response.sendRedirect($request.xredirect)
45 #end
46 #else
47 #if ($request.action == 'get' || $request.xpage== 'plain')
48 ## JavaScript call here.
49 Action was NOT successful.
50 $response.setStatus(403)
51 #elseif ("$!request.xredirect" != '')
52 ## No-JavaScript here. What now!? Redirect?
53 #handleErrorHere($request.xredirect)
54 #end
55 #else
56 {{/code}}
57
58 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.
59
60 For the background service, it translates to:
61
Eduard Moraru 5.1 62 {{code language="velocity"}}
Eduard Moraru 4.1 63 ...
64 #elseif ("$!request.xredirect" != '')
65 ## No-JavaScript here. Redirect and forward error message.
Eduard Moraru 5.2 66 #set ($errorMessageKeyPrefix = "myModule.error.")
Eduard Moraru 4.1 67 $request.session.setAttribute("${errorMessageKeyPrefix}${request.xredirect}", 'Action was NOT successful')
68 $response.sendRedirect($request.xredirect)
69 #end
70 ...
71 {{/code}}
72
73 On the UI side:
74
Eduard Moraru 5.1 75 {{code language="velocity"}}
Eduard Moraru 4.1 76 ...
77 #set ($xredirect = $doc.getURL($context.action, $!{request.queryString}))
Eduard Moraru 5.2 78 #set ($errorMessageKeyPrefix = "myModule.error.")
Eduard Moraru 4.1 79 #set ($errorMessage = $request.session.getAttribute("${errorMessageKeyPrefix}${xredirect}"))
80 #if ("$!errorMessage" != '')
81 ## Clean the error and display the message.
82 #set ($discard = $request.session.removeAttribute("${errorMessageKeyPrefix}${xredirect}"))
83 {{error}}$errorMessage{{/error}}
84 #end
85 ...
86 {{/code}}
87
88 Note that using xredirect's value as session key (prefixed or not) is a good idea because:
89
Eduard Moraru 5.1 90 1. it's already there in both the UI (for sending it as parameter) and the background service (received as parameter)
91 1. it acts like a namespace, ensuring that the error will only be displayed for the current page/request.
92
Eduard Moraru 5.2 93 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.
94
Eduard Moraru 4.1 95 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.

Get Connected