Raible's Wiki

Raible Designs
Wiki Home
News
Recent Changes

AppFuse

Homepage
  - Korean
  - Chinese
  - Italian
  - Japanese

QuickStart Guide
  - Chinese
  - French
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish
  - Japanese

User Guide
  - Korean
  - Chinese

Tutorials
  - Chinese
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish

FAQ
  - Korean

Latest Downloads

Other Applications

Struts Resume
Security Example
Struts Menu

Set your name in
UserPreferences

Edit this page


Referenced by
AppFuseXSnapshot
AppFuseXSnapshotRepl...




JSPWiki v2.2.33

[RSS]


Hide Menu

AppFuseXSnapshotBase


DTO in AppFuse using XSnapshot

Part II: Implementing the example - Create Model, DAO, Manager, Actions for the example
This tutorial depends on Part I: Integrating XSnapshot in AppFuse.

Table of content

  • [1] introduction : presenting the example
  • [2] the model
  • [3] the webapp

Introduction : presenting the example[#1]

In this example, we wiil create a very simple project manager. We can list and edit projects which contain a leader and some participants.

Here there is a class diagram for this example.

NOTE:by my little xsnapshot experience, it's recommened to use only Classes type, even for primary type (int, long...)
classesDiagram.png

I am assuming that you know AppFuse, so you know how to generate and configure DAOs, Managers, Actions and JSPs for the classes of the model. If not, please refer first to the Matt Raible tutorial.

The model[#2]

We start with creating the Person class :


package org.appfuse.model;

/**
 @author David Rouchy
 
 * @hibernate.class table="person"
 * @struts.form include-all="true" extends="BaseForm"
 */
public class Person extends BaseObject {

    private Long id;
    private String firstName;
    private String lastName;

    /**
     * @hibernate.property column="first_name" length="50"
     */
    public String getFirstName() {
  return this.firstName;
    }

    /**
     @param firstName
     *            the firstName to set
     */
    public void setFirstName(String firstName) {
  this.firstName = firstName;
    }

     /**
      @return Returns the id.
      * @hibernate.id column="id" generator-class="increment"
      *               unsaved-value="null"
      */
     public Long getId() {
  return this.id;
     }

     /**
      * @hibernate.property column="last_name" length="50"
      */
     public String getLastName() {
  return this.lastName;
     }

     /*
       Generate your setters using your favorite IDE: 
      In Eclipse:
          Right-click -> Source -> Generate Getters and Setters
     */

    /*
       Generate equals(), hashCode() and toString() with commonclipse or other
    */
}

  • The Project class


/**
 
 */
package org.appfuse.model;

import java.util.Date;
import java.util.Set;

/**
 @author David Rouchy
 
 * @hibernate.class table="project"
 * @struts.form include-all="true" extends="BaseForm"
 */
public class Project extends BaseObject {
  private Long id;

  private String name;

  private String description;

  private Date startDate;

  private Person leader;

  private Set participants;
/**
   * @hibernate.property
   @return the description
   */
  public String getDescription() {
    return this.description;
  }

  /**
   * @hibernate.id generator-class="native"
   @return the id
   */
  public Long getId() {
    return this.id;
  }

  /**
   * @hibernate.property
   @return the name
   */
  public String getName() {
    return this.name;
  }

  /**
   
   * @hibernate.set lazy="true" cascade="none" table="person_project"
   * @hibernate.collection-key column="project"
   * @hibernate.collection-many-to-many class="org.appfuse.model.Person"
   *                                    column="person"
   @return the participants
   */
  public Set getParticipants() {
    return this.participants;
  }

  /**
   * @hibernate.property
   @return the startDate
   */
  public Date getStartDate() {
    return this.startDate;
  }

  /**
   * @hibernate.many-to-one class="org.appfuse.model.Person" cascade="none"
   @return the leader
   */
  public Person getLeader() {
    return this.leader;
  }
     /*
       Generate your setters using your favorite IDE: 
      In Eclipse:
          Right-click -> Source -> Generate Getters and Setters
     */

    /*
       Generate equals(), hashCode() and toString() with commonclipse or other
    */
}

Copy generated files into the project's directory between each command.

  • Add the following to metadata/sql/sample-data.xml
<table name='person'>
    <column>id</column>
    <column>first_name</column>
    <column>last_name</column>
    <row>
      <value>1</value>
      <value>Matt</value>
      <value>Raible</value>
    </row>
    <row>
      <value>2</value>
      <value>James</value>
      <value>Davidson</value>
    </row>
    <row>
      <value>3</value>
      <value>John</value>
      <value>Smith</value>
    </row>
    <row>
      <value>4</value>
      <value>Steven</value>
      <value>Franks</value>
    </row>
    <row>
      <value>5</value>
      <value>Kevin</value>
      <value>Gerorge</value>
    </row>
    <row>
      <value>6</value>
      <value>Mark</value>
      <value>O'Connor</value>
    </row>
</table>
  
<table name="project">
  <column>id</column>
  <column>name</column>
  <column>description</column>
  <column>startDate</column>
  <column>leader</column>
  <row>
    <value>1</value>
    <value>Appfuse</value>
    <value>AppFuse is an application for "kickstarting" webapp development.</value>
    <value>2004-03-01 13:41:57.886</value>
    <value>1</value>
  </row>
  <row>
    <value>2</value>
    <value>Equinox</value>
    <value>A lightweight version of AppFuse</value>
    <value>2005-06-12 13:41:57.886</value>
    <value>1</value>
  </row>
</table>
  
<table name="person_project">
  <column>project</column>
  <column>person</column>
  <row>
    <value>1</value>
    <value>1</value>
  </row>
  <row>
    <value>1</value>
    <value>2</value>
  </row>
  <row>
    <value>2</value>
    <value>1</value>
  </row>
</table>

  • run ant setup-db

Yeah Baby, Yeah: BUILD SUCCESSFUL
Total time: 1 minute 11 seconds

The webapp [#3]

  • Use appgen for generating DAO, Manager and JSP files.

From "extras/appgen" execute :

  1. ant -Dobject.name=Person -Dappgen.type=pojo
  2. ant -Dmodel.name=Project -Dmodel.name.lowercase=project
  • Replace in projectList.jsp the displaytag part by


<display:table name="${projectList}" cellspacing="0" cellpadding="0" requestURI=""
    id="project" pagesize="25" class="list" export="true">

  <display:column property="id" escapeXml="true" sortable="true"
        url="/editProject.html" paramId="id" paramProperty="id"
        titleKey="projectForm.id"/>
  <display:column property="name" escapeXml="true" sortable="true"
         titleKey="projectForm.name"/>
    <display:column property="description" escapeXml="true" sortable="true"
         titleKey="projectForm.description"/>
    <display:column escapeXml="true" sortable="true"
         titleKey="projectForm.leader">
      <c:out value="${project.leader.firstName}"/> <c:out value="${project.leader.lastName}"/>
    </display:column>
    <display:column escapeXml="true" sortable="true"
         titleKey="projectForm.startDate">
         <fmt:formatDate value="${project.startDate}" />
    </display:column>
    <display:column escapeXml="true" sortable="true"
         title="contributors">

         &nbsp; 
    </display:column>
    <display:setProperty name="paging.banner.item_name" value="project"/>
    <display:setProperty name="paging.banner.items_name" value="projects"/>
</display:table>

  • deploy and rnu your webapp

you should have a project list page like this :

projectList.png

if you edit a project, you should have a page like this :

projectForm.png

The goal of you project is to easyly map model classes to a 'flat' form for Struts, specially, for embedded Person objects into project classes.


Next Up: Part III: Replace Beanutils by XSnapshot
Attachments:
projectList.png Info on projectList.png 6795 bytes
projectForm.png Info on projectForm.png 4490 bytes
classesDiagram.png Info on classesDiagram.png 2001 bytes


Go to top   Edit this page   More info...   Attach file...
This page last changed on 06-Nov-2006 13:53:00 MST by DavidRouchy.