Show last authors
1 {{box cssClass="floatinginfobox" title="**Contents**"}}
2 {{toc/}}
3 {{/box}}
5 = Where to put code? =
7 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:
9 * 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.
10 * In general put the minimum amount of scripts in your wiki pages since that makes them harder to maintain.
11 * 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.
13 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).
15 = XWiki Application Organization =
17 * [[XWiki Development Team best practices>>dev:Community.ApplicationDevelopmentBestPractices]]
18 * [[Best Practices XWiki Application Organization>>dev:Drafts.Best Practices XWiki Application Organization]] (proposed by Ludovic Dubost)
20 = Check for Object existence in Class Sheets documents =
22 Class sheet documents should be written using the following construct (this is an example for displaying documents containing XWiki.XWikiUsers objects):
24 {{code language="velocity"}}
25 #set($obj = $doc.getObject("XWiki.XWikiUsers"))
26 #if(!$obj)
27 1 User Sheet
28 This stylesheet must be applied on a document containing a XWiki.XWikiUsers object.
29 #else
30 1 $msg.get("userProfile", [$xwiki.getUserName($doc.fullName, false)])
31 ...
32 #end
33 {{/code}}
35 {{info}}
36 The 'if' tests first for the non existence. This is so that XWiki can extract the title from the //1 User Sheet//, which is a proper title to display when viewing the sheet page, instead of the computed name which will usually display something wrong.
37 {{/info}}
39 = Handling errors when using xredirect for non-Javascript UIs =
41 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.
43 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.).
45 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?
47 A simplified code for this in the background service that produces the error is:
49 {{code language="velocity"}}
50 #handleRequest($success)
51 #if ($success)
52 #if ($request.action == 'get' || $request.xpage == 'plain')
53 ## JavaScript call here.
54 Action was successful.
55 #elseif ("$!request.xredirect" != '')
56 ## No-JavaScript here. Redirect.
57 $response.sendRedirect($request.xredirect)
58 #end
59 #else
60 #if ($request.action == 'get' || $request.xpage== 'plain')
61 ## JavaScript call here.
62 Action was NOT successful.
63 $response.setStatus(403)
64 #elseif ("$!request.xredirect" != '')
65 ## No-JavaScript here. What now!? Redirect?
66 #handleErrorHere($request.xredirect)
67 #end
68 #end
69 {{/code}}
71 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.
73 For the background service, it translates to:
75 {{code language="velocity"}}
76 ...
77 #elseif ("$!request.xredirect" != '')
78 ## No-JavaScript here. Redirect and forward error message.
79 #set ($errorMessageKeyPrefix = "myModule.error.")
80 $request.session.setAttribute("${errorMessageKeyPrefix}${request.xredirect}", 'Action was NOT successful')
81 $response.sendRedirect($request.xredirect)
82 #end
83 ...
84 {{/code}}
86 On the UI side:
88 {{code language="velocity"}}
89 ...
90 #set ($xredirect = $doc.getURL($context.action, $!{request.queryString}))
91 #set ($errorMessageKeyPrefix = "myModule.error.")
92 #set ($errorMessage = $request.session.getAttribute("${errorMessageKeyPrefix}${xredirect}"))
93 #if ("$!errorMessage" != '')
94 ## Clean the error and display the message.
95 #set ($discard = $request.session.removeAttribute("${errorMessageKeyPrefix}${xredirect}"))
96 {{error}}$errorMessage{{/error}}
97 #end
98 ...
99 {{/code}}
101 Note that using xredirect's value as session key (prefixed or not) is a good idea because:
103 1. it's already there in both the UI (for sending it as parameter) and the background service (received as parameter)
104 1. it acts like a namespace, ensuring that the error will only be displayed for the current page/request.
106 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.
108 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