Json, Struts and XWiki 2.1.1


If you want to add a feature in XWiki, something that you cannot already find implemented in XWiki code zone, you should go to the core of this platform. Implementing something new, means interacting , adding new elements to XWiki sources. It means configuring the wiki to recognize your new features and then creating a documentation for all of this that will be helpful for future reference and at XWiki update version.

This isn’t so complicated as it may seam.  If you are adding new features through the Java power,  all you need to do is add the classes in the xwiki classes folder. If you are using another or the same database, all you need to do is add the hbm.xml file for the new table and add it to the classes folder as well, and then map the new xml in the hibernate.cfg file. At every xwiki update, copy those files and updating should take on smoothly.

Lets imagine you want a new personal feature in xwiki, and velocity is not enough for what you want to create. Use Java  🙂 . You are interacting with the user, you want to collect information from him, his actions. You need all of this to “stick” somewhere for a long time, to access it anytime. You need this to be very unreachable for users(with any type of rights), preventing any corruption on it (as much as possible) and as less configuration as possible.

Main Steps:

  1. get user information, actions, data.
  2. Send it to a controller that can manipulate and do something interesting with it . Save it somewhere. Best bet, in a table in database. It can be xwiki database, it can be a new database. This will not interfere later on when updating(will explain later)
  3. Retrieve info (or not) from database for user interface
  4. Document all new features for future references.

Reading the xwiki architecture ( http://platform.xwiki.org/xwiki/bin/view/DevGuide/Architecture ) you can see the flow.

Guidelines for the main steps:

1. Get user data, actions, information

Using: Json, Jquery, Javascript

    – picture in mind a xwiki page where you are interacting with the user. His clicks, his data inserted, anything. Depending on the requirements, using javascript and jquery, get user data, when the page loads, when the user clicks etc.

    – using JSON object in javascript, you can take all the parameters you want to send to the controller, all the information you need for your task in mind.

    Example of usage of json:

    function acquireUserData(param1, param2, param3, actionUrl) {
    $j = jQuery.noConflict();
    $j.getJSON(actionUrl, {param1:param1, param2:param2, param3:param3, action : "save", ajax:"true"});

    This means: you are seding those 4 params(param1/2/3 and action) to an action(the controller i was talking about above).  Action Url reprezents the name of the action that you will have to declare in the “struts-config.xml”.  Follow the existing example, it can look something like this:

    <action path="/mynewaction/"

    You can add another line above: “forward name=”import” path=”/templates/newfile.vm”/” which means you have there velocity, html code. This can be used when you want to show the user a message after he created, save, deleted etc something.

    NOTE: Some articles on the web suggest to use, sytax related: “.get()” instead of .getJSON. A lot of Json details can be found on their official website.

    function readSomethingFromMyActionController(actionUrl, param4,param5) {
    $j = jQuery.noConflict();
    $j.get(actionUrl, {action : "read", param4: param4, param5:param5, ajax : 'true'}, functionShow);

    This is when you want to get some data on page load for instance. The “functionShow” is the callback function.

    functionShow = function(jsondata) {
    alert("Showing loading....");
    //use what json is returning on response

    2. Implement an action Controller in XWiki

    Using: Struts, Json, Java, Eclipse(or other)
    To create an action class in xwiki, simply extend XwikiAction 🙂

    public class MyNewAction extends XWikiAction

    The code you want to execute will have to be placed here :

    public String render(XWikiContext context) throws XWikiException

    These methods can be found searching for overriding existing methods.

    All you need to know is that “Xwiki context ” has it all  🙂 : user, the parameters you send by json earlier etc.

    For instance to get the params send by json just take if from request:

    <em>String param1 = context.getRequest().getParameter("param1");"</em>
    Get the user: <em>" String user = context.getUser();"</em>

    If you want to save data to a table in database, there is a simple way to do it. Create a database connection method, using all the details(url, driver, username, password) from a properties file(but if you do this, you have to change the sources everytimes the db name will change) or take these details from the xwiki.cfg file, using Xwiki Context. This part i did not get, until a collegue showed me explicitly. The clear advantage of this approach is that when you change something in the details needed for connection you edit the xwiki.cfg, not the sources of the new feature.
    So:  “context.getWiki().Param(property);” get’s the parameter from context, from xwiki.cfg file.
    Back to using database in xwiki, saving, retrieving, deleting can be done with pure sql queries.

    To use a new table in xwiki database, you have “specify” it. First, create a new hbm.xml file(use as a n example a xml file like this from xwiki-core.jar), there you specify your fields in the table, and type. Then map this new file in hibernate.cfg file, this means only to add a new line in the data source configuration of the database you are using something like this:
    “<mapping resource=”mynewtable.hbm.xml” />”. And last thing, copy this file in the “WEB-INF\classes” folder.


    –  here you will put the new classes of the new feature you are creating

    – you need to check you have rights to create anything there. If the user has rights. User has to have rights for creating a new table and operating with it.

    And last important thing you need to know about this action controller is that you have to send the end result of your code on response , back to your browser.
    This is done by creating a new JSON Object and putting the processed before data there. If you have more information, use a json array. Then set the type of application on response and write the information on response 🙂

    JSONArray array = new JSONArray();
    for(...) {
    final JSONObject jsonObj = new JSONObject();
    jsonObj.put(<strong>"details"</strong>, details);
    HttpServletResponse response = context.getResponse().getHttpServletResponse();
    try {
    } catch (IOException e) {

    “details” will be sent on response and can be accessed with Javascript, using Json (second example from json part).

    How to integrate my new action in xwiki and see it work?

    – first compile the java dynamic project, copy the classes made on build in the “WEB-INF\classes” folder
    – copy the newtable.hbm.xml in the same place as above
    – check to see you wrote the correct details for database connection in xwiki.cfg file
    – check to see you mapped the new table in hibernate.cfg file
    – check to see you mapped the new action in struts-config.xml file

    It is not tricky and complicated, just a simple flow but  has to be done.

    Good luck.

    NOTE: don’t forget to document your work 😀 and about user rights.

Finally, there’s another very important peculiarity of what does Cialis that brings it so high above its alternatives. It is the only med that is available in two versions – one intended for use on as-needed basis and one intended for daily use. As you might know, Viagra and Levitra only come in the latter of these two forms and should be consumed shortly before expected sexual activity to ensure best effect. Daily Cialis, in its turn, contains low doses of Tadalafil, which allows to build its concentration up in your system gradually over time and maintain it on acceptable levels, which, consequently, makes it possible for you to enjoy sex at any moment without having to time it.

3 thoughts on “Json, Struts and XWiki 2.1.1”
  • This release added annotations, a color theme editor, anonymous commenting with CAPTCHA, and easier development of configurable applications using XWiki.ConfigurableClass.

    April 19, 2010 at 1:19 pm
  • CNA License says:

    Valuable info. Lucky me I found your site by accident, I bookmarked it.

    June 18, 2010 at 2:35 pm
  • Terrific work! This is the type of information that should be shared around the web. Shame on the search engines for not positioning this post higher!

    July 5, 2010 at 5:07 am

Comments are closed.

By continuing to use the site, you agree to the use of cookies. More information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.