Hello kind professional java programmers!
I am a former soldier trying to re-establish himself in the civilian
world and I have so many questions. First of all, what does it really
take to create a java bean that does anything? A friend of mine who
is exploring servlets was very shocked to discover how much work it
takes to create a dropdown HTML menu in Java, so I thought I would
have a go at it.
I thought my javabean should have properties for the:
name of the HTML dropdown menu,
a default <option> tag (to handle when user selects nothing),
a size,
and an SQL statement that when executed would yield 2 columns, the
first being the non-zero id of the row we got from the
database(for the "value" part of an option tag), and the second column
being the displayed part of the
option tag.
Anyway, I haven not handled getting the data from database yet, but
what else must i do or include to make it a bean, and how then would I
use it for a bean inside a JSP page?
/
*******************************************************************************************************************
*
* DropDownHTML
*
* This code creates a serializable object (that might someday
become a Java Bean) that, given an SQL statement,
* and a connection to the appropriate database, will produce a
dropdown menu in HTML, as a string via the toString
* method.
*******************************************************************************************************************/
im****t java.io.*; // we will sup****t serialization
public class DropDownHTML implements Serializable
{
private String SqlStatement; // SQL for 2 columns: first column is
value part, second column is display part
private String Name; // html name for the the drop down menu
private String HandleNoChoice; // if they choose nothing, this will
be displayed -- always has 0 value
private int Size; // size of the display in rows, not the
number of actual rows
private boolean SelectMoreThanOne; // can user choose more than one?
transient int nRows;
public DropDownHTML()
{
this.SqlStatement = new String("select * from categories");
this.Name = new String("example_drop_down");
this.HandleNoChoice = new String("<option selected value=\"0\">No
selection has been choosen</option>");
this.Size = 4;
this.SelectMoreThanOne = false;
}
public DropDownHTML(String mySQLStatement,String myDropDownName, int
size,boolean single )
{
this.SqlStatement = new String(mySQLStatement);
this.Name = new String(myDropDownName);
this.HandleNoChoice = new String("<option selected value=\"0\">No
selection has been choosen</option>");
this.Size = size;
this.SelectMoreThanOne = single;
}
public String toString()
{
String selectMoreThanOne = new String ((this.SelectMoreThanOne) ?
"MULTIPLE " : "SINGLE ");
String selectHTML = new String("<SELECT ") + selectMoreThanOne +
"SIZE=\'" + this.Size + "\' name=\'" + this.Name + "\'>";
String endSelectHTML = new String("</SELECT>");
return selectHTML + endSelectHTML;
}
}


|