You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Anthony Martin <An...@TRAMS.com> on 2001/06/12 03:33:41 UTC

Interfaces in Struts

I wanted to share my approach because it seems successful for what we're
doing.  I can't imagine we're the only ones doing it, but just in case we
are, I wanted to share.  I'd also like to hear any and all comments and
criticisms.

We use Struts to present database tables to the user so that we may collect
information from them, validate it, and process it.  Nothing new here.

In order to make sure we only have to design these beans once, I used
interfaces from the beginning.  So a profile starts out as something like
this:

/*
 * Profile.java
 */

package com.trams.project ;

public interface Profile {

	int proifleno = 0 ;
	String firstname = null ;
	String lastname = null ;
	.
	.
	.

	int getProfileno ( ) ;
	void setProfileno ( int profileno ) ;

	String getFirstname ( ) ;
	void setFirstname( String firstname ) ;

	void setLastname ( String lastname ) ;
	String getLastname ( ) ;

	.
	.
	.

}

// end of Profile.java

Once I have the interface, I also write the class that uses it.  In this
case, a database interactivity class that constructs SQL statements in order
to process the data presented to it:

/*
 * ProfileDBM.java
 */

package com.trams.project ;

public class ProfileDBM extends DatabaseManager {

	Profile table = null ;

	public void setTable ( Object table ) { this.table = (Profile) table
; }

	public void create ( ) {

		.
		.
		.

	}

	public void retrieve ( ) {

		.
		.
		.
		table.setProfileno(rs.getInt("PROFILENO")) ;
		table.setFirstname(rs.getString("FIRSTNAME")) ;
		table.setLastname(rs.getString("LASTNAME")) ;
		.
		.
		.

	}

	public void update ( ) {

		.
		.
		.

	}

	public void delete ( ) {

		.
		.
		.

	}

}

At this point you can see (or have to trust) that nothing uses Struts,
Servlets, or anything other than basic packages.  The above code could be
used to populate any object with Profile information, be it a command line
utility, AWT object, or even a Struts ActionForm.

I then implement my Profile into an ActionForm:

/*
 * ProfileForm.java
 */

package com.trams.project ;

public final class ProfileForm extends ActionForm implements Profile {

	private int proifleno ;
	private String firstname ;
	private String lastname ;
	.
	.
	.

	int getProfileno ( ) { return this.profileno ; }
	void setProfileno ( int profileno ) { this.profileno = profileno ; }

	String getFirstname ( ) { return this.firstname ; }
	void setFirstname( String firstname ) { this.firstname = firstname ;
}

	void setLastname ( String lastname ) { return this.lastname ; }
	String getLastname ( ) { this.lastname = lastname ; }

	.
	.
	.
	
}

Then a fragment of my ProfileAction.java file:

	.
	.
	.
	ProfileForm profileForm = (ProfileForm) form ;

	ProfileDBM dbm = new ProfileDBM() ;
	dbm.setCon(con) ; // part of my DatabaseManager class
	dbm.setTable(profileForm) ;
	dbm.retrieve() ;
	.
	.
	.

That populates the entire form so it can be displayed and manipulated by the
user.  The other methods aid in creating, updating, and deleting.

Nothing really spectacular, I know.  But I remember people talking about it,
and I just wanted to share what I've been doing.  Again, we've had great
success in representing our existing database.  And at this rate, we'll have
more than a Struts API for accessing our database.  We'll also have
independent interfaces that could be used by any developer who wants the SQL
logic all worked out for them.


Anthony
	
These pearls of thought in Persian gulfs were bred,
Each softly lucent as a rounded moon;
The diver Omar plucked them from their bed,
Fitzgerald strung them on an English thread.
-James Russell Lowell, in a copy of Omar Khayyam