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

raspBerry+


raspBerry+ is a web-based administration platform for Blackberry Enterprise Server for MS Exchange (BES). You can group-based activate/kill/delete/add and get status of users, their handhelds and services. With a little download-area and a comment-system

RASP


RASP's A Sneakernet Proxy; download using a thumbdrive.

RasmusDSP


RasmusDSP is an embeddable Audio/MIDI processor. It contains various filters and generators (including SoundFont 2.0 compatible synthesizer). Has a script interpreter which is used to describe instruments, route Audio/MIDI signal between processor units.

Rasea


An acronym for cRoss-plAtform accesS control for Enterprise Applications. Rasea aims to become a reference in access control as a service based on the RBAC model.

Rascal


Rascal, the Advanced Scientific CALculator, is a platform independent modular calculator. Based on modules for integer, doubles, strings, vectors and matrices it can be easily extended with existing C or C++ code.



Rars


RARS is the Robot Auto Racing Simulation, in which the drivers are robot programs. It is intended as a competition among programmers. It consists of a simulation of the physics of cars, a graphic display of the race, and a robot driver for each car.

RARPlayer


This small program allows you to play a video directly from a RAR file and do so in real-time. Both VLC and MPlayer are supported video players.

RAReXtract


RAReXtract is a Front-End for the UnRAR command line utility for Mac OS X 10.5 (Leopard). Its purpose is the rapid and convenient extraction of RAR archives with a double click.

RAR Expander


Rar Expander is a MacOSX program which extracts the files contained in single or multi-volume RAR archives. It uses the official unRAR library internally so it is fully compatible with archives produced by WinRAR.

rarcrack


This program uses a brute force algorithm to guess your encrypted compressed file\'s password. If you forget your encrypted file password, this program is the solution. This program can crack zip,7z and rar file passwords.

RArcInfo


RArcInfo is a package for R (http://www.r-project.org) to import data from binary Arc/Info V7.X coverages and E00 files . This will allow R users to used it as a primary GIS tool.

rar brute force shell script - rarbrute


This is rarbrute, a shell script to brute force encrypted rar files under unix and linux. A long wordlist and a paper about security in internet cafes is included.

Raquel Database System


The system will : 1. use RAQUEL (= Relational Algebra Query, Update and Executive Language) for programming, implementing Third Manifesto principles. 2. have a 'Lego-like' architecture of building blocks and plug-ins, for wider applicability.

RAPv4


RAPv4 is an engine for building web application with only a business description (in XML format). NEW 04/2006 : Stable 2006 release. Add new functions like mail, sms, web services, graph, map engine (GIS), Excel output, QBE... and also a beta release of

Rafkill


2d Scroller. Clone of Raptor: Call of the Shadows and Tyrian. Fun game written in c++ using allegro.

rapple


Lightweight XML based transformation tool written in C that builds upon expat, tidylib and XSLT to tranform authored web content (incl. Word processor generated HTML) into styled web content suitable for publication.

RapidSMS


RapidSMS is an open-source internet and communications platform

RapidSmith


RapidSmith is a research-based FPGA CAD tool framework written in Java for modern Xilinx FPGAs. Based on XDL, its objective is to serve as a rapid prototyping platform for research ideas and algorithms relating to low level FPGA CAD tools.

Rapidshare Mass Downloader


What this program does is bringing out human interaction while downloading files from rapidshare(without premium account). It downloads all the rapidshare links sequentially to the specified location.

rapido visual profiler


rapido is a visual profiler for linux-x86. It traces function call using the ptrace interface and displays the information collected in a nice visual flow chart. rapido does not require the re-compilation of the application.