如何在XWiki中点击链接/按钮时执行Groovy脚本
Got it, let's walk through exactly how to link your Groovy script to a button/link click in XWiki—no auto-run on page load, just execution when the user actually clicks. There are a couple of straightforward ways to do this, depending on whether you want a page refresh or not.
Method 1: No-Page-Refresh (AJAX) Approach
This is great if you want the script to run in the background without reloading the page. Here's how to set it up:
- Create a button using XWiki's built-in button macro, then add a JavaScript click handler to trigger an AJAX call to your Groovy script.
- Store your Groovy script in a separate page (or the same page) wrapped in a condition that only runs when the AJAX request hits it.
Example Code
First, add this to your main page (where the button will live):
{{button id="triggerScriptBtn" label="Run My Groovy Script"/}} {{html}} <script> // Wait for the page to fully load document.addEventListener('DOMContentLoaded', function() { const btn = document.getElementById('triggerScriptBtn'); btn.addEventListener('click', function() { // Show a notification that the script is running new XWiki.widgets.Notification("Executing script...", "info"); // Use XWiki's API to call the script page const scriptPageURL = new XWiki.Document('MyGroovyScriptPage', 'Main').getURL('execute', 'language=groovy'); fetch(scriptPageURL, { method: 'POST', credentials: 'include' // Keep the user session active }) .then(response => response.text()) .then(result => { new XWiki.widgets.Notification(`Script ran successfully: ${result}`, "success"); }) .catch(error => { new XWiki.widgets.Notification(`Oops, error running script: ${error}`, "error"); }); }); }); </script> {{/html}}
Then, create a page named MyGroovyScriptPage in the Main space, and add this Groovy code:
// Only run this if the request is an execute action if ($request.action == 'execute') { // Your working Groovy script goes here def currentUser = xwiki.getUser() def executionTime = new Date() // Send a result back to the frontend $response.setContentType('text/plain') $response.getWriter().write("Hello ${currentUser}, script executed at ${executionTime}") }
This way, the script only runs when the button is clicked—no auto-execution on page load.
Method 2: Simple Page-Refresh Approach (Easiest for Quick Setup)
If you don't mind a quick page refresh, this is the fastest way to set things up. We'll use a button that adds a query parameter to the page URL, then run the Groovy script only when that parameter exists.
Example Code
Add this directly to your page:
// Button that adds a "runScript=true" parameter to the page URL {{button label="Run Script" href="$doc.getURL('view', 'runScript=true')"/}} // Only execute the Groovy script if the parameter is present #if ($request.runScript == 'true') {{groovy}} // Your working Groovy script here def notificationService = xwiki.getNotificationService() notificationService.notify(XWiki.NotificationLevel.INFO, "Groovy script executed successfully!") {{/groovy}} // Redirect back to the page without the parameter to avoid re-running on refresh $response.sendRedirect($doc.getURL()) #end
How It Works
- When the user clicks the button, the page reloads with
?runScript=truein the URL. - The
#ifcheck detects the parameter and runs the Groovy script. - After execution, we redirect back to the original page (without the parameter) so hitting refresh won't re-run the script.
Key Notes
- Permissions: Make sure the user has the right to execute Groovy scripts (check XWiki's permission settings—usually requires "Programming Rights" or specific script execution permissions).
- Avoid Auto-Run: Never put your Groovy script directly in a
{{groovy}}macro without a condition (like the#ifcheck or action check)—that will make it run every time the page loads.
内容的提问来源于stack exchange,提问作者HKunibert

