HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Creating a JSP JavaBeans Framework"
Volume: 5 Issue: 2, p.24

Download files assoicated with this article

	

Listing 1: 

package jspf; 
public class DisplayEvent extends 
    java.util.EventObject { 
  private int rowIndex; 
  
  // pass rowIndex so tag can retrive 
  // multivalued items 
  public DisplayEvent(Object source, 
      int rowIndex) { 
    super(source); 
    this.rowIndex = rowIndex; 
  } 
  public int getRowIndex() { 
    return rowIndex; 
  } 
} 

package jspf; 
public interface DisplayListener 
    extends java.util.EventListener { 
  public void formatOutputProperties( 
      DisplayEvent e); 
} 

Listing 2: 
  
package jspf; 
public class DataAdapter extends Object 
    implements java.io.Serializable { 
  private Object    value; 
  private Object[]  arrayValue; 
  private boolean   multivalued = false; 
  
  // wrap a single-value item 
  public DataAdapter(Object value) { 
    this.value = value; 
    multivalued = false; 
  } 
  // wrap an array item 
  public DataAdapter(Object[] value) { 
    this.arrayValue = value; 
    multivalued = true; 
  } 
  public Object getValue() { 
    if(multivalued) { 
      return arrayValue[0]; 
    } else { 
      return value; 
    } 
  } 

 // return the requested value. Note: 
  // works with both single- and multi- 
  // value items 
  public Object getValue(int index) { 
   if(multivalued) { 
      return arrayValue[index]; 
    } else { 
      return value; 
    } 
  } 
} 

Listing 3: 
  
  private String jspName = ""; 
  private DataAdapter valueSource; 
  private String defaultStringValue = ""; 
  private String stringValue = ""; 
  private String extraAttr = ""; 
  private Vector displayListeners = new 
  Vector(); 

  abstract public String render(int index); 
  
  protected String display(int index) { 
    // reset our temp value before display 
    setStringValue(""); 
    // get default value or get dynamic value 
    // from our data source 
    if(getValueSource() == null) { 
      setStringValue(getDefaultString- 
      Value()); 
    } else { 
      setStringValue(getValueSource(). 
          getValue(index).toString()); 
    } 
    // fire format event so developers can 
    // customize stringValue to display 
    Vector v1 = (Vector)displayListen- 
    ers.clone(); 
    for(int i=0; i < v1.size(); i++) 
      ((DisplayListener) v1.elementAt(i)). 
          formatOutputProperties( 
          new DisplayEvent(this, index)); 
    // create html by calling tag's 
    // render process 
    return render(index); 
  } 

Listing 4: 
  
package jspf; 
public class HtmlTextTag extends DataTag { 
  private int maxLength = 10; 
  private int size = 10; 
  private boolean password = false; 

  public String render(int index) { 
    StringBuffer  html = new String- 
    Buffer(); 

    html.append("<INPUT TYPE="); 
    // check for password type 
    if(isPassword()) 
      html.append("PASSWORD "); 
    else 
      html.append("TEXT "); 
    html.append("NAME=").append("\""). 
        append(getJSPName()).append("\"  "); 
    html.append("VALUE=").append("\""). 
        append(getStringValue()).append("\" "); 
    html.append("MAXLENGTH=\""). 
        append(getMaxLength()).append("\" "); 
    html.append("SIZE=\"").append(getSize()). 
        append("\""); 
    // if extra attributes included, 
    // append them. 
    if(! getExtraAttr().equals("")) 
     html.append(" ").append(getExtraAttr()); 
    html.append(">"); 
    return html.toString(); 
  } 
  // property get / set methods excluded 
  // from listing  see complete source 
  

Listing 5: 
  
package jspf; 
import java.util.Hashtable; 
  
public abstract class PageBean 
    implements java.io.Serializable { 
  private Hashtable tagTable = new Hashtable(); 

  // single value display process 
  public String display(String name) { 
    return processDisplay(-1, name); 
  } 
  // overloaded display process multi- 
  // value items 
  public String display(int index, 
  String name) { 
    return processDisplay(index, name); 
  } 
  // forward display request to named tag 
  protected String processDisplay(int index, 
      String name) { 
    return ((DataTag) tagTable.get(name)). 
          display(index); 
  } 
  // add tag to hashtable of tags we 
  // can display 
  protected synchronized void enableTag( 
      DataTag tag) { 
    tagTable.put(tag.getJSPName(), tag); 
  } 
} 

Listing 6: 
  
package jspf; 

public class BudgetPageBean extends jspf.PageBean{ 
  String costCenter = "Value to   replace"; 
  String[] dept = {"Acct", "Mgmt", "Mkt"}; 
  Integer[] budget = new Integer[3]; 

  public BudgetPageBean() { 
    // visual cafe init - varies per IDE 
    vcInit(); 
    // load budget array 
    budget[0] = new Integer(100); 
    budget[1] = new Integer(120); 
    budget[2] = new Integer(130); 
    // load tags we'll display from JSP 
    enableTag(costCenterTag); 
    enableTag(deptTag); 
    enableTag(budgetTag); 
    enableTag(okTag); 
    enableTag(cancelTag); 
    // set sourceValues 
    costCenterTag.setValueSource(cost- 
    Center); 
    deptTag.setValueSource(dept); 
    budgetTag.setValueSource(budget); 
  
    SymDisplay lSymDisplay = new 
    SymDisplay(); 
     budgetTag.addDisplayListen- 
     er(lSymDisplay); 
  
   costCenterTag.addDisplayListener(lSymDis play); 
  } 
  
  public void vcInit() { 
    costCenterTag.setJSPName("costCenter"); 
    costCenterTag.setMaxLength(8); 
    costCenterTag.setSize(8); 
    deptTag.setJSPName("dept"); 
    budgetTag.setJSPName("budget"); 
    okTag.setExtraAttr( 
      "onClick=\"return 
       buttonEvent(\'Ok\')\""); 
    okTag.setDefaultStringValue("OK"); 
    okTag.setJSPName("okButton"); 
    cancelTag.setExtraAttr( 
    "onClick=\"return buttonEvent(\'Can- 
     cel\')\""); 
    cancelTag.setDefaultStringValue("Cancel"); 
    cancelTag.setJSPName("cancelButton"); 
  } 
  HtmlTextTag costCenterTag = new Html- 
  TextTag(); 
  HtmlStaticTextTag deptTag = new 
    HtmlStaticTextTag(); 
  HtmlTextTag budgetTag = new HtmlText Tag(); 
  HtmlButtonTag okTag = new HtmlButtonTag(); 
  HtmlButtonTag cancelTag = new Html- 
  ButtonTag(); 

  // diplaylistener class added by IDE 
  class SymDisplay implements Dis- 
  playListener { 
    public void formatOutputProperties( 
        DisplayEvent event) { 
      Object object = event.get- 
      Source(); 
      if (object == budgetTag) 
        budgetTag_formatOutputProper- 
        ties(event); 
  else if (object == costCenterTag)   costCenterTag_formatOutputProperties(event); 
    } 
  } 
  // add budget currency formatting 
  void budgetTag_formatOutputProperties( 
      DisplayEvent event) { 
    DataTag tag = (DataTag) event.get- 
    Source(); 
    tag.setStringValue("$" + tag.getStringValue() 
        + ".00"); 
  } 
  // set cost center value on the fly 
  // via code 
  void costCenterTag_formatOutputProperties( 
      jspf.DisplayEvent event) { 
    DataTag tag = (DataTag) event.get- 
    Source(); 
    tag.setStringValue("A12345"); 
  } 
} 

Listing 7: 
  
<%@page language="java" session="true"%> 
<jsp:useBean id="pageBean" scope="page" 
 class="jspf.BudgetPageBean"/> 
<html><head><title>JSP Budget Maintenance</title> 
<SCRIPT LANGUAGE="JavaScript"> 
  // sample Javascript function we call 
  // via setting extraAttr property 
  function buttonEvent(button) { 
    alert("I clicked the " + button + " 
    button!"); 
    return false; 
  } 
</SCRIPT> 
</head><body bgcolor="#FFFFFF"> 
<h2>JSP Budget Maintenance</h2> 
<form method="post" action="tp.jsp" name="frm1"> 
<p>Cost Center: 
  <%=pageBean.display("costCenter")%></p> 
  <table border="1"> 
    <tr><td>Department</td><td>Budget</td></tr> 
    <%for(int i = 0; i < 3; i++) {%> 
    <tr> 
      <td><%=pageBean.display(i,"dept")%></td> 
      <td><%=pageBean.display(i,"budget")%></td> 
    </tr> 
    <%}%> 
  </table> 
<p><%=pageBean.display("okButton")%> 
<%=pageBean.display("cancelButton")%></p> 
</form></body></html> 



 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.