AppFuseXSnapshotBase |
|
Your trail: |
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
- introduction : presenting the example
- the model
- the webapp
Introduction : presenting the example
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...)
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
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
*/
}
|
/**
*
*/
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>
Yeah Baby, Yeah:
BUILD SUCCESSFUL
Total time: 1 minute 11 seconds
The webapp
- Use appgen for generating DAO, Manager and JSP files.
From "extras/appgen" execute :
- ant -Dobject.name=Person -Dappgen.type=pojo
- 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">
</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 :
if you edit a project, you should have a page like this :
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:
|