10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Java</strong>-to-Schema Examples<br />

542<br />

int zip;<br />

public int getZip(){return zip;}<br />

public void setZip(int z){zip=z;}<br />

Within a bean class, you have the choice of setting the @XmlAttribute annotation on one of<br />

three components: the field, the setter method, or the getter method. If you set the<br />

@XmlAttribute annotation on the field, the setter method will need to be renamed or there will<br />

be a naming conflict at compile time. If you set the @XmlAttribute annotation on one of the<br />

methods, it must be set on either the setter or getter method, but not on both.<br />

<strong>The</strong> XmlAttribute Field example shows how to use the @XmlAttribute annotation on a static<br />

final field, on a field rather than on one of the corresponding bean methods, on a bean property<br />

(method), and on a field that is other than a collection type. In class USAddress, fields, country,<br />

and zip are tagged as attributes. <strong>The</strong> setZip method was disabled to avoid the compile error.<br />

Property state was tagged as an attribute on the setter method. You could have used the getter<br />

method instead. In class PurchaseOrderType, field cCardVendor is a non-collection type. It<br />

meets the requirement of being a simple type; it is an enum type.<br />

Building and Running the XmlAttribute Field Example Using NetBeans<br />

IDE<br />

Follow these instructions to build and run the XmlAttribute Field example on your Application<br />

Server instance using NetBeans IDE.<br />

1. In NetBeans IDE, select File→Open Project.<br />

2. In the Open Project dialog, navigate to tut-install/javaeetutorial5/examples/jaxb/.<br />

3. Select the j2s-xmlAttribute-field folder.<br />

4. Select the Open as Main Project check box.<br />

5. Click Open Project.<br />

6. Right-click the j2s-xmlAttribute-field project and select Run.<br />

Building and Running the XmlAttribute Field Example Using Ant<br />

To compile and run the XmlAttribute Field example using Ant, in a terminal window, go to the<br />

tut-install/javaeetutorial5/examples/jaxb/j2s-xmlAttribute-field/ directory and type<br />

the following:<br />

ant runapp<br />

XmlRootElement Example<br />

<strong>The</strong> XmlRootElement example demonstrates the use of the @XmlRootElement annotation to<br />

define an XML element name for the XML schema type of the corresponding class.<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!