package jsp2.examples.el; import java.util.*; public class Compute { public static int add(String x, String y) { int a = 0; int b = 0; try { a = Integer.parseInt(x); b = Integer.parseInt(y); }catch(Exception e) { } return a + b; } } |
<function> <description>add x and y</description> <name>add</name> <function-class>jsp2.examples.el.Compute </function-class> <function-signature>int add(java.lang.String,java.lang.String) </function-signature> </function> |
<%@ taglib prefix="my" uri="http://jakarta.apache.org/tomcat/jsp2-example-taglib %> <HEAD><TITLE>Functions</TITLE></HEAD> <BODY> <H3>Add Numbers</H3> <P> <FORM action="math.jsp" method="GET"> X = <input type="text" name="x" value="${ param["x"]}"> <BR> Y = <input type="text" name="y" value="${param["y"]}"> <input type="submit" value="Add Numbers"> </FORM> <P>The sum is: ${my:add(param["x"],param["y"])} </BODY> </HTML> |
![]() 图3:使用函数 |
package jsp2.examples.simpletag; import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport; import java.io.IOException; /** * SimpleTag handler that prints "This is my first tag!" */ public class HelloTag extends SimpleTagSupport { public void doTag() throws JspException, IOException { getJspContext().getOut().write("This is my first tag!"); } } |
<tag> <description>Prints this is my first tag</description> <name>hello</name> <tag-class>jsp2.examples.simpletag.HelloTag</tag-class> <body-content>empty</body-content> </tag> |
<%@ taglib prefix="mytag" uri="/WEB-INF/jsp2/jsp2-example-taglib.tld" %> <HTML> <HEAD> <TITLE>Simple Tag Handler</TITLE> </HEAD> <BODY> <H2>Simple Tag Handler</H2> <P> <B>My first tag prints</B>: <mytag:hello/> </BODY> </HTML> |
![]() 图4:简单标签处理器 |
Hello there. How are you doing? |
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %> <HTML> <HEAD> <TITLE>JSP 2.0 Examples - Hello World Using a Tag File</TITLE> </HEAD> <BODY> <H2>Tag File Example</H2> <P> <B>The output of my first tag file is</B>: <tags:greetings/> </BODY> </HTML> |
![]() 图5:简单的标签文件 |
<%@ attribute name="color" %><%@ attribute name="bgcolor" %><%@ attribute name="title" %> |
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %> <HTML> <HEAD> <TITLE>Another Tag File Example</TITLE> </HEAD> <BODY> <H2> News Portal: Another Tag File Example</H2> <TABLE border="0"> <TR valign="top"> <TD> <tags:display color="#ff0000" bgcolor="#ffc0c0" title="Travel"> Last French Concorde Arrives in NY <br/> Another Travel Headline <br/> Yet Another Travel Headline <br/> </tags:display> </TD> <TD> <tags:display color="#00fc00" bgcolor="#c0ffc0" title="Technology"> Java for in-flight entertainment <BR> Another Technology Headline <BR> Another Technology Headline <BR> </tags:display> </TD> <TD> <tags:display color="#ffcc11" bgcolor="#ffffcc" title="Sports"> American Football<BR/> NBA<BR/> Soccer<BR/> </tags:display> </TD> </TR> </TABLE> </BODY> </HTML> |
![]() 图6:把标签文件用作模板 |
联系客服