|
| |
"JSP 2.0 Technology"
Vol. 8, Issue 7, p. 24
Listing 1 Using JSP configuration to disable scriptlets
<!-- /WEB-INF/web.xml -->
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd"
version="2.4">
<jsp-config>
<jsp-property-group>
<url-pattern>/client/*.jsp</url-pattern>
<scripting-invalid>true</scripting-invalid>
</jsp-property-group>
</jsp-config>
</web-app>
Listing 2 Defining a new EL function
<!-- /WEB-INF/tlds/mytaglib.tld -->
...
<function>
<description>
Returns a random number in the provided range.
</description>
<name>randomNumber</name>
<function-class>mytaglib.Functions</function-class>
<function-signature>
int randomNumber( int, int )
</function-signature>
<example>
my:randomNumber( 1, 10 )
</example>
</function>
...
Listing 3 JSP Page to display a shopping cart
<%@ taglib prefix="my" uri="http://acme.com/mytaglib" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Here are the contents of your shopping cart:<br/>
<my:queryCart var="products" user="${username}" />
<table>
<c:forEach var="product" items="${products}">
<tr>
<td>${product.name}</td>
<td>${product.price}</td>
</tr>
</c:forEach>
</table>
Listing 4 Tag files encapsulate reusable JSP code
<%-- /WEB-INF/tags/showCart.tag --%>
<%@ attribute name="username" rtexprvalue="true" %>
<%@ taglib prefix="my" uri="http://acme.com/mytaglib" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core %>
<my:queryCart var="products" user="${username}" />
<table>
<c:forEach var="product" items="${products}">
<tr>
<td>${product.name}</td>
<td>${product.price}</td>
</tr>
</c:forEach>
</table>
Listing 5 Repeat Action implemented as a 'classic tag handler'
/* /WEB-INF/classes/mytaglib/RepeatClassicTag.java */
package mytaglib;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class RepeatClassicTag
extends BodyTagSupport
{
private int count;
private int timesRemaining;
public int doStartTag() {
this.timesRemaining = count;
return EVAL_BODY_INCLUDE;
}
public int doAfterBody() {
int result = EVAL_BODY_AGAIN;
this.timesRemaining--;
if( this.timesRemaining == 0 ) {
result = SKIP_BODY;
}
return result;
}
public void setCount( int count ) {
this.count = count;
}
public int getCount() {
return this.count;
}
}
Listing 6 Repeat Action implemented as a 'simple tag handler'
<* /WEB-INF/classes/mytaglib/RepeatSimpleTag.java *>
package mytaglib;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class RepeatSimpleTag
extends SimpleTagSupport
{
private int count;
public void doTag()
throws IOException, JspException
{
for( int i = 0; i < count; i++ ) {
getJspBody().invoke( null );
}
}
public void setCount( int count ) {
this.count = count;
}
public int getCount() {
return this.count;
}
}
|
|
All Rights Reserved
Copyright © 2004 SYS-CON Media, Inc.
E-mail: info@sys-con.com
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.
|