Struts2-jaxb-plugin - A Struts 2 plug-in that allows you to utilize JAXB to post XML to an Action or

  •        319

I was doing quite a bit of web 2.0 development that included the generation of XML to and from domain objects within my application. This was an incredibly tedious task to write custom generation and parsing off so then I decided to make use of JAXB. From the first few examples I created using JAXB I noticed a lot of boilerplate code that would be created so then i decided to to use an intercepter and result type (within struts 2) to remove the need for all of this. So thats when the first version was born and from there its just naturally matured to the state it is in now. Simple How ToSo there is a lot of JavaDoc on the intercepter and result type declared in the jar file, but i'd annoy a lot of people if i directed you to that. So i'll give you a more detailed step by step how-to for the most basic scenario in how to use this plug-in. This example is going to assume your using Java 1.6 and know how to use annotations, since its the simplest scenario. A Simple JAXB EntitySo create a simple java bean and give it the proper JAXB annotations. package example;import javax.xml.bind.annotation.XmlRootElement;/** * @author You! */ //JAXB@XmlRootElement public class SimpleBean {private String value;public SimpleBean(){}public String getValue() {return value;}public void setValue(String value) {this.value = value;} }As you can see this is just a simple bean, that also contains the JAXB @XmlRootElement annotation. jaxb.indexSo now that you have a JAXB annotated bean you need to tell JAXB that it is okay to be marshalled and unmarshalled. You do this by adding a regular text file named jaxb.index in the package with the bean you just created. Here is what your jaxb.index should contain. SimpleBeanThe format of this file is really simple as you can see. So its worth noting that you could also use an ObjectFactory to initialize your JAXB entities. However I'm not going to go into it for 2 reasons. I only ever use the jaxb.index, and its the simpler scenario. If you already use an ObjectFactory or want to use one, do a quick google or just make the necessary adjustments. ActionNow that you have an entity you need something to process your request and initialize your entity. So the below example action does just that. package example; /** * @author You! */ public class SimpleAction {private SimpleBean simpleBean;public SimpleAction(){}public execute(){this.simpleBean = new SimpleBean();this.simpleBean.setValue("Hello World");return "simpleBeanResult";}public String getSimpleBean() {return simpleBean;}public void setSimpleBean(String simpleBean) {this.simpleBean = simpleBean;} }So when this action is executed it will create the member level variable, simpleBean and set its value to "Hello World". Nothing worth noting here. Add The Plug-inSo now you need to add the plug-in to your class path. This is typicially done by placing it into the WEB-INF\\lib directory of your Web Project. Struts.xml Configuration Now we need to do a handful of things to tell struts to use the action and utilize the plug-in's capabilities. I'll give you the example first and go over it below. example.simple simpleBean So there are 3 main things of note within this struts.xml file. The first is on the package element where we extend "jaxb-communicate" (probably not the greatest name in the world). This tells struts to extend the declarations defined in the struts2-jaxb-plugin.jar that you've added to your classpath. The next thing you should notice is the declaration of "xmlUnMarshaller". You don't see a declaration of this object because its already defined in the struts.xml of the struts2-jaxb-plugin.jar. The last thing you see is within the action definition. There is a result with the type of "xmlMarshalling". This again points to a result type defined in the struts.xml file of the struts2-jaxb-plugin.jar. This result type has 2 attributes, packageContext and objectToMarshall. So thes are pretty simple. The packageContext points to the package with the jaxb.index file and your java bean (SimpleBean). The objectToMarshall attribute points to the bean defined on the Action that you want marshalled into XML. Execute your testSo now if you start up struts and go to your application and go to simpleAction.action you will see the output. http://code.google.com/p/struts2-jaxb-plugin


Tags
Implementation
License
Platform

   




Related Projects

vim-olive - Vim Mode Line Verifier


Vim Mode Line Verifier

vim-mash - Motion Activated Search Highlighter for Vim


Motion Activated Search Highlighter for Vim

vim-grillz - Flash your wicked grillz!


Flash your wicked grillz!

vim-foist - Complete whole lines from any partial therein


Complete whole lines from any partial therein

vim-efmc - Vim Error Format Compiler


Vim Error Format Compiler


uzbl-utrs - mkng uzbl tlrbl


mkng uzbl tlrbl

toycsv - A toy CSV parser written in ruby + lexr + racc


A toy CSV parser written in ruby + lexr + racc

tiktok - TikTok provides a simple asynchronous timer object for VimL.


TikTok provides a simple asynchronous timer object for VimL.

tabby - Using Vim's Tabs the Right Way


Using Vim's Tabs the Right Way

SohiVila - GNU source-highlight Vim language


GNU source-highlight Vim language

SinTax - A DSL for generating Vim syntax highlighting files


A DSL for generating Vim syntax highlighting files

rkdots - Generate a graphviz visualisation of the given javascript statement.


Generate a graphviz visualisation of the given javascript statement.

RelNumBar - Show relativenumbers alongside normal numbers in Vim


Show relativenumbers alongside normal numbers in Vim

Punisher - Punisher hurts you where it hurts most - your time.


Punisher hurts you where it hurts most - your time.

noisy - Don't chat quietly (weechat channel noises)


Don't chat quietly (weechat channel noises)

newlisp-manual - Asciidoc version of the newLISP manual


Asciidoc version of the newLISP manual

Land-of-newLISP - Selected snippets from Land of Lisp rewritten in newLISP


Selected snippets from Land of Lisp rewritten in newLISP

Fossilise - Poor man's collaborative editing in Vim


Poor man's collaborative editing in Vim

firstly - Convert Between Numeric, Spelt, and Short & Long Ordinal Forms of Numbers


Convert Between Numeric, Spelt, and Short & Long Ordinal Forms of Numbers