Fork me on GitHub

Install with Maven

Currently the JJSON artifacts are only available on my own small repository. You can add the stable repository when adding this to your pom.xml

            <repositories>
                <repository>
                    <id>grobmeier-stable</id>
                    <url>http://code.grobmeier.de/mvn/stable</url>
                </repository>
            </repositories>
            

Then add the following dependency to your project

             <dependency>
                <groupId>de.grobmeier.json</groupId>
                <artifactId>jjson</artifactId>
                <version>0.1.0</version>
             </dependency>
            

Manual Install

Just drop the jar-File(s) into your projects classpath and go for it. At the moment there is no maven repository available online, but this is in the works.

Install the Struts 2 Plugin

Install the JsonResult? class in your struts.xml package:

<result-types>
    <result-type name="json" class="de.grobmeier.json.plugins.struts2.JsonResult"/>
</result-types>
            
Then you can use this result type like that:

<action name="doSomething" class="de.grobmeier.DoSomething">
    <result type="json" />
</action>
            
The result is a json string. Please note, everything you want in your JSON string must be annotated with @JSON, even the Action class it self.

                @JSON
                class DoSomething {
                @JSON public String myString = "test";
                @JSON public String myString2 = "test2";
                // GETTER necessary like getMyString()
                }
            
This will return {"myString" : "test", "myString2" : "test2"} Fields need matching getter methods. You can even annotate methods instead of the fields, if you would like to to return a object created on the fly:

                @JSON
                class DoSomething {
                @JSON public void blub() { return "test"; }
                }
            
This will return {"blub" : "test"}