﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-网路冷眼@BlogJava-文章分类-Java</title><link>http://www.blogjava.net/lewhwa/category/17348.html</link><description>熙熙攘攘一闲人 以冷静的眼光观察技术</description><language>zh-cn</language><lastBuildDate>Tue, 08 May 2007 10:47:44 GMT</lastBuildDate><pubDate>Tue, 08 May 2007 10:47:44 GMT</pubDate><ttl>60</ttl><item><title>Using Hibernate-specific features with JPA</title><link>http://www.blogjava.net/lewhwa/articles/115786.html</link><dc:creator>网路冷眼@BlogJava</dc:creator><author>网路冷眼@BlogJava</author><pubDate>Mon, 07 May 2007 15:34:00 GMT</pubDate><guid>http://www.blogjava.net/lewhwa/articles/115786.html</guid><wfw:comment>http://www.blogjava.net/lewhwa/comments/115786.html</wfw:comment><comments>http://www.blogjava.net/lewhwa/articles/115786.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lewhwa/comments/commentRss/115786.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lewhwa/services/trackbacks/115786.html</trackback:ping><description><![CDATA[<div id=page>原文地址：<a href="http://wiki.netbeans.org/wiki/view/UsingHibernateWithJPA">http://wiki.netbeans.org/wiki/view/UsingHibernateWithJPA</a>
<div id=pagecontent>
<h3 id=section-UsingHibernateWithJPA-UsingHibernateSpecificFeaturesWithJPA>Using Hibernate-specific features with JPA</h3>
<p>The Java Persistence API offers a powerful standard for persisting POJOs.It includes all the most important features which you would expect from an object-relational mapping tool, but there are still some areas where you might need to use vendor-specific features. In this tutorial, we will show how to use Hibernate-specific features for validating and querying while using the standard API for the rest. </p>
<h4 id=section-UsingHibernateWithJPA-Prerequisites>Prerequisites</h4>
<p>This tutorial assumes you have some basic knowledge of, or programming experience with, the following technologies: </p>
<ul>
    <li>Java Server Faces
    <li>Java Persistence API.
    <li>NetBeans IDE </li>
</ul>
<p>This tutorial is partly based on the <a class=external href="http://www.netbeans.org/kb/articles/hibernate-javaee.html"><u><font color=#800080>Using Hibernate With Java Persistence API</font></u></a><u><font color=#800080><img class=outlink alt="" src="http://wiki.netbeans.org/wiki/images/out.png"></font></u> tutorial, you might want to go through it first. </p>
<h4 id=section-UsingHibernateWithJPA-SoftwareNeededForTheTutorial>Software Needed for the Tutorial</h4>
<p>Before you begin, you need to install the following software on your computer: </p>
<ul>
    <li>NetBeans IDE 5.5.1 (<a class=external href="http://www.netbeans.info/downloads/dev.php"><u><font color=#800080>download</font></u></a><u><font color=#800080><img class=outlink alt="" src="http://wiki.netbeans.org/wiki/images/out.png"></font></u>)
    <li>Sun Java System Application Server, Platform Edition 9 (<a class=external href="http://www.sun.com/software/products/appsrvr_pe/index.xml"><u><font color=#0000ff>download</font></u></a><u><font color=#0000ff><img class=outlink alt="" src="http://wiki.netbeans.org/wiki/images/out.png"></font></u>)
    <li>Hibernate Core 3.2.1.GA, Hibernate EntityManager 3.2.1.GA(<a class=external href="http://www.hibernate.org/6.html"><u><font color=#0000ff>download</font></u></a><u><font color=#0000ff><img class=outlink alt="" src="http://wiki.netbeans.org/wiki/images/out.png"></font></u>) </li>
</ul>
<h4 id=section-UsingHibernateWithJPA-SettingUpYourEnvironment>Setting up your Environment</h4>
<p>First we will create a library in the IDE for the Hibernate entity manager. </p>
<ol>
    <li>Unzip the Hibernate JAR files to any temporary location
    <li>Choose Tools &gt; Library Manager from the main menu. Click New Library, name the library Hibernate, and click OK.
    <li>In the Libraries pane of the Library Manager dialog box, select the Hibernate library you just created.
    <li>Click the Classpath tab and then click Add JAR/Folder and locate the following JAR files: </li>
</ol>
<ul>
    <li>From Hibernate EntityManager:
    <ul>
        <li>hibernate-entitymanager.jar
        <li>lib/hibernate-annotations.jar
        <li>lib/jboss-archive-browsing.jar
        <li>lib/javassist.jar </li>
    </ul>
    <li>From Hibernate Core:
    <ul>
        <li>hibernate3.jar
        <li>lib/antlr-2.7.6.jar
        <li>lib/asm-attrs.jar
        <li>lib/asm.jar
        <li>lib/c3p0-0.9.0.jar
        <li>lib/cglib-2.1.3.jar
        <li>lib/commons-collections-2.1.1.jar
        <li>lib/concurrent-1.3.2.jar
        <li>lib/commons-logging-1.0.4.jar
        <li>lib/dom4j-1.6.1.jar
        <li>lib/ehcache-1.2.3.jar
        <li>lib/log4j-1.2.11.jar </li>
    </ul>
    </li>
</ul>
<p>
<h4 id=section-UsingHibernateWithJPA-CreatingAWebApplication>Creating a Web Application</h4>
Next we will create a web application with entity classes and a JSF interface. To speed up the process, we will use the CRUD generators in the IDE.
<ul>
    <li>Choose File &gt; New Project (Ctrl-Shift-N). Select Web Application from the Web category.
    <li>Name the project HibernateWithJPA, set the server to Sun Java System Application Server, the Java EE version to Java EE 5 and make sure that the Set Source Level to 1.5 checkbox is checked. Click Next.
    <li>Select the JavaServer Faces framework and click Finish.
    <li>Add the Hibernate library we created earliead to the project by right-clicking the project node and choosing Properties &gt; Libraries &gt; Add Library &gt; Hibernate
    <li>In the Projects window, right-click the project node and choose New &gt; Entity Classes from Database.
    <li>In the Data Source drop-down, select the <tt>jdbc/sample</tt> database. If prompted for a user name and password, use app and app. Select the <tt>CUSTOMER</tt> table from the list, click Add and then click Next. * Leave the tables and class names as their default, and set the package to <tt>sample.entities</tt>.
    <li>Now it is time to create the persistence unit. Click the Create Persistence Unit button. Accept the default name, select Hibernate as the persistence provider, and select None as the table generation strategy. Click OK.
    <li>Click Finish in the Entity Classes from Database wizard. The IDE creates the persistence unit (<tt>persistence.xml</tt> under Configuration Files node) and the entity classes (under Source Packages).
    <li>Set a correct SQL dialect for Hibernate. Double-click <tt>persistence.xml</tt> and click the XML button at the top of the editor to switch to the XML view. Add the following property to the persistence unit: </li>
</ul>
<pre>        &lt;property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" /&gt;
</pre>
<h4 id=section-UsingHibernateWithJPA-CodingTheJSFInterface>Coding the JSF Interface</h4>
<p>Now we can quickly generate JSF pages for the entity classes with the NetBeans IDE CRUD generation. </p>
<ol>
    <li>Right-click the project node and choose New &gt; JSF Pages from Entity Class. Add both <tt>Customer.java</tt> and <tt>DiscountCode.java</tt> to the list of selected classes and click Next.
    <li>Change the package to <tt>sample.controller</tt> and click Finish. </li>
</ol>
<img class=inline alt="JSF from entities" src="http://wiki.netbeans.org/wiki/attach/UsingHibernateWithJPA/jsf-from-entities.png">
<h4 id=section-UsingHibernateWithJPA-TestingTheProject>Testing the Project</h4>
<p>At this point we will try to run the application and see whether everything is working as expected. </p>
<ol>
    <li>Right-click the project and choose Run Project. The IDE starts the application server, builds and deploys the application, and shows <tt>index.jsp</tt> in the external web browser.
    <li>Click the List of Customer link and you should see the following web page: </li>
</ol>
<img class=inline alt="list of customers" src="http://wiki.netbeans.org/wiki/attach/UsingHibernateWithJPA/list-of-customers.png">
<ul>
    <li>Use the New Customer link to create a customer </li>
</ul>
<h4 id=section-UsingHibernateWithJPA-AddingValidation>Adding validation</h4>
<p>When creating a new customer you might have noticed that values were not validated before attempting to persist the newly created customer. We could use the validation facilities in JSF for ensuring that only correct values are inserted, but since this tutorial is about Hibernate we will instead demonstrate how to use the Hibernate validation framework. This approach has an additional advantage that the validation rules need to be specified only once even if another type of client is added. </p>
<p>Open <tt>Customer.java</tt> in the editor and add the following annotations on its member variables: </p>
<pre>    @Column(name = "STATE")
@Length(min=2, max=2, message="not a valid state")
private String state;
@Column(name = "EMAIL")
@Email
private String email;
</pre>
For simplicity's sake we will only add validation to <tt>state</tt> and <tt>email</tt> properties. The <tt>@Length</tt> annotation validates that the length of the property will be within the range specified by <tt>min</tt> and <tt>max</tt> attributes. We apply it on the <tt>state</tt> property to make sure that it is exactly 2 characters long. We also specify a value for the <tt>message</tt> attribute, it will be the shown error message when the validation fails. The <tt>@Email</tt> annotation in turn validates that the property represents a valid email address, as you might have guessed.
<p>Now that we have our domain object annotated, we need to add handling of validation errors to our controller class. So open <tt>CustomerController.java</tt> and add the following method there: </p>
<pre>    /**
* Validates the given customer.
* @return true if there were no validation errors, false otherwise.
*/
private boolean validate(Customer customer){
ClassValidator customerValidator = new ClassValidator(Customer.class);
// get the invalid values
InvalidValue[] msgs = customerValidator.getInvalidValues(customer);
if (msgs.length &gt; 0){
for(InvalidValue msg : msgs){
// add an error message for each invalid value, these
// messages will be shown to the user
addErrorMessage(msg.getMessage());
}
return false;
}
return true;
}
</pre>
This method will first create a class validator for our Customer class and then process the validation rules we specified earlier when. We collect the invalid value messages and add each of them as error messages to the <tt>FacesContext</tt> (this is done by the <tt>addErrorMessage</tt> method). If there were no validation errors the will return true, false otherwise. Of course, as such this method is not very useful unless we invoke it in the right places. We probably want to validate the values both when a new customer is created and when an existing customer is edited. So let's first modify the <tt>create</tt> method to check whether there were any validation errors before attempting to persist:
<pre>    public String create() {
if (!validate(customer)){
// return to the input page if there were any validation errors
return null;
}
EntityManager em = getEntityManager();
try {
utx.begin();
em.joinTransaction();
em.persist(customer);
...
</pre>
<p>As you can see, we return <tt>null</tt> if any errors were found - this means JSF will display the same page again. Make a similar modification to the <tt>edit</tt> method as well and run the application. Try to create a new customer with an invalid email address and with a 3 characters long state code. This is what you should see: <img class=inline alt="not a valid customer" src="http://wiki.netbeans.org/wiki/attach/UsingHibernateWithJPA/not-valid-customer.png"> </p>
<h4 id=section-UsingHibernateWithJPA-QueryByExample>Query by Example</h4>
<p>While the Java Persistence QL is an impressive query language, there are cases when a different kind of API is more suitable. Luckily, in addition to JPQL support, Hibernate features a criteria query API which you can leverage for the cases it is needed and stick to the standard API elsewhere in the application. In the following example we will demonstrate the Query By Example approach using Hibernate's Criteria API. </p>
<p>First we need to create a new page for our new query functionality. Create a new page named <tt>Query.jsp</tt> in the customer folder and paste the following to it: </p>
<pre>&lt;%@page contentType="text/html"%&gt;
&lt;%@page pageEncoding="UTF-8"%&gt;
&lt;%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %&gt;
&lt;%@taglib uri="http://java.sun.com/jsf/html" prefix="h" %&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;Query By Example&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;f:view&gt;
&lt;h1&gt;Query By Example&lt;/h1&gt;
&lt;h:messages errorStyle="color: red" infoStyle="color: green" layout="table"/&gt;
&lt;h:form&gt;
&lt;h:panelGrid columns="2"&gt;
&lt;h:outputText value="Zip:"/&gt;
&lt;h:inputText id="zip" value="#{customer.customer.zip}" converter="stringConverter" title="Zip" /&gt;
&lt;h:outputText value="Name:"/&gt;
&lt;h:inputText id="name" value="#{customer.customer.name}" converter="stringConverter" title="Name" /&gt;
&lt;h:outputText value="State:"/&gt;
&lt;h:inputText id="state" value="#{customer.customer.state}" converter="stringConverter" title="State" /&gt;
&lt;/h:panelGrid&gt;
&lt;h:commandLink action="#{customer.queryByExample}" value="Search"/&gt;
&lt;/h:form&gt;
&lt;h:form&gt;
&lt;a href="/HibernateWithJPA/index.jsp"&gt;Back to index&lt;/a&gt;
&lt;br&gt;
&lt;h:dataTable value='#{customer.model}' var='item' border="1" cellpadding="2" cellspacing="0"&gt;
&lt;h:column&gt;
&lt;f:facet name="header"&gt;
&lt;h:outputText value="CustomerId"/&gt;
&lt;/f:facet&gt;
&lt;h:commandLink action="#{customer.detailSetup}" value="#{item.customerId}"/&gt;
&lt;/h:column&gt;
&lt;h:column&gt;
&lt;f:facet name="header"&gt;
&lt;h:outputText value="Zip"/&gt;
&lt;/f:facet&gt;
&lt;h:outputText value="#{item.zip}"/&gt;
&lt;/h:column&gt;
&lt;h:column&gt;
&lt;f:facet name="header"&gt;
&lt;h:outputText value="Name"/&gt;
&lt;/f:facet&gt;
&lt;h:outputText value="#{item.name}"/&gt;
&lt;/h:column&gt;
&lt;h:column&gt;
&lt;f:facet name="header"&gt;
&lt;h:outputText value="Addressline1"/&gt;
&lt;/f:facet&gt;
&lt;h:outputText value="#{item.addressline1}"/&gt;
&lt;/h:column&gt;
&lt;h:column&gt;
&lt;f:facet name="header"&gt;
&lt;h:outputText value="Addressline2"/&gt;
&lt;/f:facet&gt;
&lt;h:outputText value="#{item.addressline2}"/&gt;
&lt;/h:column&gt;
&lt;h:column&gt;
&lt;f:facet name="header"&gt;
&lt;h:outputText value="City"/&gt;
&lt;/f:facet&gt;
&lt;h:outputText value="#{item.city}"/&gt;
&lt;/h:column&gt;
&lt;h:column&gt;
&lt;f:facet name="header"&gt;
&lt;h:outputText value="State"/&gt;
&lt;/f:facet&gt;
&lt;h:outputText value="#{item.state}"/&gt;
&lt;/h:column&gt;
&lt;h:column&gt;
&lt;f:facet name="header"&gt;
&lt;h:outputText value="Phone"/&gt;
&lt;/f:facet&gt;
&lt;h:outputText value="#{item.phone}"/&gt;
&lt;/h:column&gt;
&lt;h:column&gt;
&lt;f:facet name="header"&gt;
&lt;h:outputText value="Fax"/&gt;
&lt;/f:facet&gt;
&lt;h:outputText value="#{item.fax}"/&gt;
&lt;/h:column&gt;
&lt;h:column&gt;
&lt;f:facet name="header"&gt;
&lt;h:outputText value="Email"/&gt;
&lt;/f:facet&gt;
&lt;h:outputText value="#{item.email}"/&gt;
&lt;/h:column&gt;
&lt;h:column&gt;
&lt;f:facet name="header"&gt;
&lt;h:outputText value="CreditLimit"/&gt;
&lt;/f:facet&gt;
&lt;h:outputText value="#{item.creditLimit}"/&gt;
&lt;/h:column&gt;
&lt;h:column&gt;
&lt;f:facet name="header"&gt;
&lt;h:outputText value="DiscountCode"/&gt;
&lt;/f:facet&gt;
&lt;h:outputText value="#{item.discountCode}"/&gt;
&lt;/h:column&gt;
&lt;h:column&gt;
&lt;h:commandLink value="Destroy" action="#{customer.destroy}"&gt;
&lt;f:param name="customerId" value="#{item.customerId}"/&gt;
&lt;/h:commandLink&gt;
&lt;h:outputText value=" "/&gt;
&lt;h:commandLink value="Edit" action="#{customer.editSetup}"&gt;
&lt;f:param name="customerId" value="#{item.customerId}"/&gt;
&lt;/h:commandLink&gt;
&lt;/h:column&gt;
&lt;/h:dataTable&gt;
&lt;/h:form&gt;
&lt;/f:view&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
There are a couple of things to note here. Firstly, we have specified "stringConverter" converter for the input fields. Secondly, the 'search' link will try to execute <tt>customer.queryByExample</tt> method, which we haven't yet implemented. Thirdly, the data table uses <tt>customer.model</tt> as its underlying model. We will get back to these in a minute, but before that we still need to create a link to our new page. To keep things simple, we will just add it to the <tt>customer/List.jsp</tt> page, right after the link to the New Customer page:
<pre>&lt;h:commandLink action="#{customer.createSetup}" value="New Customer"/&gt;
&lt;br&gt;
&lt;h:commandLink action="#{customer.querySetup}" value="Query Customers"/&gt;
</pre>
<p>Now, this link will cause <tt>querySetup</tt> method to be invoked in the <tt>CustomerController.java</tt>, so let's implement it next. To do that, add the following to the <tt>CustomerController.java</tt>: </p>
<pre>   public String querySetup(){
this.customer = new Customer();
this.model = null;
return "customer_query";
}
</pre>
<p>And to complete the UI side of things, we still need to add a navigation rule to <tt>faces-config.xml</tt>: </p>
<pre>&lt;navigation-rule&gt;
&lt;navigation-case&gt;
&lt;from-outcome&gt;customer_query&lt;/from-outcome&gt;
&lt;to-view-id&gt;/customer/Query.jsp&lt;/to-view-id&gt;
&lt;/navigation-case&gt;
&lt;/navigation-rule&gt;
</pre>
<p>What is still missing is the actual implementation of the query, and the converter we mentioned earlier. Let's tackle the implementation of the query method first. In the <tt>Query.jsp</tt> page we defined that the 'Search' link will invoke <tt>customer.queryByExample</tt> method and that <tt>customer.model</tt> will be used for the data table. To satisfy the latter, we just need to create a getter for the model in <tt>CustomerController.java</tt> - press <tt>ctrl-space</tt> and choose <tt>'create getter getModel for field model'</tt>. After that, add the following method: </p>
<pre>    /**
* Queries customers based on the values in our &lt;code&gt;customer&lt;/code&gt;.
*/
public String queryByExample(){
// get the native hibernate session
Session session = (Session) getEntityManager().getDelegate();
// create an example from our customer, exclude all zero valued numeric properties
Example customerExample = Example.create(customer).excludeZeroes();
// create criteria based on the customer example
Criteria criteria = session.createCriteria(Customer.class).add(customerExample);
// perform the query and set the result to our model.
this.model = new ListDataModel(criteria.list());
return "customer_query";
}
</pre>
<p>You can see how easily you can access Hibernate's native API - just invoke <tt>getDelegate()</tt> on the entity manager and cast it to <tt>org.hibernate.Session</tt>. Once we have the access to <tt>Session</tt> we can take advantage of the Criteria API. In the above method we create an example criteria based on the customer and execute the query. If you are not familiar with the Criteria API, it is probably worth explaining a bit. By default, all <tt>null</tt> values will be excluded, which means that the properties on our <tt>customer</tt> that were <tt>null</tt>, will not be part of the criteria. In addition, we specify that all zero valued numeric properties will be excluded as well. Here it gets a bit complicated: since JSF by default converts strings without values to empty strings (instead of nulls), we need to create a special converter for dealing with the conversion of strings from the query page. The implementation of the converter is as simple as: </p>
<pre>package sample.controller;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
/**
* A converter for string values that does not
* convert &lt;code&gt;null&lt;/code&gt; values to empty strings.
*/
public class StringConverter implements Converter{
/** Creates a new instance of StringConverter */
public StringConverter() {
}
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if(value == null || "".equals(value)){
return null;
}
return value;
}
public String getAsString(FacesContext context, UIComponent component, Object value) {
return value != null ? value.toString() : null;
}
}
</pre>
<p>Don't forget to register the converter in <tt>faces-config.xml</tt>: </p>
<pre>&lt;converter&gt;
&lt;converter-id&gt;stringConverter&lt;/converter-id&gt;&gt;
&lt;converter-class&gt;sample.controller.StringConverter&lt;/converter-class&gt;
&lt;/converter&gt;
</pre>
<p>Finally, we are ready to run the application once again and test the new functionality: </p>
<p><img class=inline alt="query by exmpale" src="http://wiki.netbeans.org/wiki/attach/UsingHibernateWithJPA/query-by-example.png"> </p>
<h4 id=section-UsingHibernateWithJPA-Troubleshooting>Troubleshooting</h4>
In case you are using NetBeans 5.5 instead of 5.5.1, you might run into <a class=external href="http://www.netbeans.org/issues/show_bug.cgi?id=90031"><u><font color=#0000ff>issue 90031</font></u></a><u><font color=#0000ff><img class=outlink alt="" src="http://wiki.netbeans.org/wiki/images/out.png"></font></u>, which causes silent failures when inserting data. The workaround is to upgrade to 5.5.1 or to manually add <span style="FONT-FAMILY: monospace; WHITE-SPACE: pre">em.joinTransaction()</span> after each call to <span style="FONT-FAMILY: monospace; WHITE-SPACE: pre">utx.begin()</span> in the generated controller classes.
<p>
<p>
<p>Download the source code for this tutorial <a class=attachment href="http://wiki.netbeans.org/wiki/attach/UsingHibernateWithJPA/HibernateWithJPA.zip"><u><font color=#0000ff>here</font></u></a><a href="http://wiki.netbeans.org/wiki/PageInfo.jsp?page=UsingHibernateWithJPA/HibernateWithJPA.zip"><u><font color=#0000ff><img alt=(info) src="http://wiki.netbeans.org/wiki/images/attachment_small.png" border=0></font></u></a> </p>
</div>
<div id=attachments>
<h3>Attachments</h3>
<div class=zebra-table>
<table>
    <tbody>
        <tr class=" odd">
            <td><a class=attachment href="http://wiki.netbeans.org/wiki/attach/UsingHibernateWithJPA/HibernateWithJPA.zip"><u><font color=#0000ff>HibernateWithJPA.zip</font></u></a></td>
            <td><a class=pageinfo href="http://wiki.netbeans.org/wiki/PageInfo.jsp?page=UsingHibernateWithJPA/HibernateWithJPA.zip"><u><font color=#0000ff><img alt="Info on HibernateWithJPA.zip" src="http://wiki.netbeans.org/wiki/images/attachment_big.png" border=0></font></u></a></td>
            <td>46352 bytes</td>
        </tr>
        <tr>
            <td><a class=attachment href="http://wiki.netbeans.org/wiki/attach/UsingHibernateWithJPA/jsf-from-entities.JPG"><u><font color=#0000ff>jsf-from-entities.JPG</font></u></a></td>
            <td><a class=pageinfo href="http://wiki.netbeans.org/wiki/PageInfo.jsp?page=UsingHibernateWithJPA/jsf-from-entities.JPG"><u><font color=#0000ff><img alt="Info on jsf-from-entities.JPG" src="http://wiki.netbeans.org/wiki/images/attachment_big.png" border=0></font></u></a></td>
            <td>50178 bytes</td>
        </tr>
        <tr class=" odd">
            <td><a class=attachment href="http://wiki.netbeans.org/wiki/attach/UsingHibernateWithJPA/list-of-customers.JPG"><u><font color=#0000ff>list-of-customers.JPG</font></u></a></td>
            <td><a class=pageinfo href="http://wiki.netbeans.org/wiki/PageInfo.jsp?page=UsingHibernateWithJPA/list-of-customers.JPG"><u><font color=#0000ff><img alt="Info on list-of-customers.JPG" src="http://wiki.netbeans.org/wiki/images/attachment_big.png" border=0></font></u></a></td>
            <td>185621 bytes</td>
        </tr>
        <tr>
            <td><a class=attachment href="http://wiki.netbeans.org/wiki/attach/UsingHibernateWithJPA/not-valid-customer.JPG"><u><font color=#0000ff>not-valid-customer.JPG</font></u></a></td>
            <td><a class=pageinfo href="http://wiki.netbeans.org/wiki/PageInfo.jsp?page=UsingHibernateWithJPA/not-valid-customer.JPG"><u><font color=#0000ff><img alt="Info on not-valid-customer.JPG" src="http://wiki.netbeans.org/wiki/images/attachment_big.png" border=0></font></u></a></td>
            <td>32670 bytes</td>
        </tr>
        <tr class=" odd">
            <td><a class=attachment href="http://wiki.netbeans.org/wiki/attach/UsingHibernateWithJPA/query-by-example.JPG"><u><font color=#0000ff>query-by-example.JPG</font></u></a></td>
            <td><a class=pageinfo href="http://wiki.netbeans.org/wiki/PageInfo.jsp?page=UsingHibernateWithJPA/query-by-example.JPG"><u><font color=#0000ff><img alt="Info on query-by-example.JPG" src="http://wiki.netbeans.org/wiki/images/attachment_big.png" border=0></font></u></a></td>
            <td>81683 bytes</td>
        </tr>
        <tr>
            <td><a class=attachment href="http://wiki.netbeans.org/wiki/attach/UsingHibernateWithJPA/query-by-example.PNG"><u><font color=#0000ff>query-by-example.PNG</font></u></a></td>
            <td><a class=pageinfo href="http://wiki.netbeans.org/wiki/PageInfo.jsp?page=UsingHibernateWithJPA/query-by-example.PNG"><u><font color=#0000ff><img alt="Info on query-by-example.PNG" src="http://wiki.netbeans.org/wiki/images/attachment_big.png" border=0></font></u></a></td>
            <td>152412 bytes</td>
        </tr>
        <tr class=" odd">
            <td><a class=attachment href="http://wiki.netbeans.org/wiki/attach/UsingHibernateWithJPA/query-by-example.png"><u><font color=#0000ff>query-by-example.png</font></u></a></td>
            <td><a class=pageinfo href="http://wiki.netbeans.org/wiki/PageInfo.jsp?page=UsingHibernateWithJPA/query-by-example.png"><u><font color=#0000ff><img alt="Info on query-by-example.png" src="http://wiki.netbeans.org/wiki/images/attachment_big.png" border=0></font></u></a></td>
            <td>152355 bytes</td>
        </tr>
        <tr>
            <td><a class=attachment href="http://wiki.netbeans.org/wiki/attach/UsingHibernateWithJPA/jsf-from-entities.png"><u><font color=#0000ff>jsf-from-entities.png</font></u></a></td>
            <td><a class=pageinfo href="http://wiki.netbeans.org/wiki/PageInfo.jsp?page=UsingHibernateWithJPA/jsf-from-entities.png"><u><font color=#0000ff><img alt="Info on jsf-from-entities.png" src="http://wiki.netbeans.org/wiki/images/attachment_big.png" border=0></font></u></a></td>
            <td>151411 bytes</td>
        </tr>
        <tr class=" odd">
            <td><a class=attachment href="http://wiki.netbeans.org/wiki/attach/UsingHibernateWithJPA/list-of-customers.png"><u><font color=#0000ff>list-of-customers.png</font></u></a></td>
            <td><a class=pageinfo href="http://wiki.netbeans.org/wiki/PageInfo.jsp?page=UsingHibernateWithJPA/list-of-customers.png"><u><font color=#0000ff><img alt="Info on list-of-customers.png" src="http://wiki.netbeans.org/wiki/images/attachment_big.png" border=0></font></u></a></td>
            <td>314586 bytes</td>
        </tr>
        <tr>
            <td><a class=attachment href="http://wiki.netbeans.org/wiki/attach/UsingHibernateWithJPA/not-valid-customer.png"><u><font color=#0000ff>not-valid-customer.png</font></u></a></td>
            <td><a class=pageinfo href="http://wiki.netbeans.org/wiki/PageInfo.jsp?page=UsingHibernateWithJPA/not-valid-customer.png"><u><font color=#0000ff><img alt="Info on not-valid-customer.png" src="http://wiki.netbeans.org/wiki/images/attachment_big.png" border=0></font></u></a></td>
            <td>84493 bytes</td>
        </tr>
    </tbody>
</table>
</div>
</div>
</div>
<div id=favorites><a title="Front page" href="http://wiki.netbeans.org/wiki/"><img id=companylogo alt="NetBeans Wiki" src="http://wiki.netbeans.org/wiki/templates/default/nb_logo.png"><font color=#0000ff><u> </u></font></a><!--
<div class="block">-->
<div>
<div class=leftmenu>
<ul>
    <li><a class=wikipage href="http://wiki.netbeans.org/wiki/view/Main"><u><font color=#0000ff>Front Page</font></u></a> </li>
</ul>
<hr>
<ul>
    <li><a class=wikipage href="http://wiki.netbeans.org/wiki/view/NetBeansUserFAQ"><font color=#0000ff><u>User FAQ </u></font></a>
    <li><a class=wikipage href="http://wiki.netbeans.org/wiki/view/NetBeansDeveloperFAQ"><u><font color=#0000ff>Developer FAQ </font></u></a>
    <li><a class=wikipage href="http://wiki.netbeans.org/wiki/view/CommunityDocs"><u><font color=#0000ff>Community Docs</font></u></a> </li>
</ul>
<hr>
<ul>
    <li><a class=wikipage href="http://wiki.netbeans.org/wiki/view/Development"><u><font color=#0000ff>Development</font></u></a>
    <li><a class=wikipage href="http://wiki.netbeans.org/wiki/view/Mobility"><u><font color=#0000ff>Mobility</font></u></a>
    <li><a class=wikipage href="http://wiki.netbeans.org/wiki/view/CAndCPlusPlus"><u><font color=#0000ff>C/C++</font></u></a>
    <li><a class=wikipage href="http://wiki.netbeans.org/wiki/view/Quality"><u><font color=#0000ff>Quality</font></u></a> </li>
</ul>
<hr>
<ul>
    <li><a class=wikipage href="http://wiki.netbeans.org/wiki/view/WikiEtiquette"><u><font color=#0000ff>Wiki Etiquette</font></u></a>
    <li><a class=wikipage href="http://wiki.netbeans.org/wiki/view/TermsOfUse"><u><font color=#0000ff>Terms Of Use</font></u></a>
    <li><a class=wikipage href="http://wiki.netbeans.org/wiki/view/TextFormattingRules"><u><font color=#0000ff>Text Formatting Rules</font></u></a>
    <li><a class=wikipage href="http://wiki.netbeans.org/wiki/view/UnusedPages"><u><font color=#0000ff>Unlinked Pages</font></u></a>
    <li><a class=wikipage href="http://wiki.netbeans.org/wiki/view/UndefinedPages"><u><font color=#0000ff>Undefined Pages</font></u></a> </li>
</ul>
<hr>
<ul>
    <li><a class=wikipage href="http://wiki.netbeans.org/wiki/view/WikiCategory"><u><font color=#0000ff>Categories</font></u></a>
    <li><a class=wikipage href="http://wiki.netbeans.org/wiki/view/PageIndex"><u><font color=#0000ff>Page Index</font></u></a>
    <li><a class=wikipage href="http://wiki.netbeans.org/wiki/view/RecentChanges"><u><font color=#0000ff>Recent Changes</font></u></a>
    <li><a class=wikipage href="http://wiki.netbeans.org/wiki/view/FindPage"><u><font color=#0000ff>Find Page</font></u></a> </li>
</ul>
</div>
<div class=username></div>
<div class=pageactions><span class=quick2Top><a title="Go to Top" href="http://wiki.netbeans.org/wiki/view/UsingHibernateWithJPA#Top"><u><font color=#800080>&nbsp;</font></u></a></span> <span><a class=pageinfo href="http://wiki.netbeans.org/wiki/PageInfo.jsp?page=UsingHibernateWithJPA"><u><font color=#0000ff>Page Info</font></u></a> </span><span><a class=wikipage href="http://wiki.netbeans.org/wiki/view/UserPreferences"><u><font color=#0000ff>My Prefs</font></u></a> </span><span><a href="http://wiki.netbeans.org/wiki/Login.jsp"><u><font color=#0000ff>Log in</font></u></a> </span>
<div class=pageInfo>This page (revision-27) last changed on <a href="http://wiki.netbeans.org/wiki/Diff.jsp?page=UsingHibernateWithJPA&amp;r1=27&amp;r2=26"><u><font color=#0000ff>09:13 16-Mar-2007 </font></u></a>by Erno Mononen. </div>
<span class=quick2Bottom><a title="Go to Bottom" href="http://wiki.netbeans.org/wiki/view/UsingHibernateWithJPA#Bottom"><u><font color=#800080>&nbsp;</font></u></a></span> </div>
<div class=leftmenufooter>
<hr>
Referenced by:
<p>
<ul>
    <li><a class=wikipage href="http://wiki.netbeans.org/wiki/view/ErnoMononen"><u><font color=#0000ff>ErnoMononen</font></u></a>
    <li><a class=wikipage href="http://wiki.netbeans.org/wiki/view/JavaEE"><u><font color=#0000ff>JavaEE</font></u></a> </li>
</ul>
<p>&#160;</p>
</div>
<hr>
<div class=rssfeed><a href="http://wiki.netbeans.org/wiki/rss.rdf"><img title="Aggregate the RSS feed of the entire wiki" alt=[RSS] src="http://wiki.netbeans.org/wiki/images/xml.png" border=0></a></div>
</div>
</div>
<img src ="http://www.blogjava.net/lewhwa/aggbug/115786.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lewhwa/" target="_blank">网路冷眼@BlogJava</a> 2007-05-07 23:34 <a href="http://www.blogjava.net/lewhwa/articles/115786.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Using Hibernate with the Java Persistence API（转载）</title><link>http://www.blogjava.net/lewhwa/articles/115783.html</link><dc:creator>网路冷眼@BlogJava</dc:creator><author>网路冷眼@BlogJava</author><pubDate>Mon, 07 May 2007 15:30:00 GMT</pubDate><guid>http://www.blogjava.net/lewhwa/articles/115783.html</guid><wfw:comment>http://www.blogjava.net/lewhwa/comments/115783.html</wfw:comment><comments>http://www.blogjava.net/lewhwa/articles/115783.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lewhwa/comments/commentRss/115783.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lewhwa/services/trackbacks/115783.html</trackback:ping><description><![CDATA[原文地址：&nbsp;<a href="http://testwww.netbeans.org/kb/articles/hibernate-javaee.html">http://testwww.netbeans.org/kb/articles/hibernate-javaee.html</a>
<h1>Using Hibernate with the Java Persistence API</h1>
<p>One of the main features of the new Java Persistence API that was introduced with the Java EE 5 platform is that you can plug in any persistence provider that you want. The Sun Java System Application Server 9 ships with Oracles's Toplink persistence provider. In this tutorial, we will install the Hibernate persistence provider on the application server and use it to develop a quick application that accesses a database with the Java Persistence API.</p>
<div class=indent>
<h3 class=tutorial>Software Required for this Tutorial</h3>
<ul>
    <li><a href="http://www.netbeans.org/products/ide/">NetBeans IDE 5.5</a> (<a href="http://www.netbeans.info/downloads/index.php">download</a>)
    <li>Sun Java System Application Server, Platform Edition 9.0 (<a href="http://www.sun.com/software/products/appsrvr_pe/index.xml">download</a>)
    <li>Hibernate Core 3.2.2.GA and Hibernate EntityManager 3.2.1.GA (<a href="http://www.hibernate.org/6.html">download</a>) </li>
</ul>
</div>
<h2>Setting up your Environment</h2>
<p>Before we start, we have to register the application server with the IDE and copy over the Hibernate JAR files to the application server's <tt><font size=2>lib</font></tt> folder. We also have to create a library in the IDE for the Hibernate entity manager.</p>
<ol>
    <li>If the application server is not registered in the IDE, register it by choosing Tools &gt; Server Manager and clicking the Add Server button. You can see all registered application servers by opening the Runtime window and expanding the Servers node.
    <li>Unzip the Hibernate JAR files to any temporary location and then copy the following JAR files to your application server's <tt><font size=2>lib</font></tt> folder:
    <ul>
        <li>From Hibernate EntityManager:
        <ul>
            <li><tt><font size=2>hibernate-entitymanager.jar </font></tt>
            <li><tt><font size=2>lib/hibernate-annotations.jar</font></tt>
            <li><tt><font size=2>lib/jboss-archive-browsing.jar </font></tt>
            <li><tt><font size=2>lib/javassist.jar</font></tt> </li>
        </ul>
        <li>From Hibernate Core
        <ul>
            <li><tt><font size=2>hibernate3.jar</font></tt>
            <li><tt><font size=2>lib/antlr-2.7.6.jar</font></tt>
            <li><tt><font size=2>lib/asm-attrs.jar</font></tt>
            <li><tt><font size=2>lib/asm.jar</font></tt>
            <li><tt><font size=2>lib/c3p0-0.9.0.jar</font></tt>
            <li><tt><font size=2>lib/cglib-2.1.3.jar</font></tt>
            <li><tt><font size=2>lib/commons-collections-2.1.1.jar</font></tt>
            <li><tt><font size=2>lib/commons-logging-1.0.4.jar</font></tt>
            <li><tt><font size=2>lib/concurrent-1.3.2.jar</font></tt>
            <li><tt><font size=2>lib/dom4j-1.6.1.jar</font></tt>
            <li><tt><font size=2>lib/ehcache-1.2.3.jar</font></tt>
            <li><tt><font size=2>lib/log4j-1.2.11.jar</font></tt> </li>
        </ul>
        </li>
    </ul>
    <li>Restart the server if it is running. You can restart the server by right-clicking its node in the Runtime window and choosing Restart.
    <li>Choose Tools &gt; Library Manager from the main menu. Click New Library, name the library Hibernate, and click OK.
    <li>In the Libraries pane of the Library Manager dialog box, select the Hibernate library you just created. Click the Classpath tab and then click Add JAR/Folder and locate the <tt><font size=2>hibernate-entitymanager.jar</font></tt> and <tt><font size=2>javaee.jar</font></tt> in your application server's <tt><font size=2>lib</font></tt> folder. Click Add JAR / Folder and then click OK.
    <p align=center><img alt="library manager" src="http://testwww.netbeans.org/images/articles/hibernate/hibernate-library.png"> </p>
    </li>
</ol>
<h2>Creating the Entity Classes and Persistence Unit</h2>
<p>Now that we have added the Hibernate libraries to the lib directory of our application server, we can create the entity classes and web interface. We will create a web application named HibernateApp with entity classes and a JSF interface. We will use the NetBeans CRUD facilities.</p>
<ol>
    <li>Choose File &gt; New Project (Ctrl-Shift-N). Select Web Application from the Web category.
    <li>Name the project HibernateApp, set the server to Sun Java System Application Server and the Java EE version to Java EE 5. Click Next.
    <li>Select the JavaServer Faces framework and click Finish.
    <li>In the Projects window, right-click the project node and choose New &gt; Entity Classes from Database. In the Data Source drop-down, select the <tt><font size=2>jdbc/sample</font></tt> database. If prompted for a user name and password, use app and app. Select the <tt><font size=2>CUSTOMER</font></tt> table from the list, click Add and then click Next.
    <p align=center><img height=439 alt="selecting the tables from the database" src="http://testwww.netbeans.org/images/articles/hibernate/hibernate-tables.png" width=600></p>
    <li>Leave the tables and class names as their default, and set the package to <tt><font size=2>hibernatesample.db</font></tt>.
    <p align=center><img alt="wizard panel displaying the entity classes that will be generated from the selected tables" src="http://testwww.netbeans.org/images/articles/hibernate/hibernate-entity.png"> </p>
    <li>Now it is time to create the persistence unit. Click the Create Persistence Unit button. Set <tt><font size=2>HibernateApp </font></tt>as the persistence unit name, select Hibernate as the persistence provider, and select None as the table generation strategy. Click Create.
    <p align=center><img height=282 alt="create persistence unit dialog box" src="http://testwww.netbeans.org/images/articles/hibernate/hibernate-persistence.png" width=504></p>
    <li>Click Finish in the Entity Classes from Database wizard. The IDE creates the persistence unit (<tt><font size=2>persistence.xml</font></tt> under Configuration Files node) and the entity classes (under Source Packages).
    <li>Finally, let's add some Hibernate-specific properties. Double-click <tt><font size=2>persistence.xml</font></tt> and click the XML button at the top of the editor to switch to the XML view. Add the following property to the persistence unit:
    <pre class=examplecode>  &lt;persistence-unit name="HibernateApp" transaction-type="JTA"&gt;
    &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
    &lt;jta-data-source&gt;jdbc/sample&lt;/jta-data-source&gt;
    <strong>&lt;properties&gt;
    &lt;property name="hibernate.show_sql" value="true" /&gt;
    &lt;/properties&gt;</strong>
    &lt;/persistence-unit&gt;
    &lt;/persistence&gt;</pre>
    <br>This property causes all SQL statements to be printed to the application server's log file. </li>
</ol>
<h2>Coding the JSF Interface</h2>
<p>Now we can quickly generate JSF pages for the entity classes with the NetBeans IDE CRUD generation. </p>
<ol>
    <li>Right-click the project node and choose New &gt; JSF Pages from Entity Class. Add both <tt><font size=2>Customer.java</font></tt> and <tt><font size=2>DiscountCode.java</font></tt> to the list of selected classes and click Next.
    <p align=center><img height=448 alt="New JSF Pages from Entity Class Wizard" src="http://testwww.netbeans.org/images/articles/hibernate/hibernate-jsf.png" width=600></p>
    <li>Change the package to <tt><font size=2>hibernatesample.controller</font></tt> and click Finish.
    <li>To make this application work correctly, we need to make some changes to <tt><font size=2>CustomerController.java</font></tt> and <tt><font size=2>DiscountCodeController.java</font></tt>. We need to modify the <tt><font size=2>create</font></tt>, <tt><font size=2>edit</font></tt> and <tt><font size=2>destroy</font></tt> methods in the files to change the order in which we get the entity manager.
    <p>Open <tt><font size=2>CustomerController.java</font></tt> and <tt><font size=2>DiscountCodeController.java</font></tt> in the Source Editor. Each of the files contains <tt><font size=2>create</font></tt>, <tt><font size=2>edit</font></tt> and <tt><font size=2>destroy</font></tt> methods with the following code:</p>
    <pre class=examplecode>EntityManager em = getEntityManager();
    try {
    utx.begin();
    </pre>
    <p>Modify the code in each of the methods to change the order in which we get the entity manager.</p>
    <pre class=examplecode><strong>EntityManager em = null;</strong>
    try {
    utx.begin();
    <strong>em = getEntityManager();</strong>
    </pre>
    <p>For example, the modified code in the <tt><font size=2>create</font></tt> method of <tt><font size=2>CustomerController.java</font></tt> should look like the following (changes in bold):</p>
    <pre class=examplecode>public String create() {
    <strong>EntityManager em = null;</strong>
    try {
    utx.begin();
    <strong>em = getEntityManager();</strong>
    em.persist(customer);
    utx.commit();
    ...
    </pre>
    </li>
</ol>
<p>After creating the JSF pages and modifying the code in the controller classes, you should be able to run and test the application.</p>
<h2>Testing the Project</h2>
<p>Now we need to run the project to test whether the connection to our database is working correctly.</p>
<ol>
    <li>Right-click the project and choose Run Project. The IDE starts the application server, builds and deploys the application, and shows <tt><font size=2>index.jsp</font></tt> in the external web browser.
    <li>Click the List of Customer link and you should see the following web page:
    <p align=center><img height=526 alt="List of Customers JSP Page" src="http://testwww.netbeans.org/images/articles/hibernate/hibernate-result.png" width=533 border=1></p>
    <li>Use the New Customer link to create a customer. Then check the server log by right-clicking the application server node in the Runtime window and choosing View Server Log. The SQL statements for creating the customer should be listed in the server log. </li>
</ol>
<br>
<div class=feedback-box><a href="http://testwww.netbeans.org/about/contact_form.html?to=3&amp;subject=Feedback:%20Using%20Hibernate%20with%20Java%20Persistence">Send Us Your Feedback</a></div>
<br style="CLEAR: both"><!-- ======================================================================================== -->
<h2><a name=nextsteps></a>Next Steps</h2>
<p>For more information about using NetBeans IDE 5.5 to develop Java EE applications, see the following resources:</p>
<ul>
    <li><a href="http://testwww.netbeans.org/kb/trails/java-ee.html">Java EE Applications Learning Trail</a>
    <li><a href="http://testwww.netbeans.org/kb/55/javaee-intro.html">Introduction to Java EE 5 Technology</a>
    <li><a href="http://testwww.netbeans.org/kb/55/persistence_ontomcat.html">Using Java Persistence in a J2EE 1.4 Web Application</a>
    <li><a href="http://wiki.netbeans.org/wiki/view/UsingHibernateWithJPA">Using Hibernate-specific features with JPA</a> </li>
</ul>
<p>To send comments and suggestions, get support, and keep informed on the latest developments on the NetBeans IDE Java EE development features, <a href="http://testwww.netbeans.org/community/lists/top.html">join the nbj2ee@netbeans.org mailing list</a>.</p>
<!-- /Servlet-Specific template --><!-- end of right navigation --><!-- servlets and anything not on www or testwww -->
<img src ="http://www.blogjava.net/lewhwa/aggbug/115783.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lewhwa/" target="_blank">网路冷眼@BlogJava</a> 2007-05-07 23:30 <a href="http://www.blogjava.net/lewhwa/articles/115783.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java EE 5 中的 Web 服务 (JAX-WS)（转载）</title><link>http://www.blogjava.net/lewhwa/articles/115782.html</link><dc:creator>网路冷眼@BlogJava</dc:creator><author>网路冷眼@BlogJava</author><pubDate>Mon, 07 May 2007 15:24:00 GMT</pubDate><guid>http://www.blogjava.net/lewhwa/articles/115782.html</guid><wfw:comment>http://www.blogjava.net/lewhwa/comments/115782.html</wfw:comment><comments>http://www.blogjava.net/lewhwa/articles/115782.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lewhwa/comments/commentRss/115782.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lewhwa/services/trackbacks/115782.html</trackback:ping><description><![CDATA[&nbsp;原文地址：<a href="http://testwww.netbeans.org/kb/55/websvc-jax-ws_zh_CN.html">http://testwww.netbeans.org/kb/55/websvc-jax-ws_zh_CN.html</a>
<h1>&nbsp;</h1>
<h1>Java EE 5 中的 Web 服务 (JAX-WS)</h1>
<div class=articledate style="MARGIN-LEFT: 0px">由 Geertjan Wielenga、Manisha Umbarje、Martin Grebac、Milan Kuchtiak 和 Radko Najman 撰稿并修订</div>
<p><a href="http://www.jcp.org/en/jsr/detail?id=224"><u><font color=#1e2a60>Java API for XML Web Services (JAX-WS) 2.0，JSR 224</font></u></a> 是 Java EE 5 平台的重要组成部分。作为 Java API for XML-based RPC 1.1 (JAX-RPC) 的后续发行版本，JAX-WS 简化了使用 Java 技术开发 Web 服务的工作。并且通过对多种协议（如 SOAP 1.1、SOAP 1.2、XML）的支持，以及提供用于支持其他协议和 HTTP 的工具，它解决了 JAX-RPC 1.1 中存在的一些问题。JAX-WS 使用 JAXB 2.0 提供数据绑定服务，并支持通过定制来控制生成的服务端点接口。通过对标注的支持，JAX-WS 简化了 Web 服务开发，并缩小了运行时 JAR 文件的大小。</p>
<p>本文档向您介绍了有关使用 IDE 开发 JAX-WS Web 服务以及在三个不同的客户端（Java SE 应用程序中的 Java 类、Web 应用程序中的 Servlet 或 JSP 页）中使用该服务的基础知识。在本文档中创建的三个客户端是独立的应用程序，它们都使用同一个 Web 服务。</p>
<p align=center><strong>预计持续时间：25 分钟</strong></p>
<div class=indent>
<h3 class=tutorial>本教程所需的软件</h3>
<p>在开始之前，您需要在计算机中安装以下软件：</p>
<ul>
    <li><a href="http://www.netbeans.org/products/ide/index_zh_CN.html"><u><font color=#1e2a60>NetBeans IDE 5.5</font></u></a>（<a href="http://www.netbeans.info/downloads/index.php"><u><font color=#1e2a60>下载</font></u></a>）
    <li>Java Standard Development Kit (JDK) 版本 5.0 或版本 6.0（<a href="http://java.sun.com/javase/downloads/index.jsp"><u><font color=#1e2a60>下载</font></u></a>）
    <li>Sun Java System Application Server 9.0（如果未与安装的 NetBeans IDE 捆绑在一起，请<a href="https://glassfish.dev.java.net/"><u><font color=#1e2a60>下载</font></u></a>并单独安装它）。 </li>
</ul>
<h3 class=tutorial>教程练习</h3>
<ul><!--
    <li><a href="#Exercise_0">Exercise 0: Install and configure the tutorial environment</a></li>
    -->
    <li><a href="http://testwww.netbeans.org/kb/55/websvc-jax-ws_zh_CN.html#Exercise_0"><!--exercise 1: --><u><font color=#1e2a60>安装并配置教程环境</font></u></a>
    <li><a href="http://testwww.netbeans.org/kb/55/websvc-jax-ws_zh_CN.html#Exercise_1"><!--exercise 1: --><u><font color=#1e2a60>创建 Web 服务</font></u></a>
    <li><a href="http://testwww.netbeans.org/kb/55/websvc-jax-ws_zh_CN.html#Exercise_2"><!--exercise 2: --><u><font color=#1e2a60>对 Web 服务进行编码</font></u></a>
    <li><a href="http://testwww.netbeans.org/kb/55/websvc-jax-ws_zh_CN.html#Exercise_2_1"><!--exercise 2_1: --><u><font color=#1e2a60>部署并测试 Web 服务</font></u></a>
    <li><!--exercise 3: -->在以下客户端中使用 Web 服务：
    <ul>
        <li><a href="http://testwww.netbeans.org/kb/55/websvc-jax-ws_zh_CN.html#Exercise_3_1"><u><font color=#1e2a60>Java SE 应用程序中的 Java 类</font></u></a>
        <li><a href="http://testwww.netbeans.org/kb/55/websvc-jax-ws_zh_CN.html#Exercise_3_2"><u><font color=#1e2a60>Web 应用程序中的 Servlet</font></u></a>
        <li><a href="http://testwww.netbeans.org/kb/55/websvc-jax-ws_zh_CN.html#Exercise_3_3"><u><font color=#1e2a60>Web 应用程序中的 JSP 页</font></u></a> </li>
    </ul>
    </li>
</ul>
</div>
<!-- ===================================================================================== -->
<h2><a name=Exercise_0></a>安装并配置教程环境</h2>
<p>如果尚未注册 Sun Java System Application Server 9.0 的实例，则必须先进行注册，然后才可以着手开发 Java EE 5 应用程序：</p>
<ol>
    <li>从主窗口中，选择&#8220;工具&#8221;&gt;&#8220;服务器管理器&#8221;。
    <li>单击&#8220;添加服务器&#8221;。选择 "Sun Java System Application Server"，并为此实例指定一个名称，然后，单击&#8220;下一步&#8221;。
    <li>指定服务器信息、应用服务器本地实例的位置以及要将 Web 服务部署到的域。
    <li>单击&#8220;完成&#8221;。 </li>
</ol>
<p><strong>注意：</strong>也可以部署到 Tomcat Web Server，但由于它仅具有一个 Web 容器，因此应在下一部分创建 Web 应用程序，而不是 EJB 模块。与 JSR-109 Web 服务不同的是，JAX-WS Web 服务可以成功地部署到 Tomcat Web 容器中。 <!-- ===================================================================================== -->
<h2><a name=Exercise_1></a><!--exercise 1: -->创建 Web 服务</h2>
<p>本练习的目的是创建一个适用于您决定使用的部署容器的项目。在建立项目后，您将在其中创建 Web 服务。</p>
<div class=indent>
<h3 class=tutorial>选择容器</h3>
<p>您可以在 Web 容器或 EJB 容器中部署 Web 服务。这要取决于具体的选择。例如，如果您计划部署到仅具有 Web 容器的 Tomcat Web Server 上，则应该选择创建 Web 应用程序，而不是 EJB 模块。</p>
<ol>
    <li>选择&#8220;文件&#8221;&gt;&#8220;新建项目&#8221;(Ctrl-Shift-N)。从 "Web" 类别中，选择&#8220;Web 应用程序&#8221;，或者从&#8220;企业&#8221;类别中，选择&#8220;EJB 模块&#8221;。
    <li>将项目命名为 <tt><font size=2>CalculatorWSApplication</font></tt>。
    <li>根据要使用的部署服务器，请执行以下操作：
    <ul>
        <li>对于 Sun Java System Application Server，将 J2EE 版本设置为 "Java EE 5"。
        <li>对于 Tomcat Web Server，请取消选中&#8220;将源代码级别设置为 1.4&#8221;复选框。</li>
    </ul>
    <li>单击&#8220;完成&#8221;。 </li>
</ol>
<h3 class=tutorial>通过 Java 类创建 Web 服务</h3>
<ol>
    <li>右键单击 <tt><font size=2>CalculatorWSApplication</font></tt> 节点，然后选择&#8220;新建&#8221;&gt;&#8220;Web 服务&#8221;。
    <li>将 Web 服务命名为 <tt><font size=2>CalculatorWS</font></tt>，在&#8220;包&#8221;中键入 <tt><font size=2>org.me.calculator</font></tt>，然后单击&#8220;完成&#8221;。
    <p>将在&#8220;项目&#8221;窗口中显示新建的 Web 服务。例如，对于 Web 应用程序，现在&#8220;项目&#8221;窗口将如下所示：</p>
    <p align=center><img src="http://testwww.netbeans.org/images/articles/javaee5/jax-ws-project-view_zh_CN.png" border=1>
    <p>IDE 将自动创建服务器所需的部署描述符（如果有）。对于 Sun Java System Application Server，则不需要部署描述符。对于部署到 Tomcat Web Server 的 Web 服务，将添加 <tt><font size=2>sun-jaxws.xml</font></tt> 以及 <tt><font size=2>web.xml</font></tt> 中的 <tt><font size=2>WSServlet</font></tt> 项。 </p>
    </li>
</ol>
<h3 class=tutorial>小结</h3>
<p>在本练习中，您创建了一个 NetBeans 项目并建立了 Web 服务。 </p>
</div>
<!-- ===================================================================================== -->
<h2><a name=Exercise_2></a><!--exercise 2: -->对 Web 服务进行编码</h2>
<p>本练习的目的是对 IDE 生成的文件和代码执行一些有意义的操作。您将添加一个操作，该操作将从客户端接收到的两个数字相加起来。</p>
<div class=indent>
<h3 class=tutorial>将业务逻辑添加到 Web 服务</h3>
<ol>
    <li>展开&#8220;Web 服务&#8221;节点，然后双击 "CalculatorWS" 节点。将在源代码编辑器中打开 Web 服务。请注意，代码中已存在了一个操作。它被注释掉了。现在，我们将从头开始创建一个新的操作。创建此操作的另一种方法是删除代码注释掉的行。
    <li>在类主体（位于注释掉的代码之上或之下）中单击鼠标右键，然后选择&#8220;Web 服务&#8221;&gt;&#8220;添加操作&#8221;。
    <li>在&#8220;添加操作&#8221;对话框的上半部分，在&#8220;名称&#8221;中键入 <tt><font size=2>add</font></tt>，并从&#8220;返回类型&#8221;下拉列表中选择 <tt><font size=2>int</font></tt>。
    <li>在&#8220;添加操作&#8221;对话框的下半部分，单击&#8220;添加&#8221;，然后创建一个类型为 <tt><font size=2>int</font></tt>、名为 <tt><font size=2>i</font></tt> 的参数。单击&#8220;确定&#8221;。
    <li>重新单击&#8220;添加&#8221;，然后创建一个类型为 <tt><font size=2>int</font></tt>、名为 <tt><font size=2>j</font></tt> 的参数。
    <li>单击位于&#8220;添加操作&#8221;对话框底部的&#8220;确定&#8221;。请注意，<tt><font size=2>add</font></tt> 方法的框架已添加到源代码编辑器中：
    <p>&#160;</p>
    <pre class=examplecode>    @WebMethod
    public int add(@WebParam(name = "i") int i, @WebParam(name = "j") int j) {
    // TODO implement operation
    return 0;
    }</pre>
    <p>&#160;</p>
    <li>按如下所示更改 <tt><font size=2>add</font></tt> 方法（更改内容以粗体显示）：
    <p>&#160;</p>
    <pre class=examplecode>    @WebMethod
    public int add(@WebParam(name = "i") int i, @WebParam(name = "j") int j) {
    <strong>int k = i + j;</strong>
    return <strong>k</strong>;
    }</pre>
    <p>&#160;</p>
    </li>
</ol>
<h3 class=tutorial>小结</h3>
<p>在本练习中，您已为 Web 服务添加了代码。 </p>
</div>
<!-- ======================================================================================== -->
<h2 class=tutorial><a name=Exercise_2_1></a>部署并测试 Web 服务</h2>
<div class=indent>
<p>在将 Web 服务部署到 Web 容器时，IDE 允许您测试 Web 服务以查看它是否能够按预期的那样工作。鉴于此目的，我们在 IDE 中集成了 Sun Java System Application Server 提供的 Tester 应用程序。对于 Tomcat Web Server，也存在类似的工具。但是，Sun Java System Application Server 的 Tester 页允许您输入值并对其进行测试，而 Tomcat Web Server 则不允许这样做。在后一种情况下，您只能看到已部署了 Web 服务，但是不能对值进行测试。目前还没有用于测试 EJB 模块是否已成功部署的工具。
<p><strong>测试是否已成功部署到 Web 容器：</strong>
<ol>
    <li>右键单击项目节点，选择&#8220;属性&#8221;，然后单击&#8220;运行&#8221;。根据要使用的部署服务器，请执行以下操作：
    <ul>
        <li>对于 Sun Java System Application Server，在&#8220;相对 URL&#8221;字段中键入 <tt><font size=2>/CalculatorWSService?Tester</font></tt>。
        <li>对于 Tomcat Web Server，在&#8220;相对 URL&#8221;字段中键入 <tt><font size=2>/CalculatorWS?Tester</font></tt>。 </li>
    </ul>
    <p><strong>注意：</strong>由于部署的 EJB 模块的结果不会显示在浏览器中，因此，如果您使用的是 EJB 模块，则不能执行上述步骤。</p>
    <li>右键单击项目节点，然后选择&#8220;运行项目&#8221;。
    <p>IDE 将启动应用服务器、生成应用程序，并在浏览器中打开 tester 页（如果已将 Web 应用程序部署到 Sun Java System Application Server）。对于使用 Tomcat Web Server 和部署 EJB 模块来说，情况则有所不同：</p>
    <ul>
        <li>如果部署到 Tomcat Web Server，将看到以下内容（表明已成功部署了 Web 服务）：
        <p align=center><img src="http://testwww.netbeans.org/images/articles/javaee5/tomcat-successful-ws.png" border=1> </p>
        <li>如果部署了 EJB 模块，则&#8220;输出&#8221;窗口显示下面的消息才表明已成功部署：
        <p>&#160;</p>
        <pre class=examplecode>    Deployment of application CalculatorWSApplication  completed successfully
        Enable of CalculatorWSApplication in target server   completed successfully
        Enable of application in all targets  completed successfully
        All operations completed successfully
        run-deploy:
        run:
        BUILD SUCCESSFUL</pre>
        <p>&#160;</p>
        </li>
    </ul>
    <li>如果部署到 Sun Java System Application Server，在 tester 页上键入两个数字，如下所示：
    <p align=center><img src="http://testwww.netbeans.org/images/articles/javaee5/jax-ws-tester.png" border=1> </p>
    <li>将显示这两个数字的和：
    <p align=center><img src="http://testwww.netbeans.org/images/articles/javaee5/jax-ws-tester2.png" border=1> </p>
    </li>
</ol>
<h3 class=tutorial>小结</h3>
<p>在本练习中，您已部署了 Web 服务并对其进行了测试。 </p>
<ol></ol>
    </div>
    <!-- ======================================================================================== -->
    <h2 class=tutorial><a name=Exercise_3></a>使用 Web 服务</h2>
    <p class=tutorial>部署 Web 服务后，我们需要创建一个客户端来使用 Web 服务的 <tt><font size=2>add</font></tt> 方法。在本练习中，我们将创建三个客户端，即 Java SE 应用程序中的 Java 类、Web 应用程序中的 Servlet 和 JSP 页。</p>
    <div class=indent>
    <h3 class=tutorial><a name=Exercise_3_1></a>客户端 1：Java SE 应用程序中的 Java 类</h3>
    <ol>
        <li>选择&#8220;文件&#8221;&gt;&#8220;新建项目&#8221;(Ctrl-Shift-N)。从&#8220;常规&#8221;类别中，选择&#8220;Java 应用程序&#8221;。将项目命名为 <tt><font size=2>CalculatorWS_Client_Application</font></tt>。
        <p><strong>注意：</strong>在编写本文档时，<a href="https://jax-ws.dev.java.net/issues/show_bug.cgi?id=10"><u><font color=#1e2a60>问题 10</font></u></a> 还仍未得到解决，您必须在其路径<strong>不</strong>包含空格的项目文件夹中创建 Web 服务客户端。例如，路径不能为 "C:\Documents and Settings\..."。</p>
        <p>单击&#8220;完成&#8221;。 </p>
        <li>右键单击 "CalculatorWS_Client_Application" 节点，然后选择&#8220;新建&#8221;&gt;&#8220;Web 服务客户端&#8221;。
        <li>在&#8220;项目&#8221;中，单击&#8220;浏览&#8221;。找到要使用的 Web 服务。选择 Web 服务后，单击&#8220;确定&#8221;。
        <li>在&#8220;包&#8221;中键入 <tt><font size=2>org.me.calculator.client</font></tt>，然后单击&#8220;完成&#8221;。
        <p>将在&#8220;项目&#8221;窗口中显示新建的 Web 服务客户端：
        <p align=center><img src="http://testwww.netbeans.org/images/articles/javaee5/jax-ws-project-view2_zh_CN.png" border=1> </p>
        <li>双击 "Main.java" 以在源代码编辑器中将其打开。删除 TODO 注释，并在该行中单击鼠标右键。选择&#8220;Web 服务客户端资源&#8221;&gt;&#8220;调用 Web 服务操作&#8221;。</tt>
        <li>找到&#8220;添加&#8221;操作，然后单击&#8220;确定&#8221;。
        <li>将带红色下划线的行更改为以下内容：
        <p>&#160;</p>
        <pre class=examplecode>    System.out.println("Sum: " + port.add(3,4));</pre>
        <li>右键单击项目节点，然后选择&#8220;运行项目&#8221;。
        <p>现在&#8220;输出&#8221;窗口会显示以下内容：
        <p>&#160;</p>
        <pre class=examplecode>    compile:
        run:
        Sum: 7
        BUILD SUCCESSFUL (total time: 1 second)</pre>
        <p>&#160;</p>
        </li>
    </ol>
    <h3 class=tutorial><a name=Exercise_3_2></a>客户端 2：Web 应用程序中的 Servlet</h3>
    <ol>
        <li>选择&#8220;文件&#8221;&gt;&#8220;新建项目&#8221;(Ctrl-Shift-N)。从 "Web" 类别中，选择&#8220;Web 应用程序&#8221;。将项目命名为 <tt><font size=2>CalculatorWSServletClient</font></tt>。
        <p><strong>注意：</strong>在编写本文档时，<a href="https://jax-ws.dev.java.net/issues/show_bug.cgi?id=10"><u><font color=#1e2a60>问题 10</font></u></a> 还仍未得到解决，您必须在其路径<strong>不</strong>包含空格的项目文件夹中创建 Web 服务客户端。例如，路径不能为 "C:\Documents and Settings\..."。
        <p>单击&#8220;完成&#8221;。 </p>
        <li>右键单击 "CalculatorWSServletClient" 节点，然后选择&#8220;新建&#8221;&gt;&#8220;Web 服务客户端&#8221;。
        <li>在&#8220;项目&#8221;中，单击&#8220;浏览&#8221;。找到要使用的 Web 服务。选择 Web 服务后，单击&#8220;确定&#8221;。
        <li>在&#8220;包&#8221;中键入 <tt><font size=2>org.me.calculator.client</font></tt>，然后单击&#8220;完成&#8221;。
        <p>将在&#8220;项目&#8221;窗口中显示新建的 Web 服务客户端：
        <p align=center><img src="http://testwww.netbeans.org/images/articles/javaee5/jax-ws-project-view3_zh_CN.png" border=1> </p>
        <li>右键单击项目节点，然后选择&#8220;新建&#8221;&gt; "Servlet"。将 Servlet 命名为 <tt><font size=2>ClientServlet</font></tt>，并将它放置在名为 <tt><font size=2>org.me.calculator.client</font></tt> 的包中。单击&#8220;完成&#8221;。要使 Servlet 成为应用程序的入口点，请右键单击该项目节点，选择&#8220;属性&#8221;，单击&#8220;运行&#8221;，然后在&#8220;相对 URL&#8221;中键入 <tt><font size=2>/ClientServlet</font></tt>。单击&#8220;确定&#8221;。
        <li>在源代码编辑器中，删除注释掉 <tt><font size=2>processRequest</font></tt> 方法主体的行。该行是注释掉代码部分的起始行：
        <p>&#160;</p>
        <pre class=examplecode>    /* TODO output your page here</pre>
        <p>然后，删除注释掉代码部分的结束行：
        <p>&#160;</p>
        <pre class=examplecode>    */</pre>
        <p>在以下行之后添加一些空行：
        <p>&#160;</p>
        <pre class=examplecode>    out.println("&lt;h1&gt;Servlet ClientServlet at " + request.getContextPath () + "&lt;/h1&gt;");</pre>
        <p>现在，在您添加的一个空行中单击鼠标右键，选择&#8220;Web 服务客户端资源&#8221;&gt;&#8220;调用 Web 服务操作&#8221;。将出现&#8220;选择要调用的操作&#8221;对话框。 </p>
        <li>找到 <tt><font size=2>add</font></tt> 操作，然后单击&#8220;确定&#8221;。
        <p>现在 <tt><font size=2>processRequest</font></tt> 方法应该如下所示（下面以粗体显示添加的代码）：
        <p>&#160;</p>
        <pre class=examplecode>    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("&lt;html&gt;");
        out.println("&lt;head&gt;");
        out.println("&lt;title&gt;Servlet ClientServlet&lt;/title&gt;");
        out.println("&lt;/head&gt;");
        out.println("&lt;body&gt;");
        out.println("&lt;h1&gt;Servlet ClientServlet at " + request.getContextPath () + "&lt;/h1&gt;");
        <strong>try { // Call Web Service Operation
        org.me.calculator.client.CalculatorWSService service = new org.me.calculator.client.CalculatorWSService();
        org.me.calculator.client.CalculatorWS port = service.getCalculatorWSPort();
        // TODO initialize WS operation arguments here
        int arg0 = 0;
        int arg1 = 0;
        // TODO process result here
        int result = port.add(arg0, arg1);
        System.out.println("Result = "+result);
        } catch (Exception ex) {
        // TODO handle custom exceptions here
        }</strong>
        out.println("&lt;/body&gt;");
        out.println("&lt;/html&gt;");
        out.close();
        }</pre>
        <p>&#160;</p>
        <p>将 <tt><font size=2>arg0</font></tt> 和 <tt><font size=2>arg1</font></tt> 的值更改为其他数值，如 3 和 4。
        <p>将 <tt><font size=2>System.out.println</font></tt> 语句更改为 <tt><font size=2>out.println</font></tt>。
        <p>添加输出异常的行（如果抛出异常）。
        <p>现在 try/catch 块应该如下所示（会突出显示新行和更改行）：
        <p>&#160;</p>
        <pre class=examplecode>    try { // Call Web Service Operation
        org.me.calculator.client.CalculatorWSService service = new org.me.calculator.client.CalculatorWSService();
        org.me.calculator.client.CalculatorWSApplication port = service.getCalculatorWSApplicationPort();
        // TODO initialize WS operation arguments here
        <strong>int arg0 = 3;
        int arg1 = 4;</strong>
        // TODO process result here
        int result = port.add(arg0, arg1);
        <strong>out.println("&lt;p&gt;Result: " + result);</strong>
        } catch (Exception ex) {
        <strong>out.println("&lt;p&gt;Exception: " + ex);</strong>
        }</pre>
        <p>&#160;</p>
        <li>右键单击项目节点，然后选择&#8220;运行项目&#8221;。
        <p>将启动服务器（如果它尚未运行），生成并部署应用程序，并且打开浏览器以显示计算结果。 </p>
        </li>
    </ol>
    <h3 class=tutorial><a name=Exercise_3_3></a>客户端 3：Web 应用程序中的 JSP 页</h3>
    <ol>
        <li>选择&#8220;文件&#8221;&gt;&#8220;新建项目&#8221;(Ctrl-Shift-N)。从 "Web" 类别中，选择&#8220;Web 应用程序&#8221;。将项目命名为 <tt><font size=2>CalculatorWSJSPClient</font></tt>。
        <p><strong>注意：</strong>在编写本文档时，<a href="https://jax-ws.dev.java.net/issues/show_bug.cgi?id=10"><u><font color=#1e2a60>问题 10</font></u></a> 还仍未得到解决，您必须在其路径<strong>不</strong>包含空格的项目文件夹中创建 Web 服务客户端。例如，路径不能为 "C:\Documents and Settings\..."。
        <p>单击&#8220;完成&#8221;。 </p>
        <li>右键单击 "CalculatorWSJSPClient" 节点，然后选择&#8220;新建&#8221;&gt;&#8220;Web 服务客户端&#8221;。
        <li>在&#8220;项目&#8221;中，单击&#8220;浏览&#8221;。找到要使用的 Web 服务。选择 Web 服务后，单击&#8220;确定&#8221;。
        <li>在&#8220;包&#8221;中键入 <tt><font size=2>org.me.calculator.client</font></tt>，然后单击&#8220;完成&#8221;。
        <p>将在&#8220;项目&#8221;窗口中显示新建的 Web 服务客户端。</p>
        <li>在&#8220;Web 页&#8221;文件夹中，双击 "index.jsp" 以在源代码编辑器中将其打开。
        <li>在&#8220;Web 服务引用&#8221;节点中，展开表示 Web 服务的节点。现在将公开要从客户端调用的 <tt><font size=2>add</font></tt> 操作。
        <li>将 <tt><font size=2>add</font></tt> 操作拖动到客户端的 <tt><font size=2>index.jsp</font></tt> 页上，并将其放在 H1 标记之下。现在，将在 <tt><font size=2>index.jsp</font></tt> 页中生成用于调用服务操作的代码。
        <p>将 <tt><font size=2>arg0</font></tt> 和 <tt><font size=2>arg1</font></tt> 的值更改为其他数值，如 3 和 4。 </p>
        <li>右键单击项目节点，然后选择&#8220;运行项目&#8221;。
        <p>将启动服务器（如果它尚未运行），生成并部署应用程序，并且打开浏览器以显示计算结果：
        <p align=center><img src="http://testwww.netbeans.org/images/articles/javaee5/jax-ws-project-jsp-result.png" border=1> </p>
        </li>
    </ol>
    <br>
    <div class=feedback-box><a href="http://testwww.netbeans.org/about/contact_form.html?to=3&amp;subject=Feedback:%20Web%20Services%20in%20Java%20EE%205"><u><font color=#1e2a60>请将您的意见和建议发送给我们</font></u></a></div>
    <br style="CLEAR: both"><!-- ======================================================================================= -->
    <h2><a name=nextsteps></a>后续步骤</h2>
    <p>有关使用 NetBeans IDE 5.5 开发 Java EE 应用程序的更多信息，请参见以下资源： </p>
    <ul>
        <li><a href="http://testwww.netbeans.org/kb/55/javaee-intro_zh_CN.html"><u><font color=#1e2a60>Java EE 5 技术简介</font></u></a>
        <li><a href="http://testwww.netbeans.org/kb/55/persistence_zh_CN.html"><u><font color=#1e2a60>Java EE 5 平台中的 Java 持久性</font></u></a>
        <li><a href="http://testwww.netbeans.org/kb/55/customer-book_zh_CN.html"><u><font color=#1e2a60>比较 Java EE 5 平台和 J2EE 1.4 平台</font></u></a>
        <li><a href="http://testwww.netbeans.org/kb/trails/java-ee_zh_CN.html"><u><font color=#1e2a60>Java EE 应用程序相关教程</font></u></a> </li>
    </ul>
    <p>要发送意见和建议、获得支持以及随时了解 NetBeans IDE Java EE 开发功能的最新开发情况，请<a href="http://testwww.netbeans.org/community/lists/top.html"><u><font color=#1e2a60>加入 nbj2ee@netbeans.org 邮件列表</font></u></a>。</p>
    <!-- /Servlet-Specific template --><!-- end of right navigation --><!-- servlets and anything not on www or testwww --></div>
<img src ="http://www.blogjava.net/lewhwa/aggbug/115782.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lewhwa/" target="_blank">网路冷眼@BlogJava</a> 2007-05-07 23:24 <a href="http://www.blogjava.net/lewhwa/articles/115782.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>EJB 3.0 Enterprise Beans（转载）</title><link>http://www.blogjava.net/lewhwa/articles/115781.html</link><dc:creator>网路冷眼@BlogJava</dc:creator><author>网路冷眼@BlogJava</author><pubDate>Mon, 07 May 2007 15:19:00 GMT</pubDate><guid>http://www.blogjava.net/lewhwa/articles/115781.html</guid><wfw:comment>http://www.blogjava.net/lewhwa/comments/115781.html</wfw:comment><comments>http://www.blogjava.net/lewhwa/articles/115781.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lewhwa/comments/commentRss/115781.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lewhwa/services/trackbacks/115781.html</trackback:ping><description><![CDATA[原文地址：<a href="http://testwww.netbeans.org/kb/55/ejb30_zh_CN.html">http://testwww.netbeans.org/kb/55/ejb30_zh_CN.html</a><br><br>
<meta name=description value="A template for writing tutorials for netbeans.org">
<link href="http://www.netbeans.org/netbeans.css" rel=stylesheet>
<meta content="ken ganfield、jan horvath" name=author>
<meta content="java IDE, java editor, java Platform, free, open source, java development, j2ee support, development enviroment, web development, NetBeans, IDE, java" name=keywords><script language=JavaScript type=text/javascript>
<!--
function get_sc_login() {
var sclogin = "guest";
if(sclogin.indexOf("()")==-1) {
return (sclogin);
} else {
return "guest";
}
}
var username=get_sc_login();
//-->
</script><!-- begin TopTabs  --><!-- end TopTabs --><!-- Servlet-Specific template -->
<h1>EJB 3.0 Enterprise Beans</h1>
<p>本文档介绍了有关使用 EJB 3.0 技术（作为 Java EE 5 平台的一部分）开发企业应用程序的基础知识，同时说明了 EJB 3.0 技术是如何简化企业应用程序的开发过程的。本文档使用的是 <a href="http://www.netbeans.org/products/ide/index_zh_CN.html">NetBeans IDE 5.5 发行版本</a>。 </p>
<!--
<div style="border: 1px solid rgb(14, 27, 85); padding: 10px 30px; background-color: rgb(231, 231, 231); margin-left: 60px; margin-right: 40px;">
Please note that this release is a beta release.
For our latest full release, go to the <a href="http://www.netbeans.info/downloads/index.php">NetBeans
Download page</a> and download NetBeans IDE 5.0.</div>
-->
<p align=center><strong>预计持续时间：30 分钟</strong></p>
<div class=indent>
<h3 class=tutorial>先决条件</h3>
<p>本文档假定您已具备了以下技术的一些基本知识或编程经验：</p>
<ul>
    <li>Java 编程
    <li>NetBeans IDE </li>
</ul>
<h3 class=tutorial>本教程所需的软件</h3>
<p>在学习本教程之前，您需要在计算机中安装以下软件：</p>
<ul>
    <li><a href="http://www.netbeans.org/products/ide/index_zh_CN.html">NetBeans IDE 5.5</a>（<a href="http://www.netbeans.info/downloads/index.php">下载</a>）
    <li>Java Standard Development Kit (JDK) 版本 5.0 或版本 6.0（<a href="http://java.sun.com/javase/downloads/index.jsp">下载</a>）
    <li>Sun Java System Application Server Platform Edition 9（<a href="http://www.sun.com/software/products/appsrvr_pe/index.xml">下载</a>） </li>
</ul>
<p>在学习本教程之前，您需要在 IDE 中注册 Sun Java System Application Server 的本地实例。</p>
<h3 class=tutorial>教程练习</h3>
<ul>
    <li><a href="http://testwww.netbeans.org/kb/55/ejb30_zh_CN.html#Exercise_1">建立企业应用程序项目</a>
    <li><a href="http://testwww.netbeans.org/kb/55/ejb30_zh_CN.html#Exercise_2">对 EJB 模块进行编码</a>
    <li><a href="http://testwww.netbeans.org/kb/55/ejb30_zh_CN.html#Exercise_3">对 Web 模块进行编码</a>
    <li><a href="http://testwww.netbeans.org/kb/55/ejb30_zh_CN.html#Exercise_4">运行项目</a>
    <li><a href="http://testwww.netbeans.org/kb/55/ejb30_zh_CN.html#Exercise_5">疑难解答</a> </li>
</ul>
</div>
<br><!-- ===================================================================================== -->
<h2><a name=Exercise_1></a><!--exercise 1: -->建立企业应用程序项目</h2>
<p>本练习的目的是：创建包含一个 EJB 模块和一个 Web 模块的 NewsApp 企业应用程序项目。NewsApp 应用程序使用消息驱动 Bean 接收和处理 Servlet 发送到队列中的消息。该应用程序使用 Servlet 将消息发送到消息驱动 Bean 并显示消息。</p>
<div class=indent>
<h3 class=tutorial>创建企业应用程序</h3>
<ol>
    <li>从主菜单中选择&#8220;文件&#8221;&gt;&#8220;新建项目&#8221;(Ctrl-Shift-N)。
    <li>从&#8220;企业&#8221;类别中选择&#8220;企业应用程序&#8221;，然后单击&#8220;下一步&#8221;。
    <li>将项目命名为 NewsApp，并将服务器设置为 Sun Java System Application Server。
    <li>将 J2EE 版本设置为 "Java EE 5"，然后选中&#8220;创建 EJB 模块&#8221;和&#8220;创建 Web 应用程序模块&#8221;（如果未选中）。
    <li>单击&#8220;完成&#8221;。 </li>
</ol>
<h3 class=tutorial>小结</h3>
<p>在本练习中，我们创建了包含一个 EJB 模块和一个 Web 模块的 Java EE 5 企业应用程序。</p>
</div>
<h2><a name=Exercise_2></a>对 EJB 模块进行编码</h2>
<p>在本练习中，我们将在 EJB 模块中创建对象。我们将创建实体类、消息驱动 Bean 和会话 Facade。我们还将创建一个持久性单元，以便为容器提供用于管理实体的信息，以及消息驱动 Bean 将使用的 Java 消息服务 (Java Message Service, JMS) 资源。</p>
<div class=indent>
<h3 class=tutorial>创建持久性单元</h3>
<p>首先，我们将创建一个持久性单元，它用于定义在应用程序中使用的数据源和实体管理器。</p>
<ol>
    <li>右键单击 EJB 模块，然后选择&#8220;新建&#8221;&gt;&#8220;文件/文件夹&#8221;。
    <li>从&#8220;持久性&#8221;类别中，选择&#8220;持久性单元&#8221;，然后单击&#8220;下一步&#8221;。
    <li>保留缺省的持久性单元名称。
    <li>对于持久性提供程序，请选择&#8220;TopLink（缺省）&#8221;。
    <li>对于数据源，请选择缺省的数据源 <tt><font size=2>jdbc/sample</font></tt>。
    <li>检查是否为持久性单元选中了&#8220;使用 Java 事务 API&#8221;，以及&#8220;表生成策略&#8221;是否设置为&#8220;创建&#8221;，以便在部署应用程序时创建基于实体类的表。
    <li>单击&#8220;完成&#8221;。 </li>
</ol>
<p align=center><img height=315 alt=&#8220;新建持久性单元&#8221;向导 src="http://testwww.netbeans.org/images/articles/javaee5/newsapp_persistence_unit_wizard_zh_CN.png" width=578>
<p>单击&#8220;完成&#8221;后，IDE 将创建 <tt><font size=2>persistence.xml</font></tt>，并在源代码编辑器中将其打开。关闭 <tt><font size=2>persistence.xml</font></tt>。</p>
<!--
<h3 class="tutorial">Creating the JMS Resources</h3>
<p>We will use the JMS API to send and receive messages in our enterprise application.
We need to create our JMS resources, a destination and connection factory, and then register the resources with the application server.
We will create <tt>jms/Queue</tt> and <tt>jms/NewsConnectionFactory</tt>.
When we create our message-driven bean, we will use annotations to introduce <tt>jms/Queue</tt> directly in the bean class.
Later, when we create our servlets in the web module, we will inject <tt>jms/Queue</tt> and <tt>jms/NewsConnectionFactory</tt> directly into the servlet class.</p>
<p>To create our <tt>jms/Queue</tt> resource, do the following:</p>
<ol>
    <li>Right-click the EJB module in the project and choose New &gt; File/Folder to open the New File wizard.</li>
    <li>From the Sun Resources category, select JMS Resource and click Next.</li>
    <li>Type <tt>jms/Queue</tt> for the JNDI Name, select <tt>javax.jms.Queue</tt> for the
    Admin Object Resource type, and click Next.</li>
    <li>Type <tt>Queue</tt> for the value of the Name property and click Finish.</li>
</ol>
<p>To create our <tt>jms/NewsConnectionFactory</tt> resource, do the following:</p>
<ol>
    <li>Right-click the EJB module in the project and choose New &gt; File/Folder to open the New File wizard.</li>
    <li>From the Sun Resources category, select JMS Resource and click Next.</li>
    <li>Type <tt>jms/NewsConnectionFactory</tt> for the JNDI Name, select <tt>javax.jms.ConnectionFactory</tt> for the
    Connector Resource type, and click Finish.</li>
</ol>
<p>Now that we have created the JMS resources, we need to register the resources with the server.
To register the resources, do the following:</p>
<ol>
    <li>Start the application server if the server is not already running.
    You can start the application server in the Runtime window of the IDE
    by expanding the Servers node and right-clicking the application server node and choosing Start.</li>
    <li>In the Projects window, expand the Server Resources node in your EJB module.</li>
    <li>For each of the resources we created, right-click the resource and select Register from the pop-up menu,
    and then click Register in the dialog box.</li>
</ol>
<p>You can see if the resources are registered by expanding the application server node in the Runtime window
and looking under the JMS Resources node.</p>
-->
<h3 class=tutorial>创建 <tt><font size=3>NewsEntity</font></tt> 实体类</h3>
<p>在本练习中，我们将创建 <tt><font size=2>NewsEntity</font></tt> 实体类。实体类是一个简单的 Java 类。在创建实体类时，IDE 会添加 <tt><font size=2>@Entity</font></tt> 标注以将该类定义为实体类。当创建了类后，我们将在该类中创建字段以表示表中所需的数据。</p>
<p>每个实体类都必须具有一个主键。在创建实体类时，IDE 会添加 <tt><font size=2>@Id</font></tt> 标注以声明要用作主键的字段。此外，IDE 还会添加 <tt><font size=2>@Generated</font></tt> 标注以指定主 Id 的键生成策略。</p>
<p>要创建 <tt><font size=2>NewsEntity</font></tt> 类，请执行以下操作：</p>
<ol>
    <li>在&#8220;项目&#8221;窗口中右键单击 EJB 模块，然后选择&#8220;新建&#8221;&gt;&#8220;文件/文件夹&#8221;打开&#8220;新建文件&#8221;向导。
    <li>从&#8220;持久性&#8221;类别中，选择&#8220;实体类&#8221;，然后单击&#8220;下一步&#8221;。
    <li>键入 <tt><font size=2>NewsEntity</font></tt> 作为类名，键入 <tt><font size=2>ejb</font></tt> 作为包名，并将&#8220;主键类型&#8221;保留为 <tt><font size=2>Long</font></tt>。单击&#8220;完成&#8221;。 </li>
</ol>
<p>单击&#8220;完成&#8221;后，将在源代码编辑器中打开实体类 <tt><font size=2>NewsEntity.java</font></tt>。在源代码编辑器中，请执行以下操作：</p>
<ol>
    <li>将以下字段声明添加到类中：
    <pre class=examplecode>String title;
    String body;</pre>
    <li>在源代码编辑器中单击鼠标右键，然后选择&#8220;重构&#8221;&gt;&#8220;封装字段&#8221;以便为每个字段生成 getter 和 setter。在&#8220;封装字段&#8221;对话框中，确保选中了字段 <tt><font size=2>id</font></tt>、<tt><font size=2>title</font></tt> 和 <tt><font size=2>body</font></tt> 的 getter 和 setter 复选框。
    <li>在&#8220;封装字段&#8221;对话框中单击&#8220;下一步&#8221;，然后在&#8220;输出&#8221;窗口的&#8220;重构&#8221;标签中单击&#8220;执行重构&#8221;。IDE 将为各字段添加 getter 和 setter 方法，并将字段的可视性更改为 <tt><font size=2>private</font></tt>。
    <li>保存对文件所做的更改。 </li>
</ol>
<p>在接下来的步骤中，我们将创建 <tt><font size=2>NewMessage</font></tt> 消息驱动 Bean。</p>
<h3 class=tutorial>创建 <tt><font size=3>NewMessage</font></tt> 消息驱动 Bean</h3>
<p>现在我们将在 EJB 模块中创建 <tt><font size=2>NewMessage</font></tt> 消息驱动 Bean。我们将使用&#8220;新建消息驱动 Bean&#8221;向导来创建 Bean 和所需的 JMS 资源。 </p>
<p>要创建 <tt><font size=2>NewMessage</font></tt> 消息驱动 Bean，请执行以下操作：</p>
<ol>
    <li>在&#8220;项目&#8221;窗口中右键单击 EJB 模块，然后选择&#8220;新建&#8221;&gt;&#8220;文件/文件夹&#8221;以打开&#8220;新建文件&#8221;向导。
    <li>从&#8220;企业&#8221;类别中，选择&#8220;消息驱动 Bean&#8221;，然后单击&#8220;下一步&#8221;。
    <li>键入 <tt><font size=2>NewMessage</font></tt> 作为类名。
    <li>从&#8220;包&#8221;下拉列表中选择 "ejb"。
    <li>选择&#8220;队列&#8221;作为目标类型，然后单击&#8220;完成&#8221;。 </li>
</ol>
<p>单击&#8220;完成&#8221;后，将在源代码编辑器中打开新建的消息驱动 Bean 类 <tt><font size=2>NewMessage.java</font></tt>。您可以看到该类具有以下标注：</p>
<pre class=examplecode>@MessageDriven(mappedName = "jms/NewMessage")</pre>
<p>此标注向容器说明：该组件是一个消息驱动 Bean 并且该 Bean 使用 JMS 资源。当 IDE 生成类时，将从类 (<tt><font size=2>NewMessage.java</font></tt>) 名称派生资源的映射名 (<tt><font size=2>jms/NewMessage</font></tt>)。JMS 资源被映射到目标的 JNDI 名称，Bean 从该目标中接收消息。<!--we provided the JNDI name when we created the resource.-->&#8220;新建消息驱动 Bean&#8221;向导已为我们创建了 JMS 资源。通过 EJB 3.0 API，我们可以从 Bean 类中查找 JNDI 名称空间中的对象，这样就不需要配置部署描述符来指定 JMS 资源了。</p>
<p>EJB 3.0 规范允许您使用标注将资源直接引入类中。现在，我们准备使用标注将 <tt><font size=2>MessageDrivenContext</font></tt> 资源引入类中，然后注入 <tt><font size=2>PersistenceContext</font></tt> 资源，EntityManager API 将使用该资源来管理持久性实体实例。我们要在源代码编辑器中将标注添加到类中。</p>
<ol>
    <li>通过在类中添加以下带标注的字段（以粗体显示），将 <tt><font size=2>MessageDrivenContext</font></tt> 资源注入到类中：
    <pre class=examplecode>public class NewMessage implements MessageListener {
    <strong>@Resource
    private MessageDrivenContext mdc;</strong></pre>
    <li>在代码中单击鼠标右键，然后从弹出式菜单中选择&#8220;持久性&#8221;&gt;&#8220;使用实体管理器&#8221;，将实体管理器引入类中。<br>这会在源代码中添加以下标注：
    <pre class=examplecode>@PersistenceContext
    private EntityManager em;</pre>
    并在代码中生成以下方法：
    <pre class=examplecode>public void persist(Object object) {
    // TODO:
    // em.persist(object);
    }</pre>
    <li>按如下所示修改 <tt><font size=2>persist</font></tt> 方法：
    <pre class=examplecode>public void save(Object object) {
    em.persist(object);
    }</pre>
    <li>通过将以下代码添加到主体中来修改 <tt><font size=2>onMessage</font></tt> 方法：
    <pre class=examplecode>    ObjectMessage msg = null;
    try {
    if (message instanceof ObjectMessage) {
    msg = (ObjectMessage) message;
    NewsEntity e = (NewsEntity) msg.getObject();
    save(e);
    }
    } catch (JMSException e) {
    e.printStackTrace();
    mdc.setRollbackOnly();
    } catch (Throwable te) {
    te.printStackTrace();
    }</pre>
    <li>按 Alt-Shift-F 组合键生成所有必要的 import 语句。在生成 import 语句时，我们需要确保导入 <tt><font size=2>jms</font></tt> 和 <tt><font size=2>javax.annotation.Resource;</font></tt> 库。
    <li>保存该文件。 </li>
</ol>
<h3 class=tutorial>创建会话 Bean</h3>
<p>接下来，我们将为 <tt><font size=2>NewsEntity</font></tt> 实体类创建一个会话 Facade。要创建会话 Facade，请执行以下操作：</p>
<ol>
    <li>右键单击 EJB 模块，然后选择&#8220;新建&#8221;&gt;&#8220;文件/文件夹&#8221;。
    <li>从&#8220;持久性&#8221;类别中，选择&#8220;实体类的会话 Bean&#8221;，然后单击&#8220;下一步&#8221;。
    <li>从可用的实体类列表中，选择 "NewsEntity"，单击&#8220;添加&#8221;，然后单击&#8220;下一步&#8221;。
    <li>检查是否将包设置为 <tt><font size=2>ejb</font></tt> 以及是否选中了创建本地接口。
    <li>单击&#8220;完成&#8221;。 </li>
</ol>
<p>单击&#8220;完成&#8221;后，将创建会话 Facade 类 <tt><font size=2>NewsEntityFacade.java</font></tt>，并在源代码编辑器中将其打开。IDE 还将创建本地接口 <tt><font size=2>NewsEntityFacadeLocal.java</font></tt>。 </p>
<p>EJB 3.0 技术通过减少所需的代码量来简化会话 Bean 的创建。您可以看到，标注 <tt><font size=2>@Stateless</font></tt> 用于将类声明为无态会话 Bean 组件，并且该类不再需要实现 <tt><font size=2>javax.ejb.SessionBean</font></tt> 的语句。此外，代码也更为简洁了，因为利用 EJB 3.0 技术，业务方法不再需要使用代码来声明其抛出了所检查到的异常。</p>
<p>您会看到，在创建会话 Facade 时，<tt><font size=2>PersistenceContext</font></tt> 资源已直接注入到会话 Bean 组件中。</p>
<h3 class=tutorial>小结</h3>
<p>在本练习中，我们已对 EJB 模块中的实体类和消息驱动 Bean 进行了编码，然后为实体类创建了会话 Facade。此外，我们还创建了应用程序使用的 JMS 资源。</p>
</div>
<!-- ===================================================================================== -->
<h2><a name=Exercise_3></a>对 Web 模块进行编码</h2>
<p>现在，我们将在 Web 模块中创建 Servlet <tt><font size=2>ListNews</font></tt> 和 <tt><font size=2>PostMessage</font></tt>。这些 Servlet 将用于读取和添加消息。</p>
<div class=indent>
<h3 class=tutorial>创建 <tt><font size=3>ListNews</font></tt> Servlet</h3>
<p>在本练习中，我们将创建一个用于显示数据的简单 Servlet。我们将使用标注从 Servlet 中调用实体 Bean。</p>
<ol>
    <li>右键单击 Web 模块项目，然后选择&#8220;新建&#8221;&gt; "Servlet"。
    <li>键入 <tt><font size=2>ListNews</font></tt> 作为类名。
    <li>输入 <tt><font size=2>web</font></tt> 作为包名，然后单击&#8220;完成&#8221;。 </li>
</ol>
<p>单击&#8220;完成&#8221;后，将在源代码编辑器中打开类 <tt><font size=2>ListNews.java</font></tt>。在源代码编辑器中，请执行以下操作：</p>
<ol>
    <li>在源代码中，单击鼠标右键，然后选择&#8220;企业资源&#8221;&gt;&#8220;调用 Enterprise Bean&#8221;。
    <li>在&#8220;调用 Enterprise Bean&#8221;对话框中，选择 "NewsEntityFacade"，然后单击&#8220;确定&#8221;。单击&#8220;确定&#8221;后，将使用 <tt><font size=2>@EJB</font></tt> 标注在 Servlet 中注入实体 Bean 资源。
    <li>在 <tt><font size=2>processRequest</font></tt> 方法中，对其进行如下修改：取消代码注释，然后将下面以粗体显示的行添加到方法主体中。
    <pre class=examplecode>out.println("&lt;h1&gt;Servlet ListNews at " + request.getContextPath () + "&lt;/h1&gt;");
    <strong>List news = newsEntityFacade.findAll();
    for (Iterator it = news.iterator(); it.hasNext();) {
    NewsEntity elem = (NewsEntity) it.next();
    out.println(" &lt;b&gt;"+elem.getTitle()+" &lt;/b&gt;&lt;br /&gt;");
    out.println(elem.getBody()+"&lt;br /&gt; ");
    }
    out.println("&lt;a href='PostMessage'&gt;Add new message&lt;/a&gt;");</strong>
    out.println("&lt;/body&gt;");
    </pre>
    <li>按 Alt-Shift-F 组合键为类生成所有必要的 import 语句。在生成 import 语句时，我们希望从 <tt><font size=2>util</font></tt> 包中导入类。
    <li>保存对文件所做的更改。 </li>
</ol>
<h3 class=tutorial>创建 <tt><font size=3>PostMessage</font></tt> Servlet</h3>
<p>在本练习中，我们将创建用于传递消息的 <tt><font size=2>PostMessage</font></tt> Servlet。我们将使用标注将所创建的 JMS 资源直接注入 Servlet 中，并且指定变量名称及其映射到的名称。然后，添加用于发送 JMS 消息的代码，以及用于在 HTML 表单中添加消息的代码。</p>
<ol>
    <li>右键单击 Web 模块项目，然后选择&#8220;新建&#8221;&gt; "Servlet"。
    <li>键入 <tt><font size=2>PostMessage</font></tt> 作为类名。
    <li>输入 <tt><font size=2>web</font></tt> 作为包名，然后单击&#8220;完成&#8221;。 </li>
</ol>
<p>单击&#8220;完成&#8221;后，将在源代码编辑器中打开类 <tt><font size=2>PostMessage.java</font></tt>。在源代码编辑器中，请执行以下操作：</p>
<ol>
    <li>通过添加下面以粗体显示的字段声明，使用标注来注入 <tt><font size=2>ConnectionFactory</font></tt> 和 <tt><font size=2>Queue</font></tt> 资源：
    <pre class=examplecode>public class PostMessage extends HttpServlet {
    <strong>@Resource(mappedName="jms/NewMessageFactory")
    private  ConnectionFactory connectionFactory;
    @Resource(mappedName="jms/NewMessage")
    private  Queue queue;</strong></pre>
    <li>现在，通过将下面以粗体显示的代码添加到 <tt><font size=2>processRequest</font></tt> 方法中，添加用于发送 JMS 消息的代码：
    <pre class=examplecode>response.setContentType("text/html;charset=UTF-8");
    // Add the following code to send the JMS message
    <strong>String title=request.getParameter("title");
    String body=request.getParameter("body");
    if ((title!=null) &amp;&amp; (body!=null)) {
    try {
    Connection connection = connectionFactory.createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer messageProducer = session.createProducer(queue);
    ObjectMessage message = session.createObjectMessage();
    // here we create NewsEntity, that will be sent in JMS message
    NewsEntity e = new NewsEntity();
    e.setTitle(title);
    e.setBody(body);
    message.setObject(e);
    messageProducer.send(message);
    messageProducer.close();
    connection.close();
    response.sendRedirect("ListNews");
    } catch (JMSException ex) {
    ex.printStackTrace();
    }
    }</strong>
    PrintWriter out = response.getWriter();
    </pre>
    <li>现在，将对输出 HTML 的代码取消注释，并添加用于添加消息的 Web 表单。将下面以粗体显示的代码行添加到 <tt><font size=2>processRequest</font></tt> 方法中：
    <pre class=examplecode>out.println("Servlet PostMessage at " + request.getContextPath() + "&lt;/h1&gt;");
    // Add the following code to add the form to the web page
    <strong>out.println("&lt;form&gt;");
    out.println("Title: &lt;input type='text' name='title'&gt;&lt;br/&gt;");
    out.println("Message: &lt;textarea name='body'&gt;&lt;/textarea&gt;&lt;br/&gt;");
    out.println("&lt;input type='submit'&gt;&lt;br/&gt;");
    out.println("&lt;/form&gt;");</strong>
    out.println("&lt;/body&gt;");
    </pre>
    <li>按 Alt-Shift-F 组合键为类生成所有必要的 import 语句。在选择 <tt><font size=2>Connection</font></tt>、<tt><font size=2>ConnectionFactory</font></tt>、<tt><font size=2>Session</font></tt> 和 <tt><font size=2>Queue</font></tt> 的 import 语句时，将导入 <tt><font size=2>java.jms</font></tt> 库。
    <li>保存对文件所做的更改。 </li>
</ol>
</div>
<h2><a name=Exercise_4></a>运行项目</h2>
<p>现在可以运行项目了。在运行项目时，我们希望浏览器打开包含 <tt><font size=2>ListNews</font></tt> Servlet 的页面。可以通过在企业应用程序的&#8220;属性&#8221;对话框中指定该页的 URL 来实现这一目的。该 URL 是应用程序的上下文路径的相对 URL。输入相对 URL 后，可以从&#8220;项目&#8221;窗口中生成、部署并运行应用程序。</p>
<p>要设置相对 URL 并运行应用程序，请执行以下操作：</p>
<ol>
    <li>在&#8220;项目&#8221;窗口中，右键单击 "NewsApp" 企业应用程序节点，然后从弹出式菜单中选择&#8220;属性&#8221;。
    <li>在&#8220;类别&#8221;窗格中选择&#8220;运行&#8221;。
    <li>在&#8220;相对 URL&#8221;文本字段中，键入 /ListNews。
    <li>单击&#8220;确定&#8221;。
    <li>在&#8220;项目&#8221;窗口中，右键单击 "NewsApp" 企业应用程序节点，然后选择&#8220;运行项目&#8221;。 </li>
</ol>
<p>运行项目时，将在浏览器中打开 <tt><font size=2>ListNews</font></tt> Servlet，该 Servlet 用于显示数据库中的消息列表。如果您是第一次运行项目，则数据库为空，但是您可以单击&#8220;添加消息&#8221;来添加消息。</p>
<p align=center><img height=426 alt="ListNews Servlet 页" src="http://testwww.netbeans.org/images/articles/javaee5/newsapp_listnews_servlet_pg.png" width=642></p>
<p>当您使用 <tt><font size=2>PostMessage</font></tt> Servlet 添加消息时，消息会发送到消息驱动 Bean 以写入到持久性存储中，并会调用 <tt><font size=2>ListNews</font></tt> Servlet 来显示数据库中的消息。由 <tt><font size=2>ListNews</font></tt> 检索的数据库中的消息列表通常不包含新消息，这是因为消息服务是异步的。 </p>
<h2><a name=Exercise_5></a>疑难解答</h2>
<p>下面是您创建项目时可能会遇到的一些问题。</p>
<div class=indent>
<h3 class=tutorial>JMS 资源问题</h3>
<p>使用向导来创建 JMS 资源时，您可能会在输出窗口中看到以下服务器错误消息：</p>
<pre>[com.sun.enterprise.connectors.ConnectorRuntimeException:
JMS resource not created : jms/Queue]
</pre>
<p>此消息可能表明没有创建 JMS 资源，或者没有在应用服务器中注册该资源。您可以使用 Sun Java System Application Server 管理控制台来检查、创建以及编辑 JMS 资源。</p>
<p>要打开管理控制台，请执行以下操作：</p>
<ol>
    <li>在 IDE 的&#8220;运行环境&#8221;中，展开&#8220;服务器&#8221;节点以确认 Sun Java System Application Server 正在运行。"Sun Java System Application Server" 节点旁边的小绿色箭头表示服务器正在运行。
    <li>右键单击 "Sun Java System Application Server" 节点，然后选择&#8220;查看管理控制台&#8221;以在浏览器中打开登录窗口。
    <li>登录到 Sun Java System Application Server。缺省的用户名为 <tt><font size=2>admin</font></tt>，口令为 <tt><font size=2>adminadmin</font></tt>。
    <li>在浏览器的管理控制台中，依次展开左框架中的&#8220;资源&#8221;节点和&#8220;JMS 资源&#8221;节点。
    <li>在左框架中单击&#8220;连接工厂&#8221;和&#8220;目标资源&#8221;链接以检查是否在服务器中注册了这些资源，并在必要时修改这些资源。如果这些资源不存在，您可以在管理控制台中创建这些资源。 </li>
</ol>
<p>您需要确保将 PostMessage Servlet 中的 JMS 连接工厂资源映射到在 Sun Java System Application Server 中注册的 JMS 连接工厂资源的对应 JNDI 名称上。</p>
<p>应在 Sun Java System Application Server 中注册以下资源：</p>
<ul>
    <li>具有 JNDI 名称 <tt><font size=2>jms/NewMessage</font></tt> 和类型 <tt><font size=2>javax.jms.Queue</font></tt> 的目标资源
    <li>具有 JNDI 名称 <tt><font size=2>jms/NewMessageFactory</font></tt> 和类型 <tt><font size=2>javax.jms.QueueConnectionFactory</font></tt> 的连接工厂资源 </li>
</ul>
</div>
<br>
<div class=feedback-box><a href="http://testwww.netbeans.org/about/contact_form.html?to=3&amp;subject=Feedback:%20EJB%203%20Enterprise%20Beans">请将您的意见和建议发送给我们</a></div>
<br style="CLEAR: both"><!-- ======================================================================================= -->
<h2><a name=nextsteps></a>后续步骤</h2>
<p>有关使用 NetBeans IDE 5.5 开发 Java EE 应用程序的更多信息，请参见以下资源： </p>
<ul>
    <li><a href="http://testwww.netbeans.org/kb/55/javaee-intro_zh_CN.html">Java EE 5 技术简介</a>
    <li><a href="http://testwww.netbeans.org/kb/55/persistence_zh_CN.html">Java EE 5 平台中的 Java 持久性</a>
    <li><a href="http://testwww.netbeans.org/kb/55/customer-book_zh_CN.html">比较 Java EE 5 平台和 J2EE 1.4 平台</a>
    <li><a href="http://testwww.netbeans.org/kb/trails/java-ee_zh_CN.html">Java EE 应用程序相关教程</a> </li>
</ul>
<p>您可以在 <a href="http://java.sun.com/javaee/5/docs/tutorial/doc/">Java EE 5 教程</a>中找到有关使用 EJB 3.0 Enterprise Beans 的详细信息。</p>
<p>要发送意见和建议、获得支持以及随时了解 NetBeans IDE Java EE 开发功能的最新开发情况，请<a href="http://testwww.netbeans.org/community/lists/top.html">加入 nbj2ee 邮件列表</a>。</p>
<!-- /Servlet-Specific template --><!-- end of right navigation --><!-- servlets and anything not on www or testwww -->
<img src ="http://www.blogjava.net/lewhwa/aggbug/115781.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lewhwa/" target="_blank">网路冷眼@BlogJava</a> 2007-05-07 23:19 <a href="http://www.blogjava.net/lewhwa/articles/115781.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java EE 5 平台中的 Java 持久性（转载）</title><link>http://www.blogjava.net/lewhwa/articles/115780.html</link><dc:creator>网路冷眼@BlogJava</dc:creator><author>网路冷眼@BlogJava</author><pubDate>Mon, 07 May 2007 15:17:00 GMT</pubDate><guid>http://www.blogjava.net/lewhwa/articles/115780.html</guid><wfw:comment>http://www.blogjava.net/lewhwa/comments/115780.html</wfw:comment><comments>http://www.blogjava.net/lewhwa/articles/115780.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lewhwa/comments/commentRss/115780.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lewhwa/services/trackbacks/115780.html</trackback:ping><description><![CDATA[&nbsp;原文地址：<a href="http://testwww.netbeans.org/kb/55/persistence_zh_CN.html">http://testwww.netbeans.org/kb/55/persistence_zh_CN.html</a>
<h1>&nbsp;</h1>
<h1>Java EE 5 平台中的 Java 持久性</h1>
<p>本文档向您介绍了有关使用 Java&#8482; 持久性开发 Web 应用程序的基础知识。Java 持久性 API 是作为 Java EE 5 平台的一部分引入的。在本教程中，您将创建一个简单的 Web 应用程序项目。我们将在项目中使用持久性，但是 Java 持久性 API 允许我们在无需创建 EJB 模块的情况下使用持久性。</p>
<p>在项目中使用 Java 持久性可以大大简化应用程序的开发，因为您不需要配置部署描述符为持久性字段或属性提供对象关系映射信息，而可以使用标注直接在简单的 Java 类中定义这些属性。</p>
<p>实体持久性是通过 EntityManager API 来管理的。EntityManager API 用于处理持久性上下文，而每个持久性上下文是一组实体实例。在开发应用程序时，您可以在类中使用标注来指定实体实例的持久性上下文实例，然后通过容器处理实体实例的生命周期。</p>
<p>本文档使用的是 <a href="http://www.netbeans.org/products/ide/index_zh_CN.html"><u><font color=#1e2a60>NetBeans IDE 5.5 发行版本</font></u></a>。 </p>
<!--
<div style="border: 1px solid rgb(14, 27, 85); padding: 10px 30px; background-color: rgb(231, 231, 231); margin-left: 60px; margin-right: 40px;">
Please note that this release is a beta release.
For our latest full release, go to the <a href="http://www.netbeans.info/downloads/index.php">NetBeans
Download page</a> and download NetBeans IDE 5.0.</div>
-->
<p align=center><strong>预计持续时间：15 分钟</strong></p>
<div class=indent>
<h3 class=tutorial>先决条件</h3>
<p>本文档假定您已具备以下技术的一些基本知识或编程经验：</p>
<ul>
    <li>Java 编程
    <li>NetBeans IDE </li>
</ul>
<h3 class=tutorial>本教程所需的软件</h3>
<p>在学习本教程之前，您需要在计算机中安装以下软件：</p>
<ul>
    <li><a href="http://www.netbeans.org/products/ide/index_zh_CN.html"><u><font color=#1e2a60>NetBeans IDE 5.5</font></u></a>（<a href="http://www.netbeans.info/downloads/index.php"><u><font color=#1e2a60>下载</font></u></a>） <!--or NetBeans IDE 5.5 daily builds later than July 17, 2006
    (To download a daily build, go to <a href="http://www.netbeans.info/downloads/download.php?a=n&p=1">this page</a>, set 5.5 as the release version and Daily as the build type.-->
    <li>Java Standard Development Kit (JDK) 版本 5.0 或版本 6.0（<a href="http://java.sun.com/javase/downloads/index.jsp"><u><font color=#1e2a60>下载</font></u></a>）
    <li>Sun Java System Application Server Platform Edition 9（<a href="http://www.sun.com/software/products/appsrvr_pe/index.xml"><u><font color=#1e2a60>下载</font></u></a>） </li>
</ul>
<p>在学习本教程之前，您需要在 IDE 中注册 Sun Java System Application Server 的本地实例。</p>
<h3 class=tutorial>教程练习</h3>
<ul><!--
    <li><a href="#Exercise_0">Exercise 0: Install and configure the tutorial environment</a></li>
    -->
    <li><a href="http://testwww.netbeans.org/kb/55/persistence_zh_CN.html#Exercise_1"><u><font color=#1e2a60>建立 Web 应用程序项目</font></u></a>
    <li><a href="http://testwww.netbeans.org/kb/55/persistence_zh_CN.html#Exercise_2"><u><font color=#1e2a60>创建持久性单元</font></u></a>
    <li><a href="http://testwww.netbeans.org/kb/55/persistence_zh_CN.html#Exercise_3"><u><font color=#1e2a60>创建实体类</font></u></a>
    <li><a href="http://testwww.netbeans.org/kb/55/persistence_zh_CN.html#Exercise_4"><u><font color=#1e2a60>创建 Web 接口</font></u></a>
    <li><a href="http://testwww.netbeans.org/kb/55/persistence_zh_CN.html#Exercise_5"><u><font color=#1e2a60>运行项目</font></u></a>
    <li><a href="http://testwww.netbeans.org/kb/55/persistence_zh_CN.html#Exercise_6"><u><font color=#1e2a60>疑难解答</font></u></a> </li>
</ul>
</div>
<br><!-- ===================================================================================== -->
<h2><a name=Exercise_1></a><!--exercise 1: -->建立 Web 应用程序项目</h2>
<p>本练习的目的是创建 ZooApp Web 应用程序项目。此应用程序将利用新增的 Java 持久性模型的功能。其中一项功能就是，实体对象可以成为简单的 Java 类，因此不再需要将它们放置在 EJB 模块中并将其打包到 EAR 文件中。这意味着，我们可以直接在 Web 应用程序内创建实体类。</p>
<!--we will then show how you can run the Java EE 5 web application in a non-EE 5 container by adding the bundled
TopLink Essentials library to you application.
[ configuring the project's classpath to contain the libraries for Java
EE 5 and the Java DB database client.]-->
<div class=indent>
<ol>
    <li>选择&#8220;文件&#8221;&gt;&#8220;新建项目&#8221;(Ctrl-Shift-N)。从 "Web" 类别中选择&#8220;Web 应用程序&#8221;，然后单击&#8220;下一步&#8221;。
    <li>将项目命名为 ZooApp，将服务器设置为 "Sun Java System Application Server"，将 Java EE 版本设置为 "Java EE 5"，然后单击&#8220;下一步&#8221;。
    <li>选中 "JavaServer Faces" 复选框，然后单击&#8220;完成&#8221;。 </li>
</ol>
<h3 class=tutorial>小结</h3>
<p>在本练习中，您创建了一个将包含实体类的 Java EE 5 Web 应用程序。 </p>
</div>
<!-- ===================================================================================== -->
<h2><a name=Exercise_2></a><!--exercise 2: -->创建持久性单元</h2>
<p>在本练习中，我们将创建一个持久性单元，目的在于通知容器哪些实体类需要由实体管理器进行管理，以及这些实体需要使用哪些数据源信息。</p>
<!--
<p>We will define our persistence unit in <tt>persistence.xml</tt>.
In addition to its name, we will also specify the properties of the persistence unit such as the data source
the container will use to manage the entities which reference that persistence unit.</p>
-->
<p>我们通过在 <tt><font size=2>persistence.xml</font></tt>（将在 Web 模块中创建此文件）中定义持久性单元的属性来创建持久性单元。指定持久性单元的名称后，将指定持久性提供程序，它包含容器将用于管理实体实例的 API。此外，还需要指定数据源和表生成策略。我们将使用&#8220;新建持久性单元&#8221;向导创建持久性单元。</p>
<p><strong>注意：</strong>也可以在&#8220;新建 - 实体类&#8221;向导中创建持久性单元。创建实体类时，如果不存在持久性单元，该向导将提示我们进行创建。</p>
<!--the JAR file or directory whose META-INF directory contains <tt>persistence.xml</tt> is called the root of the persistence unit.
The persistence unit's root determines the scope of that persistent unit.
Each persistence unit must be identified with a name that is unique to the scope of that persistence unit.</p>
--><!--
file to define connection
settings to the database. We then create entity classes that represent each
of the tables in the database, and use getter and setter classes in the entity
classes to work with the table data.</p>
-->
<div class=indent>
<h3 class=tutorial>创建持久性单元</h3>
<ol>
    <li>在&#8220;项目&#8221;窗口中右键单击 "ZooApp" Web 应用程序节点，然后选择&#8220;新建&#8221;&gt;&#8220;文件/文件夹&#8221;以打开&#8220;新建文件&#8221;向导。
    <li>从&#8220;持久性&#8221;类别中，选择&#8220;持久性单元&#8221;，然后单击&#8220;下一步&#8221;。
    <li>保留缺省的持久性单元名称。
    <li>对于&#8220;持久性提供程序&#8221;，使用 <tt><font size=2>TopLink(default)</font></tt>。
    <p>缺省提供程序是 <tt><font size=2>TopLink Essential.jar</font></tt>。<tt><font size=2>TopLink Essential.jar</font></tt> 包含了 Java 持久性的库。并且实体管理器位于 <tt><font size=2>TopLink Essential.jar</font></tt> 中。</p>
    <li>对于&#8220;数据源&#8221;，使用缺省的数据源 <tt><font size=2>jdbc/sample</font></tt>。
    <p>缺省数据源 <tt><font size=2>jdbc/sample</font></tt> 用于连接到与 Sun Java System Application Server 捆绑在一起的 Java DB 数据库。</p>
    <li>检查是否为持久性单元选中了&#8220;使用 Java 事务 API&#8221;，以及&#8220;表生成策略&#8221;是否设置为&#8220;创建&#8221;，以便在部署应用程序时创建基于实体类的表。
    <li>单击&#8220;完成&#8221;。 </li>
</ol>
<p align=center><img height=309 alt=&#8220;新建持久性单元&#8221;向导 src="http://testwww.netbeans.org/images/articles/javaee5/zooapp_persistence_unit_wizard_zh_CN.png" width=583></p>
<p>单击&#8220;完成&#8221;后，IDE 将创建 <tt><font size=2>persistence.xml</font></tt>，并在源代码编辑器中将其打开。通过单击源代码编辑器工具栏中的 "XML"，可以查看 <tt><font size=2>persistence.xml</font></tt> 的 XML。此文件包含了 Java EE 5 容器管理应用程序的实体和持久性所需的所有信息。</p>
<h3 class=tutorial>小结</h3>
<p>在本练习中，您创建了一个持久性单元，用来指定数据源、要永久保存的实体类以及容器将用于管理实体生命周期的实体管理器。</p>
</div>
<h2><a name=Exercise_3></a><!--exercise 2: -->创建实体类</h2>
<p>在本练习中，我们将创建两个实体类，即 <tt><font size=2>Animal.java</font></tt> 和 <tt><font size=2>Pavilion.java</font></tt>，它们用于表示要创建的关系数据库中的表。此外，还将在类中定义一些字段来表示数据。Java 持久性规范允许我们使用标注为容器提供有关字段的信息，如对象关系映射信息。 <!--when we run our application, the database tables will be created based on these entity classes.--></p>
<div class=indent>
<h3 class=tutorial>创建 <tt><font size=3>Animal</font></tt> 实体类</h3>
<p>首先，我们将创建实体类 <tt><font size=2>Animal</font></tt>。此类用于表示数据库中的 <tt><font size=2>ANIMAL</font></tt> 表。在创建实体类时，IDE 会添加 <tt><font size=2>@Entity</font></tt> 标注以将该类定义为实体类。创建类后，我们将在类中创建字段以表示表中所需的数据，并使用标注提供有关一些字段的其他信息。</p>
<p>每个实体类都必须具有一个主键。在创建实体类时，IDE 会添加 <tt><font size=2>@Id</font></tt> 标注以声明要用作主键的字段。此外，IDE 还会添加 <tt><font size=2>@Generated</font></tt> 标注以指定主 Id 的键生成策略。</p>
<p>要创建 Animal 类，请执行以下操作：</p>
<ol>
    <li>右键单击 "ZooApp" 项目节点，然后选择&#8220;新建&#8221;&gt;&#8220;文件/文件夹&#8221;。
    <li>从&#8220;持久性&#8221;类别中，选择&#8220;实体类&#8221;，然后单击&#8220;下一步&#8221;。
    <li>键入 <tt><font size=2>Animal</font></tt> 作为类名，键入 <tt><font size=2>entity</font></tt> 作为包名，并将&#8220;主键类型&#8221;保留为 <tt><font size=2>Long</font></tt>。单击&#8220;完成&#8221;。 </li>
</ol>
<p>单击&#8220;完成&#8221;后，将在源代码编辑器中打开新建的实体类 <tt><font size=2>Animal.java</font></tt>。在源代码编辑器中，请执行以下操作：</p>
<ol>
    <li>将以下字段声明添加到类中：
    <pre class=examplecode>  String name;
    String kind;
    String weight;
    Pavilion pavilion;
    </pre>
    <li>在源代码编辑器中单击鼠标右键，然后选择&#8220;重构&#8221;&gt;&#8220;封装字段&#8221;以便为每个字段生成 getter 和 setter。在&#8220;封装字段&#8221;对话框中，确保选中所有字段的 getter 和 setter 复选框。
    <li>在&#8220;封装字段&#8221;对话框中单击&#8220;下一步&#8221;，然后在&#8220;输出&#8221;窗口的&#8220;重构&#8221;标签中单击&#8220;执行重构&#8221;。
    <li>现在我们希望更改将在 <tt><font size=2>Animal</font></tt> 表中创建的其中一列的名称。我们希望该列的名称是 <tt><font size=2>animalName</font></tt> 而不是 <tt><font size=2>name</font></tt>。通过在 <tt><font size=2>name</font></tt> 字段声明之上添加以下标注，可以使用标注来指定生成列的名称：
    <pre class=examplecode>    @Column(name="animalName")</pre>
    <li>我们还希望 <tt><font size=2>Animal</font></tt> 表中的 <tt><font size=2>pavilion</font></tt> 列具有多对一的关系。通过在 <tt><font size=2>pavilion</font></tt> 声明之上添加以下标注，可以实现这一目的：
    <pre class=examplecode>    @ManyToOne</pre>
    <li>按 Alt-Shift-F 组合键为类生成所有必要的 import 语句。
    <li>保存您所做的更改。 </li>
</ol>
<p>在下一步中，我们将创建 <tt><font size=2>Pavilion</font></tt> 实体类。</p>
<h3 class=tutorial>创建 <tt><font size=3>Pavilion</font></tt> 实体类</h3>
<p>现在我们将创建实体类 <tt><font size=2>Pavilion</font></tt>，它用于表示数据库中的 <tt><font size=2>PAVILION</font></tt> 表。我们将再次在类中使用标注指定一些字段的对象关系映射。要创建 <tt><font size=2>Pavilion</font></tt> 类，请执行以下操作：</p>
<ol>
    <li>右键单击 "ZooApp" 项目节点，然后选择&#8220;新建&#8221;&gt;&#8220;文件/文件夹&#8221;。
    <li>从&#8220;持久性&#8221;类别中，选择&#8220;实体类&#8221;，然后单击&#8220;下一步&#8221;。
    <li>键入 <tt><font size=2>Pavilion</font></tt> 作为类名，键入 <tt><font size=2>entity</font></tt> 作为包名，并将&#8220;主键类型&#8221;保留为 <tt><font size=2>Long</font></tt>。单击&#8220;完成&#8221;。 </li>
</ol>
<p>单击&#8220;完成&#8221;后，将在源代码编辑器中打开新建的实体类 <tt><font size=2>Pavilion.java</font></tt>。在源代码编辑器中，请执行以下操作：</p>
<ol>
    <li>将以下字段声明添加到类中：
    <pre class=examplecode>  String name;
    String address;
    Collection &lt;Animal&gt; animals;</pre>
    <li>选择&#8220;重构&#8221;&gt;&#8220;封装字段&#8221;以便为字段生成 getter 和 setter。
    <li>在 <tt><font size=2>name</font></tt> 声明之上添加以下标注以更改生成列的名称：
    <pre class=examplecode>    @Column(name="pavilionName")</pre>
    <li>在 <tt><font size=2>animals</font></tt> 声明之上添加以下标注，为实体指定一对多的关系：
    <pre class=examplecode>    @OneToMany(mappedBy="pavilion")</pre>
    <li>按 Alt-Shift-F 组合键生成所有缺少的 import 语句。
    <li>保存您所做的更改。 </li>
</ol>
<h3 class=tutorial>小结</h3>
<p>在本练习中，您创建了两个实体类并定义了字段，而且还使用标注定义了在部署应用程序时将在表中生成的某些列的属性。</p>
</div>
<a name=Exercise_4></a>
<h2>创建 Web 接口</h2>
<p>现在我们希望创建一些简单的 Web 页，以查看是否创建了数据库表以及是否可以添加数据。我们将为应用程序添加 Java Server Faces (JSF) 页，并使用&#8220;通过实体类创建 JSF 页&#8221;向导快速创建一个简单的 Web 接口。</p>
<ol>
    <li>从主菜单中，选择&#8220;文件&#8221;&gt;&#8220;新建文件&#8221;。从&#8220;持久性&#8221;类别中，选择&#8220;通过实体类创建 JSF 页&#8221;，然后单击&#8220;下一步&#8221;。
    <li>在&#8220;新建 - 通过实体类创建 JSF 页&#8221;向导中，单击&#8220;全部添加&#8221;以选择两个实体类，然后单击&#8220;下一步&#8221;。
    <li>将&#8220;JSF 页文件夹&#8221;文本字段保留为空，以便在缺省位置保存 JSF 文件。
    <li>将 <tt><font size=2>entity</font></tt> 指定为已生成类的包，然后单击&#8220;完成&#8221;。 </li>
</ol>
<p align=center><img height=408 alt="&#8220;通过实体类创建 JSF 页&#8221;向导" src="http://testwww.netbeans.org/images/articles/javaee5/zooapp_jsf_from_entity_wizard_zh_CN.png" width=627>
<p>单击&#8220;完成&#8221;后，IDE 将生成所需的 JavaServer Faces 文件，以便我们可以运行并测试 ZooApp。</p>
<!-- ======================================================================================= -->
<h2><a name=Exercise_5></a>运行项目</h2>
<p>在本练习中，我们将部署 ZooApp Web 应用程序项目并测试该应用程序。</p>
<ol>
    <li>启动 Java DB 数据库，方法是：选择&#8220;工具&#8221;&gt;&#8220;Java DB 数据库&#8221;&gt;&#8220;启动 Java DB 服务器&#8221;。
    <li>右键单击 "ZooApp" 项目节点，然后选择&#8220;运行项目&#8221;。 </li>
</ol>
<p>单击&#8220;运行项目&#8221;后，将在浏览器中打开一个页面，您可以通过该页面包含的菜单，查看场馆 (Pavilions) 和动物 (Animals) 的列表。</p>
<p align=center><img height=384 alt="ZooApp 主页" src="http://testwww.netbeans.org/images/articles/javaee5/zooapp_final_jsp_page.png" width=581>
<p>您还可以添加、编辑或删除动物 (Animals) 和场馆 (Pavilions) 数据。</p>
<p align=center><img height=445 alt="ZooApp 的添加动物 (Animal) 页" src="http://testwww.netbeans.org/images/articles/javaee5/zooapp_newanimal_jsp_page.png" width=581>
<h3 class=tutorial>小结</h3>
<p>在本练习中，您生成了为 Java EE 5 平台开发的 ZooApp Web 应用程序，然后部署并测试了该 Web 应用程序。</p>
<h2><a name=Exercise_6></a>疑难解答</h2>
<p>下面是您创建项目时可能会遇到的一些问题。</p>
<div class=indent>
<h3 class=tutorial>使用&#8220;新建 - 通过实体类创建 JSF 页&#8221;向导时的问题</h3>
<p>使用该向导通过实体类创建 JSF 页时，您可能会在向导中看到以下错误消息：</p>
<p><span style="FONT-WEIGHT: bold; COLOR: red">"This wizard can only be used in a web project with JSF support."（此向导只能在具有 JSF 支持的 Web 项目中使用。）</span></p>
<p>如果看到此消息，则需要检查是否已将 Java Server Faces 框架添加到项目属性中。通过执行以下操作，可以将 Java Server Faces 支持添加到 Web 项目中：</p>
<ol>
    <li>在&#8220;项目&#8221;窗口中，右键单击 Web 应用程序节点，然后选择&#8220;属性&#8221;。
    <li>在&#8220;项目属性&#8221;对话框的&#8220;类别&#8221;窗格中选择&#8220;框架&#8221;，然后单击&#8220;添加&#8221;。
    <li>在&#8220;选择框架&#8221;对话框中，选择 "Java Server Faces"，然后单击&#8220;确定&#8221;。
    <li>在&#8220;项目属性&#8221;对话框中，单击&#8220;确定&#8221;以关闭窗口。 </li>
</ol>
<p>将 JSF 框架添加到项目属性后，您应该能够使用该向导创建 JSF 页了。</p>
<!--
<h3 class="tutorial">Validation Error when Running the Project</h3>
<p>If you get a validation error when trying to add a new animal, add the following
methods to <tt>Pavilion.java</tt> to validate the data.
You can add the methods after the <tt>setAnimals</tt> method at the bottom of the class.</p>
<pre class="examplecode">  //Code for validating information when creating new item
public int hashCode() {
int hash = 0;
hash += (this.id != null ? this.id.hashCode() : 0);
return hash;
}
public boolean equals(Object object) {
if (object == null || !this.getClass().equals(object.getClass())) {
return false;
}
Pavilion other = (Pavilion)object;
if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) return false;
return true;
}
</pre>
<p>After adding the methods to the class, build, deploy and run the project again.</p>
--></div>
<br>
<div class=feedback-box><a href="http://testwww.netbeans.org/about/contact_form.html?to=3&amp;subject=Feedback:%20Java%20Persistence%20in%20Java%20EE%205%20Platform"><u><font color=#1e2a60>请将您的意见和建议发送给我们</font></u></a></div>
<br style="CLEAR: both"><!-- ======================================================================================= -->
<h2><a name=nextsteps></a>后续步骤</h2>
<p>有关使用 NetBeans IDE 5.5 开发 Java EE 应用程序的更多信息，请参见以下资源： </p>
<ul>
    <li><a href="http://testwww.netbeans.org/kb/55/javaee-intro_zh_CN.html"><u><font color=#1e2a60>Java EE 5 技术简介</font></u></a>
    <li><a href="http://testwww.netbeans.org/kb/55/ejb30_zh_CN.html"><u><font color=#1e2a60>EJB 3.0 Enterprise Beans</font></u></a>
    <li><a href="http://testwww.netbeans.org/kb/55/customer-book_zh_CN.html"><u><font color=#1e2a60>比较 Java EE 5 平台和 J2EE 1.4 平台</font></u></a>
    <li><a href="http://testwww.netbeans.org/kb/trails/java-ee_zh_CN.html"><u><font color=#1e2a60>Java EE 应用程序相关教程</font></u></a> </li>
</ul>
<p>您可以在 <a href="http://java.sun.com/javaee/5/docs/tutorial/doc/"><u><font color=#1e2a60>Java EE 5 教程</font></u></a>中找到有关使用 Java 持久性的详细信息。</p>
<p>要发送意见和建议、获得支持以及随时了解 NetBeans IDE Java EE 开发功能的最新开发情况，请<a href="http://testwww.netbeans.org/community/lists/top.html"><u><font color=#1e2a60>加入 nbj2ee 邮件列表</font></u></a>。</p>
<!-- /Servlet-Specific template --><!-- end of right navigation --><!-- servlets and anything not on www or testwww -->
<img src ="http://www.blogjava.net/lewhwa/aggbug/115780.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lewhwa/" target="_blank">网路冷眼@BlogJava</a> 2007-05-07 23:17 <a href="http://www.blogjava.net/lewhwa/articles/115780.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java EE 5 技术简介(转载）</title><link>http://www.blogjava.net/lewhwa/articles/115779.html</link><dc:creator>网路冷眼@BlogJava</dc:creator><author>网路冷眼@BlogJava</author><pubDate>Mon, 07 May 2007 15:15:00 GMT</pubDate><guid>http://www.blogjava.net/lewhwa/articles/115779.html</guid><wfw:comment>http://www.blogjava.net/lewhwa/comments/115779.html</wfw:comment><comments>http://www.blogjava.net/lewhwa/articles/115779.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lewhwa/comments/commentRss/115779.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lewhwa/services/trackbacks/115779.html</trackback:ping><description><![CDATA[原文地址：<a href="http://testwww.netbeans.org/kb/55/javaee-intro_zh_CN.html">http://testwww.netbeans.org/kb/55/javaee-intro_zh_CN.html</a>&nbsp;
<h1>&nbsp;</h1>
<h1>Java EE 5 技术简介</h1>
<table class=tablebg width=250 align=right>
    <tbody>
        <tr>
            <td class=tblheader>
            <div align=left>Java EE 5 教程</div>
            </td>
        </tr>
        <tr>
            <td class=tbltd0>
            <ul>
                <li><a href="http://testwww.netbeans.org/kb/55/websvc-jax-ws_zh_CN.html"><u><font color=#1e2a60>Java EE 5 中的 Web 服务</font></u></a>
                <li><a href="http://testwww.netbeans.org/kb/55/persistence_zh_CN.html"><u><font color=#1e2a60>Java EE 5 平台中的 Java 持久性</font></u></a>
                <li><a href="http://testwww.netbeans.org/kb/55/ejb30_zh_CN.html"><u><font color=#1e2a60>EJB 3.0 Enterprise Beans</font></u></a>
                <li><a href="http://testwww.netbeans.org/kb/55/customer-book_zh_CN.html"><u><font color=#1e2a60>比较 Java EE 5 平台和 J2EE 1.4 平台 </font></u></a></li>
            </ul>
            </td>
        </tr>
    </tbody>
</table>
<p>NetBeans IDE 5.5 的开发是在 Java EE 和 Glassfish 工作组的密切合作下完成的，它实现了与新的 Java EE 5 规范的完美集成，从而可以在此 IDE 中轻松地使用新规范。NetBeans IDE 5.5 是您快速学习 Java EE 5 编程并使用它高效工作的最佳途径。</p>
<p>本文档介绍了 Java EE 5 规范的一些重要概念，以及它们如何与您目前的编程项目相关联的。下面将介绍以下主题：</p>
<ul>
    <li><a href="http://testwww.netbeans.org/kb/55/javaee-intro_zh_CN.html#annotations"><u><font color=#1e2a60>标注取代部署描述符</font></u></a>
    <li><a href="http://testwww.netbeans.org/kb/55/javaee-intro_zh_CN.html#ejb"><u><font color=#1e2a60>简化的 EJB 软件开发</font></u></a>
    <li><a href="http://testwww.netbeans.org/kb/55/javaee-intro_zh_CN.html#injection"><u><font color=#1e2a60>使用依赖关系注入来访问资源</font></u></a>
    <li><a href="http://testwww.netbeans.org/kb/55/javaee-intro_zh_CN.html#persistence"><font color=#1e2a60><u>Java 持久性 API 模型 </u></font>
    <h2></h2>
    </a>
    <li><a href="http://testwww.netbeans.org/kb/55/javaee-intro_zh_CN.html#webservice"><u><font color=#1e2a60>Web 服务</font></u></a> </li>
</ul>
<p>您可以在 <a href="http://java.sun.com/javaee/5/docs/tutorial/doc/"><u><font color=#1e2a60>Java EE 5 教程</font></u></a>中找到有关开发 Java EE 5 应用程序并在 Sun Java System Application Server Platform Edition 9 上部署该应用程序的信息。</p>
<a name=annotations>
<h2>标注取代部署描述符</h2>
<p>Java EE 5 平台不需要任何部署描述符（Servlet 规范所需的部署描述符 <tt><font size=2>web.xml</font></tt> 文件除外），从而简化了部署过程。其他部署描述符（如 <tt><font size=2>ejb-jar.xml</font></tt> 以及在 <tt><font size=2>web.xml</font></tt> 中与 Web 服务相关的条目）已过时。J2EE 1.4 部署描述符通常很复杂，在填充它们时很容易出错。但是，Java EE 5 平台使用的是&#8220;标注&#8221;。标注是 Java 修饰符，与代码中指定的 <tt><font size=2>public</font></tt> 和 <tt><font size=2>private</font></tt> 类似。例如，EJB 3 规范（Java EE 5 规范的子集）为 Bean 类型、接口类型、资源引用、事务属性、安全性等定义了标注。JAX-WS 2.0 规范为 Web 服务提供了一组类似的标注。有些标注用来生成工件，另外一些标注用来描述代码，还有一些标注用来提供增强的服务，如安全性或特定于运行时的逻辑。总之，Java EE 5 平台为以下任务（以及其他任务）提供了标注：
<ul>
    <li>定义和使用 Web 服务
    <li>开发 EJB 软件应用程序
    <li>将 Java 技术类映射到 XML
    <li>将 Java 技术类映射到数据库
    <li>将方法映射到操作
    <li>指定外部依赖关系
    <li>指定部署信息，其中包括安全属性 </li>
</ul>
<p>标注使用 <tt><font size=2>@</font></tt> 字符来标记。在 IDE 中，当您创建了使用 Java EE 5 中的标注的类型时，将在生成的代码中提供相关的占位符。例如，当您使用 IDE 创建无态会话 Bean 时，将生成以下代码，其中包括 <tt><font size=2>@Stateless()</font></tt> 标注：
<p>&#160;</p>
<pre class=examplecode>package mypackage;
import javax.ejb.*;
@Stateless()
public class HelloWorldSessionBean implements mypackage.HelloWorldSessionLocal {
}</pre>
通过代码完成，您可以对特定于光标下的项的标注属性进行访问。例如，在 <tt><font size=2>@WebService()</font></tt> 标注的括号中按 Ctrl-空格键时，将看到以下内容：
<p align=center><img alt="Web 服务中的标注" src="http://testwww.netbeans.org/images/articles/javaee5/jax-ws-annotate.png"></p>
<p>每个属性都具有缺省值。因此，除非您需要使用缺省值以外的其他值，否则不必指定任何属性。在一些简单的示例中，使用缺省值就可以了，这意味着您根本不需要提供属性。 </p>
<a name=ejb>
<h2>简化的 EJB 软件开发</h2>
<p>使用新的 EJB 3.0 API，可以减少并简化开发者需要完成的工作量，从而可以更轻松地进行软件开发。换句话说，就是使用了更少的类和代码。这是因为现在容器承担了更多的工作，从而实现了这一目的。下面是新 EJB 3.0 API 的一些功能和优点：</p>
<ul>
    <li><strong>只需很少的类和接口。</strong>您不再需要 EJB 组件的 Home 接口和对象接口，因为现在容器负责公开必要的方法。您只需提供业务接口。您可以使用标注来声明 EJB 组件，并且通过容器来管理事务。
    <li><strong>不再需要部署描述符。</strong>您可以在类中直接使用标注，为容器提供以前在部署描述符中定义的依赖关系和配置信息。如果没有任何特殊说明，容器将使用缺省规则来处理最常见的情况。
    <li><strong>查找简单。</strong>您可以通过 <code><font size=2>EJBContext</font></code> 直接在类中查找 JNDI 名称空间中的对象。
    <li><strong>简化了对象关系映射。</strong>新的 Java 持久性 API 允许您使用 POJO 中的标注将 Java 对象映射到关系数据库，从而使对象关系映射变得更简单透明。 </li>
</ul>
<p>在 IDE 中，您可以对 Enterprise Beans 进行编码，就像对其他 Java 类进行编码一样，方法是：使用代码完成和编辑器提示实现正确的方法并使类与其接口保持同步。您不必使用特殊命令和对话框生成诸如业务方法或 Web 服务操作之类的内容，虽然这些命令仍可以帮助您熟悉 Java EE 5 代码的语法。</p>
<a name=injection>
<h2>使用依赖关系注入来访问资源</h2>
通过<strong>依赖关系注入</strong>，对象可以使用标注直接请求外部资源。这样代码会更简洁，因为您不再需要将代码与资源创建和查找代码混杂在一起。您可以在 EJB 组件、Web 容器和客户端中使用资源注入。
<p>&#160;</p>
<p>要请求资源注入，组件需要使用 <code><font size=2>@Resource</font></code> 标注或针对一些专用资源的 <code><font size=2>@EJB</font></code> 和 <code><font size=2>@WebServiceRef</font></code> 标注。您可以注入以下资源： </p>
<ul>
    <li><code><font size=2>SessionContext</font></code> 对象
    <li><code><font size=2>DataSources</font></code> 对象
    <li><code><font size=2>EntityManager</font></code> 接口
    <li>其他 Enterprise Beans
    <li>Web 服务
    <li>消息队列和主题
    <li>资源适配器的连接工厂 </li>
</ul>
<p>在 IDE 中，源代码编辑器为 Java EE 5 平台提供的资源注入标注提供了完整的代码完成。此外，当您运行诸如&#8220;调用 EJB&#8221;和&#8220;使用数据库&#8221;之类的命令时，IDE 会自动将资源注入到文件中。 </p>
<a name=persistence>
<h2>Java 持久性 API 模型</h2>
<p>Java EE 5 平台引入了新的 Java 持久性 API（它是作为 <a href="http://jcp.org/en/jsr/detail?id=220" target=_blank><u><font color=#1e2a60>JSR-220</font></u></a> 的一部分开发的）。Java 持久性 API 不但可以在 EJB 组件外部使用（例如，在 Web 应用程序和应用程序客户端中使用），而且还可以在 Java EE 平台之外的 Java SE 应用程序中使用。</p>
<p>Java 持久性 API 具有以下主要功能：</p>
<ul>
    <li><strong>实体是 POJO。</strong>与使用容器管理持久性 (Container-Managed Persistence, CMP) 的 EJB 组件不同，使用新 API 的实体对象不再是组件，并且它们不再需要位于 EJB 模块中。
    <li><strong>标准化的对象关系映射。</strong>新规范将对对象关系映射的处理方式进行标准化，从而使开发者不再需要了解特定于供应商的策略。Java 持久性 API 使用标注来指定对象关系映射信息，但它仍支持 XML 描述符。
    <li><strong>命名查询。</strong>现在命名查询是用元数据表示的静态查询。查询可以是 Java 持久性 API 查询或本地查询。这样会使重用查询变得非常简单。
    <li><strong>简单的打包规则。</strong>由于实体 Bean 是简单的 Java 技术类，因此几乎可以在 Java EE 应用程序中的任意位置将其打包。例如，实体 Bean 可以是 EJB <code><font size=2>JAR</font></code>、应用程序客户端 <code><font size=2>JAR</font></code>、<code><font size=2>WEB-INF/lib</font></code>、<code><font size=2>WEB-INF/classes</font></code> 的一部分，甚至是企业应用程序归档 (Enterprise Application Archive, EAR) 文件中实用程序 <code><font size=2>JAR</font></code> 的一部分。通过这些简单的打包规则，您不再需要创建 EAR 文件以使用来自 Web 应用程序或应用程序客户端的实体 Bean。
    <li><strong>分离的实体。</strong>由于实体 Bean 是 POJO，因此可以对它们执行序列化，通过网络将其发送到其他地址空间，并在不识别持久性的环境中使用它们。这样，您就不再需要使用数据传输对象 (Data Transfer Object, DTO)。
    <li><strong>EntityManager API。</strong>现在，应用程序编程人员可以使用标准 EntityManager API 来执行涉及实体的<code><font size=2>创建、读取、更新和删除</font></code> (Create Read Update Delete, CRUD) 操作。 </li>
</ul>
<p>IDE 提供了处理新 Java 持久性 API 的工具。您可以通过数据库自动生成实体类，或手动对实体类进行编码。IDE 还提供了用于创建和维护持久性单元的模板和图形编辑器。有关使用新 Java 持久性 API 的详细信息，请参见 <a href="http://testwww.netbeans.org/kb/55/persistence_zh_CN.html"><u><font color=#1e2a60>Java EE 5 平台中的 Java 持久性</font></u></a>。</p>
<a name=webservice>
<h2>Web 服务</h2>
<p>在 Java EE 5 平台中，通过使用标注显著改进和简化了 Web 服务支持。以下规范已应用于此领域中：JSR 224，Java API for XML-Based Web Services (JAX-WS) 2.0；JSR 222，Java Architecture for XML Binding (JAXB) 2.0 以及 JSR 181，Web Services Metadata for the Java Platform。 </p>
<div class=indent>
<h3 class=tutorial>JAX-WS 2.0</h3>
<p>JAX-WS 2.0 是 Java EE 5 平台中用于 Web 服务的新 API。作为 JAX-RPC 1.1 的后继者，JAX-WS 2.0 保留了自然的 RPC 编程模型，同时在以下几个方面进行了改进：数据绑定、协议和传输的独立性、对 Web 服务的 <code><font size=2>REST</font></code> 样式的支持以及易开发性。 </p>
<p>与 JAX-RPC 1.1 的主要区别在于，现在所有数据绑定都委托给了 JAXB 2.0。这样，基于 JAX-WS 的 Web 服务就可以使用百分之百的 XML 架构，从而提高了互操作性和易使用性。这两种技术已很好地集成在一起了，因此用户不必再为使用两套不同的工具而勉为其难了。从 Java 技术类开始开发时，JAXB 2.0 可以生成自动嵌入到 Web 服务描述语言 (Web Service Description Language, WSDL) 文档内的 XML 架构文档，从而用户无需手动执行这一容易出错的集成。 </p>
<p>现成的 JAX-WS 2.0 本身可以支持 SOAP 1.1、SOAP 1.2 和 XML/HTTP 协议。协议可扩展性从一开始就是人们所希望实现的目标，JAX-WS 2.0 允许供应商支持其他协议和编码来获取更佳的性能（例如 FAST Infoset）或专用的应用程序。Web 服务（使用附件来优化大型二进制数据的发送和接收）可以利用 W3C 制订的 MTOM/XOP（&#8220;消息传输优化机制/XML 二进制优化打包&#8221;的简称）标准，且对编程模型没有任何不利的影响（有关 MTOM/XOP 的信息，请参见<a href="http://www.w3.org/TR/2005/REC-soap12-mtom-20050125/" target=_blank><u><font color=#1e2a60>此页</font></u></a>）。在 Java EE 5 技术出现之前，定义 Web 服务需要使用很长且复杂的描述符。现在，定义它却非常简单，只需将 <code><font size=2>@WebService</font></code> 标注放置在 Java 技术类上即可。类上的所有公共方法会自动发布为 Web 服务操作，并且其所有参数都将使用 JAXB 2.0 映射到 XML 架构数据类型。 </p>
<h3 class=tutorial>异步 Web 服务</h3>
<p>由于 Web 服务调用是通过网络来实现的，因此这种调用所花费的时间是无法预测的。许多客户端（特别是交互式客户端，如基于 JFC/Swing 的桌面应用程序）由于必须等待服务器的响应而严重地影响了它们的性能。为了避免出现性能降低，JAX-WS 2.0 提供了新的异步客户端 API。使用此 API，应用程序编程人员不再需要自己创建线程，而是依赖于 JAX-WS 运行时为他们管理长时间运行的远程调用。 </p>
<p>异步方法可以与 WSDL 生成的任何接口以及动态性更强的 <code><font size=2>Dispatch</font></code> API 联合使用。为了方便起见，在导入 WSDL 文档时，您可以要求为 Web 服务定义的任何操作生成异步方法。 </p>
<p>以下提供了两种使用模型：</p>
<ul>
    <li>在轮询模型中，发出调用。准备就绪后，请求结果。<br>
    <li>在回调模型中，注册处理程序。在响应到达后，立即向您发出通知。 </li>
</ul>
<p>请注意，异步调用支持是完全在客户端上实现的，因此不需要对目标 Web 服务进行任何更改。</p>
<p>IDE 提供了处理 JAX-WS 的工具。您可以使用&#8220;新建文件&#8221;向导中的模板生成 JAX-WS 工件。异步 Web 服务可以通过 Web 服务定制编辑器来创建。代码完成功能包括可以在 Web 服务中使用的标注。 </p>
</div>
<br>
<div class=feedback-box><a href="http://testwww.netbeans.org/about/contact_form.html?to=3&amp;subject=Feedback:%20Intro%20to%20Java%20EE%205%20Technology"><u><font color=#1e2a60>请将您的意见和建议发送给我们</font></u></a></div>
<br style="CLEAR: both"><!-- ======================================================================================= -->
<h2><a name=nextsteps></a>后续步骤</h2>
<p>有关使用 NetBeans IDE 5.5 开发 Java EE 应用程序的更多信息，请参见以下资源： </p>
<ul>
    <li><a href="http://testwww.netbeans.org/kb/55/websvc-jax-ws_zh_CN.html"><u><font color=#1e2a60>Java EE 5 中的 Web 服务 (JAX-WS)</font></u></a>
    <li><a href="http://testwww.netbeans.org/kb/55/ejb30_zh_CN.html"><u><font color=#1e2a60>EJB 3.0 Enterprise Beans</font></u></a>
    <li><a href="http://testwww.netbeans.org/kb/55/customer-book_zh_CN.html"><u><font color=#1e2a60>比较 Java EE 5 平台和 J2EE 1.4 平台</font></u></a>
    <li><a href="http://testwww.netbeans.org/kb/trails/java-ee_zh_CN.html"><u><font color=#1e2a60>Java EE 应用程序相关教程</font></u></a> </li>
</ul>
<p>要发送意见和建议、获得支持以及随时了解 NetBeans IDE Java EE 开发功能的最新开发情况，请<a href="http://testwww.netbeans.org/community/lists/top.html"><u><font color=#1e2a60>加入 nbj2ee@netbeans.org 邮件列表</font></u></a>。</p>
<!-- /Servlet-Specific template --><!-- end of right navigation --><!-- servlets and anything not on www or testwww -->
<img src ="http://www.blogjava.net/lewhwa/aggbug/115779.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lewhwa/" target="_blank">网路冷眼@BlogJava</a> 2007-05-07 23:15 <a href="http://www.blogjava.net/lewhwa/articles/115779.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java EE SDK Update 2 提供下载</title><link>http://www.blogjava.net/lewhwa/articles/88884.html</link><dc:creator>网路冷眼@BlogJava</dc:creator><author>网路冷眼@BlogJava</author><pubDate>Thu, 21 Dec 2006 14:13:00 GMT</pubDate><guid>http://www.blogjava.net/lewhwa/articles/88884.html</guid><wfw:comment>http://www.blogjava.net/lewhwa/comments/88884.html</wfw:comment><comments>http://www.blogjava.net/lewhwa/articles/88884.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lewhwa/comments/commentRss/88884.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lewhwa/services/trackbacks/88884.html</trackback:ping><description><![CDATA[Glassfish 项目组宣布 Java EE SDK Update 2 发布了.<br /><br />Glassfish 项目组在今年5月发布了Java EE 5的SDK. Java EE 5的关注重点是简化应用开发，尤其是大量采用元数据标注（annotation）和POJO驱动的开发方式，对平台进行了重新定义。对比此前的J2EE 1.4规范，Java EE 5最重要的新增特性包括Java持久化API（即EJB 3 entity bean）、JSF、JSTL等。<br /><br />这次发布的Java EE SDK Update 2, 包括的更新有:<br /><br /><font color="#000000"><span style="color: blue;">与Java SE 6测试兼容；<br />SDK整合了Access Manager , 7.1 Beta版本；<br />SDK整合了来自Open ESB 项目的 JBI Runtime；<br />SDK还整合了一个测试版本的Portlet Container, 开发者可以测试JSR 168 portlets</span><br /><br /></font>同时还提供了更新的Java EE samples, Api docs等等.<br /><br />官方博客:<br /><a href="http://blogs.sun.com/theaquarium/entry/java_ee_sdk_update_2" target="_new"><font color="#002c99">http://blogs.sun.com/theaquarium/entry/java_ee_sdk_update_2</font></a><br /><img src ="http://www.blogjava.net/lewhwa/aggbug/88884.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lewhwa/" target="_blank">网路冷眼@BlogJava</a> 2006-12-21 22:13 <a href="http://www.blogjava.net/lewhwa/articles/88884.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[原创]利用Google Web Toolkit(GWT) SDK 开发AJAX应用程序(1)</title><link>http://www.blogjava.net/lewhwa/articles/81611.html</link><dc:creator>网路冷眼@BlogJava</dc:creator><author>网路冷眼@BlogJava</author><pubDate>Wed, 15 Nov 2006 23:25:00 GMT</pubDate><guid>http://www.blogjava.net/lewhwa/articles/81611.html</guid><wfw:comment>http://www.blogjava.net/lewhwa/comments/81611.html</wfw:comment><comments>http://www.blogjava.net/lewhwa/articles/81611.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lewhwa/comments/commentRss/81611.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lewhwa/services/trackbacks/81611.html</trackback:ping><description><![CDATA[Google的GMail是一个Web 2.0 AJAX的典型示例。Google Web Toolkit(GWT) SDK 提供一个Java框架，采用开发和调试一般Java应用程序的方式来编写AJAX的软件。简便而快捷。面对炙手可热的Web 2.0,咱们可以说，I can do it!<img src="http://blog.csdn.net/wwwghost/aggbug/1386866.aspx" height="1" width="1" /><img src ="http://www.blogjava.net/lewhwa/aggbug/81611.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lewhwa/" target="_blank">网路冷眼@BlogJava</a> 2006-11-16 07:25 <a href="http://www.blogjava.net/lewhwa/articles/81611.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>