How to use GET/POST in xwiki
I have a question regarding the use of GET/POST in xwiki. How do I send to a velocity script a string using GET or maybe something similar (like $context??) in xwiki? I read something about xwiki context, like getResponse() and getRequest(), but I would like an example to understand better, because the api is very lacunal. Thank you. Evelina Slatineanu
hi evelina, A not too extensive but very good conceptual introduction is this one: http://click.sourceforge.net/docs/velocity/VelocityUsersGuide.pdf 43 pages that are worth reading. I am not sure if I did entirely understand your question. Do you want to set a variable in a velocity template programatically (i.e. setting it in the template itself), like #set($force = $!request.get("force")) Or do you want to remote control an existing wiki page (that in the background is made up of velocity templates)? The following method makes use of the apache commons httpclient to establish a connection to xwiki and delete a group from the wiki. Basically, it performs an http POST to the page of the group "group". If you have a look at com.xpn.xwiki.web.DeleteAction, you see, that the parameter "confirm" must be set to 1, if DeleteAction.action() should succeed. Thence, in the method below I set this parameter to 1 (to avoid a javascript alert / confirmation page). Hope that helps Thomas Note, that you have to perform a login first, if you want that method to succeed. /** * Removes a group from the xwiki user management * Author : Thomas Kraemer <mailto:[email protected]> * Creation Date : 02.05.2007 00:57:37 * @param group */ public void removeGroup(String group) { try { result = 0; PostMethod remGroup = new PostMethod(server + "/xwiki/bin/delete/XWiki/"+group + "?confirm=1&language=en"); httpclient.executeMethod(remGroup); remGroup.releaseConnection(); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } [email protected] schrieb:
I have a question regarding the use of GET/POST in xwiki. How do I send to a velocity script a string using GET or maybe something similar (like $context??) in xwiki? I read something about xwiki context, like getResponse() and getRequest(), but I would like an example to understand better, because the api is very lacunal. Thank you. Evelina Slatineanu
------------------------------------------------------------------------
-- You receive this message as a subscriber of the [email protected] mailing list. To unsubscribe: mailto:[email protected] For general help: mailto:[email protected]?subject=help ObjectWeb mailing lists service home page: http://www.objectweb.org/wws
-- ontopica Thomas Krämer Krämer&Okpue GbR Kurfürstenstr. 66 53115 Bonn Fon 0228 - 180 99 737 Fax 0228 - 242 78 60 Email [email protected]
Evelina: Congratulations; you sent me to the dictionary in my own language! I presume that you mean that the documentation is full of holes, which is charitable of you; like most software not sold commercially, it's more like one large hole, in which one occasionally finds something useful. One positive comment is that (aside from giving two classes the same name) the Javadocs are as useful as they can be with virtually no comments because the names of methods and their arguments are about as meaningful as one could hope for. What the Velocity manual can't tell you, of course, is what can be found in XWiki's Velocity context, and that's the whole story. I don't remember where I found the following; it's been a long time. There are five objects placed in the Velocity context available to a user page by the XWiki action servlet. They are: * xwiki - XWiki object wrapper (class com.xpn.xwiki.api.XWiki, subclassed from com.xpn.xwiki.api.Api) giving rights-controlled access to the core XWiki object (class com.xpn.xwiki.XWiki) * context - the XWiki context, (class com.xpn.xwiki.api.Context), an access-controlled wrapper for the XWikiContext object, which also contains a reference to the Velocity context from which you got it * doc - com.xpn.xwiki.api.Document, API wrapper for the com.xpn.xwiki.doc.XWikiDocument representing the current document * request - com.xpn.xwiki.web.XWikiRequest, an XWiki-specific wrapper for the HttpServletRequest or HttpPortletRequest to which the current action is responding * response - com.xpn.xwiki.web.XWikiResponse, an XWiki-specific wrapper for the HttpServletResponse or HttpPortletResponse with which the action will respond A helpful way to tie the XWiki Velocity script stuff to the actual Java APIs is: starting from those five objects, you can get all of their properties to which Velocity, XWiki, and the Java permissions structure will allow you access, and since all of them inherit from java.lang.Object, you can get their class names by appending ".class" to the variable reference (which Velocity translates to ".getClass()") to display its Class object, whose toString() method is invoked to return its fully-qualified classname. From that, you can go to the Javadocs for the named class; that's how I did most of my debugging before I started running XWiki under Eclipse. Looking at the J2EE Servlet API (available from java.sun.com) will show that the HttpServletRequest interface has methods to get the various pieces of the request, of which the GET parameters are found in method getQueryString(), which XWikiRequest wraps in a method called getParameter(paramName), which through some magic or other XWiki tells Velocity to call with the reference $request.paramName. As an aside, note that it actually doesn't matter whether the HTTP request method is GET or POST as far as XWikiRequest is concerned; the value of a form field named "paramName" will be returned by XWikiRequest.getParameter(paramName), and hence $request.paramName within Velocity, in exactly the same way as if it were in the GET query string. So, to sum up: if a browser sends XWiki the request URL http://myhost.org/xwiki/bin/view/Me/MyDoc?param1Name=foo¶m2Name=bar, then the XWiki document Me.MyDoc will find $request.param1Name returns "foo", and that $request.param2Name is "bar". See the Javadocs for more methods to get a Map of all the name/value pairs, or an Enumeration of just the names, etc. Further, the Velocity #foreach directive works great for stepping through nearly any kind of standard Java collection you can throw at it. My apologies if I misunderstood the question, of course, but I think that should get you started... brain[sic]
-----Original Message----- From: [email protected] [mailto:[email protected]] Sent: Thursday, May 10, 2007 2:45 AM To: [email protected] Subject: [xwiki-users] How to use GET/POST in xwiki
I have a question regarding the use of GET/POST in xwiki. How do I send to a velocity script a string using GET or maybe something similar (like $context??) in xwiki? I read something about xwiki context, like getResponse() and getRequest(), but I would like an example to understand better, because the api is very lacunal. Thank you. Evelina Slatineanu
participants (3)
-
evelyne24@gmail.com -
Thomas Krämer -
THOMAS, BRIAN M (ATTSI)