|
Source Code for Shuffle Example
________________________________________________________
<html>
<head>
<title> Shuffle Example/title>
</head>
<body>
<h1> Shuffle Example</h1>
<hr>
<p> Try reloading the page a few times. Both the rows and the columns
are shuffled and appear different each time.</p>
<p> Here's how the code works. The SimpleTag handler called
<my:shuffle> accepts three attributes. Each attribute is a
JSP Fragment, meaning it is a fragment of JSP code that can be
dynamically executed by the shuffle tag handler on demand. The
shuffle tag handler executes the three fragments in a random order.
To shuffle both the rows and the columns, the shuffle tag is used
with itself as a parameter.</p>
<hr>
<blockquote>
<font color="#ffffff">
<table>
<my:shuffle>
<jsp:attribute name="fragment1">
<tr>
<my:shuffle>
<jsp:attribute name="fragment1">
<my:tile color="#ff0000" label="A"/>
</jsp:attribute>
<jsp:attribute name="fragment2">
<my:tile color="#00ff00" label="B"/>
</jsp:attribute>
<jsp:attribute name="fragment3">
<my:tile color="#0000ff" label="C"/>
</jsp:attribute>
</my:shuffle>
</tr>
</jsp:attribute>
<jsp:attribute name="fragment2">
<tr>
<my:shuffle>
<jsp:attribute name="fragment1">
<my:tile color="#ff0000" label="1"/>
</jsp:attribute>
<jsp:attribute name="fragment2">
<my:tile color="#00ff00" label="2"/>
</jsp:attribute>
<jsp:attribute name="fragment3">
<my:tile color="#0000ff" label="3"/>
</jsp:attribute>
</my:shuffle>
</tr>
</jsp:attribute>
<jsp:attribute name="fragment3">
<tr>
<my:shuffle>
<jsp:attribute name="fragment1">
<my:tile color="#ff0000" label="!"/>
</jsp:attribute>
<jsp:attribute name="fragment2">
<my:tile color="#00ff00" label="@"/>
</jsp:attribute>
<jsp:attribute name="fragment3">
<my:tile color="#0000ff" label="#"/>
/jsp:attribute>
</my:shuffle>
</tr>
</jsp:attribute>
</my:shuffle>
</table>
</font>
</blockquote>
</body>
</html>
__________________________________________________________________
Source Code for ShuffleSimpleTag.java
package jsp2.examples.simpletag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
/**
* SimpleTag handler that accepts takes three attributes of type
* JspFragment and invokes then in a random order.
*/
public class ShuffleSimpleTag extends SimpleTagSupport {
private JspFragment fragment1;
private JspFragment fragment2;
private JspFragment fragment3;
public void doTag() throws JspException, IOException {
switch( (int)(Math.random() * 6) ) {
case 0:
fragment1.invoke( null );
fragment2.invoke( null );
fragment3.invoke( null );
break;
case 1:
fragment1.invoke( null );
fragment3.invoke( null );
fragment2.invoke( null );
break;
case 2:
fragment2.invoke( null );
fragment1.invoke( null );
fragment3.invoke( null );
break;
case 3:
fragment2.invoke( null );
fragment3.invoke( null );
fragment1.invoke( null );
break;
case 4:
fragment3.invoke( null );
fragment1.invoke( null );
fragment2.invoke( null );
break;
case 5:
fragment3.invoke( null );
fragment2.invoke( null );
fragment1.invoke( null );
break;
}
}
public void setFragment1( JspFragment fragment1 ) {
this.fragment1 = fragment1;
}
public void setFragment2( JspFragment fragment2 ) {
this.fragment2 = fragment2;
}
public void setFragment3( JspFragment fragment3 ) {
this.fragment3 = fragment3;
}
}
__________________________________________________________
Source Code for TileSimpleTag.java
package jsp2.examples.simpletag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
/**
* Displays a tile as a single cell in a table.
*/
public class TileSimpleTag extends SimpleTagSupport {
private String color;
private String label;
public void doTag() throws JspException, IOException {
getJspContext().getOut().write (
"<td width=\"32\" height=\"32\" bgcolor=\"" + this.color +
"\"><font color=\"#ffffff\"><center>" + this.label +
"</center></font></td>" );
}
public void setColor( String color ) {
this.color = color;
}
public void setLabel( String label ) {
this.label = label;
}
}
|