Alfresco新手求助:如何通过Web表单调用指定execute方法?
Hey there! As someone who's built plenty of Alfresco WebScript integrations, I can confirm your approach is fully feasible—Alfresco's Java-backed WebScripts are designed exactly for this kind of HTTP-triggered logic execution. Let's walk through the tweaks you need to make to get this up and running:
1. Fix the Web Form's Action Path
Your current form action execute() won't work because it needs to point to the actual URL of your deployed WebScript. Alfresco maps WebScripts to specific URLs via their descriptor files.
First, let's correct the form code (note the typo fix for lable → label, and remove the problematic enctype):
<form action="/alfresco/service/example/search-invoice" method="get"> <input type="hidden" name="url" value="http://book.myrest.com/api?sort=TransDate"> <label>From: <input type="date" name="from"></label> <label>To:<input type="date" name="to"></label> <input type="submit" name="submit" value="search"> </form>
- Remove
enctype="text/plain"—this causes parameter parsing issues, as Alfresco's WebScriptRequest expects the defaultapplication/x-www-form-urlencodedformat. - Replace the action URL with the path defined in your WebScript's descriptor (we'll set that up next).
2. Configure Your WebScript Descriptor
Every Java-backed WebScript needs a *.desc.xml file to define its URL, permissions, and metadata. Create a file named search-invoice.get.desc.xml and place it in Alfresco/WEB-INF/classes/alfresco/extension/templates/webscripts/org/example/ (create the nested directories if they don't exist):
<webscript> <shortname>Invoice Search</shortname> <description>Triggers invoice search via external API with date range</description> <url>/example/search-invoice</url> <authentication>user</authentication> <family>Custom Invoices</family> </webscript>
- The
<url>value matches the action path in your form. <authentication>can be set touser,admin, orsystem—since your code usesAuthenticationUtil.runAs()to switch to the system user, this setting only controls who can trigger the request initially.
3. Handle Form Parameters in Your Execute Method
Your searchInvoice method needs to safely extract and validate form parameters from WebScriptRequest. Here's a robust implementation:
private void searchInvoice(WebScriptRequest req, WebScriptResponse res) throws IOException { // Extract form parameters String fromDate = req.getParameter("from"); String toDate = req.getParameter("to"); String apiUrl = req.getParameter("url"); // Validate required parameters if (fromDate == null || toDate == null || apiUrl == null) { res.setStatus(HttpServletResponse.SC_BAD_REQUEST); res.setContentType("application/json"); res.getWriter().write("{\"error\":\"Missing required date or API URL parameters\"}"); return; } // Add your business logic here: call the external API, process results, interact with Alfresco // ... // Send a success response res.setContentType("application/json"); PrintWriter out = res.getWriter(); out.write("{\"status\":\"success\",\"message\":\"Invoice search initiated successfully\"}"); out.close(); }
- Replace
e.printStackTrace()with Alfresco's standard logging for better debugging:
This ensures logs are written to Alfresco's official log files instead of the console.private static final Logger logger = LoggerFactory.getLogger(YourWebScriptClassName.class);
4. Deploy and Test
- Compile your Java WebScript class into a JAR file, then place it in
Alfresco/WEB-INF/lib/. - Place the descriptor file in the correct extension directory as mentioned earlier.
- Restart your Alfresco server to load the new WebScript.
- Access the page containing your form (host it in Alfresco Share's web resources or a custom WebScript), submit it, and verify:
- Alfresco logs for any errors or info messages.
- The browser's network tab confirms the request is sent to the correct URL and returns a valid response.
Key Notes
- If you need to send sensitive data or larger payloads, switch the form method to
POSTand create a correspondingsearch-invoice.post.desc.xmldescriptor (copy the GET one and adjust as needed). - For production environments, use AMP packages to deploy your WebScript instead of manual file copies—this simplifies updates and rollbacks.
内容的提问来源于stack exchange,提问作者Ахмед Савмурзаев

