java èªå®ä¹æ ç¾
1.èªå®ä¹æ ç¾å®ç°å¯¹éååè½
ForEachTag.java
package com.geeksun.tag;
import java.io.IOException;
import java.util.Collection;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class ForEachTag extends SimpleTagSupport
{
private Collection<?> items;
private String var;
public void setItems(Collection<?> items)
{
this.items = items;
}
public void setVar(String var)
{
this.var = var;
}
@Override
public void doTag() throws JspException, IOException
{
if(items != null)
{
for(Object obj:items)
{
getJspContext().setAttribute(var, obj);
getJspBody().invoke(null);
}
}
}
}
index.jsp
<%@page import="com.geeksun.one.Customer"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
//模æservletæä½
List<Customer> customers = new ArrayList<Customer>();
customers.add(new Customer("1","1","1","1"));
customers.add(new Customer("2","2","2","2"));
customers.add(new Customer("3","3","3","3"));
customers.add(new Customer("4","4","4","4"));
customers.add(new Customer("5","5","5","5"));
request.setAttribute("customers", customers);
%>
<Geeksun:forEach items="${requestScope.customers }" var="customer">
${customer.name },${customer.address },${customer.cardType },${customer.card }
<br><br>
</Geeksun:forEach>
</body>
</html>
2.å©ç¨å¸¦ç¶æ ç¾çæ ç¾å®ç°if else if elseçåè½
ChooseTag.java
package com.geeksun.tag2;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class ChooseTag extends SimpleTagSupport
{
private boolean flag = true;
public void setFlag(boolean flag)
{
this.flag = flag;
}
public boolean isFlag()
{
return flag;
}
@Override
public void doTag() throws JspException, IOException
{
getJspBody().invoke(null);
}
}
WhenTag.java
package com.geeksun.tag2;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspTag;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class WhenTag extends SimpleTagSupport
{
private boolean test;
public void setTest(boolean test)
{
this.test = test;
}
@Override
public void doTag() throws JspException, IOException
{
JspTag jspTag = getParent();
ChooseTag parentTag = (ChooseTag)jspTag;
if(parentTag.isFlag() == true&&test == true)
{
parentTag.setFlag(false);
getJspBody().invoke(null);
}
}
}
OtherwiseTag.java
package com.geeksun.tag2;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspTag;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class OtherwiseTag extends SimpleTagSupport
{
private boolean test;
public void setTest(boolean test)
{
this.test = test;
}
@Override
public void doTag() throws JspException, IOException
{
JspTag jspTag = getParent();
ChooseTag parentTag = (ChooseTag)jspTag;
if(parentTag.isFlag() == true)
{
getJspBody().invoke(null);
}
}
}
TestTag.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://www.Geeksun.com/mytag/core/" prefix="Geeksun"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<Geeksun:Choose>
<Geeksun:When test="${param.age > 22}">大å¦æ¯ä¸</Geeksun:When>
<Geeksun:When test="${param.age > 20}">é«ä¸æ¯ä¸</Geeksun:When>
<Geeksun:Otherwise>é«ä¸æªæ¯ä¸</Geeksun:Otherwise>
</Geeksun:Choose>
</body>
</html>
mytag.tld
<?xml version="1.0" encoding="UTF-8" ?>
<taglib 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 http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>MyTag 1.1 core library</description>
<display-name>MyTag core</display-name>
<tlib-version>1.0</tlib-version>
<!--å®ä¹ä¸ä¸ªç®ççå称ï¼ä¸»è¦æ¯ç»ä¸äºå·¥å
·ä½¿ç¨ï¼å¿
é¡»å
ç´ ï¼-->
<short-name>Geeksun</short-name>
<!--å®ä¹æ¤æ ç¾åºçuriè·¯å¾ï¼ç¨äºå¯ä¸æ è¯è¯¥æ°æ®åºï¼ä¾¿äºé¡µé¢çå¼ç¨-->
<uri>http://www.Geeksun.com/mytag/core/</uri>
<!--æ¤æ ç¾åºä¸çä¸ä¸ªæ ç¾å¤çå¨ç声æ-->
<tag>
<!--该æ ç¾å称ï¼å¿
é¡»å
ç´ ï¼-->
<name>Choose</name>
<!--该æ ç¾å¤çç±»çå
¨éå®åï¼å¿
é¡»å
ç´ ï¼-->
<tag-class>com.geeksun.tag2.ChooseTag</tag-class>
<!--ææ该æ ç¾ä¸»é¢ç±»åï¼-->
<body-content>scriptless</body-content>
</tag>
<tag>
<name>When</name>
<tag-class>com.geeksun.tag2.WhenTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>Otherwise</name>
<tag-class>com.geeksun.tag2.OtherwiseTag</tag-class>
<body-content>scriptless</body-content>
</tag>
<tag>
<name>forEach</name>
<tag-class>com.geeksun.tag.ForEachTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
- <description><display-name>ï¼ä½ç¨ä¸taglibçä¸æ ·ï¼å¯ä»¥æå¤ä¸ªã
- <name>ï¼å¿ éï¼æ ç¾å称ã
- <tag-class>ï¼æ ç¾å¤çç±»ã
- <tei-class>ï¼å¯éï¼javax.servlet.jsp.tagext.TagExtraInfoç继æ¿ç±»ï¼ç¨äºæ ¡éªtagçå±æ§ï¼ç¡®ä¿ä½¿ç¨çæ£ç¡®ï¼<c:import><c:forEach>å°±æå®ä¹ã
- <body-content>ï¼å¿ éï¼æå个é项emptyãscriptlessãJSPãtagdependentï¼ä¹åç¯ç« ä»ç»ã
-
<variable>ï¼å¯éï¼å¯å®ä¹å¤ä¸ªï¼JSTL TLDä¸é½æ²¡æ使ç¨ï¼å®æ¯ç¨æ¥æä¾tagçç»æçåç§åéä¿¡æ¯ãä»ä¹æ¯variableï¼æ们éè¿pageContext.setAttribute/getAttribute设置æè·åæ个å¼ï¼è¿å°±æ¯åéãä¸éè¿variableï¼æ们ä¹å¯ä»¥éè¿ELæ¥è·åãå¦ææä¾äºvariableï¼æ们é¤äºELå¤ï¼è¿å¯ä»¥å¨java代ç ä¸ä½¿ç¨è¿äºåéãä½ç°å¨é½æ¯éç¨MVC模å¼ï¼JSP页é¢ä¸java代ç åºæ¬ä¸ä¹ä¸ä¼è¢«ä½¿ç¨ãååæ°ä¾æ¬¡å æ¬ï¼
- <description>
- <name-given> åéçåå ã
- <name-from-attribute> å³å®åéååï¼è¿å<name-given>ç¸äºå²çªï¼åªè½äºéä¸ãStringï¼ä¸è½æ¯ELçå¨è¿è¡æ¶è®¡ç® ã
- <variable-class> åéçç±»å ã
- <declare> true表示åéæ¯æ°ï¼éè¦å£°æï¼false表示已åå¨ï¼åªæ¯ä¿®æ¹å¼ã
- <scope> 缺çæ¯NESTEDï¼å³å¨tagå ææï¼AT_BEGIN表示åétagå åä¹åææï¼AT_END表示åéåªå¨tagåææã
-
<attribute>ï¼å¯éï¼å¯å®ä¹å¤ä¸ªï¼å®ä¹Tagçå±æ§ã
- <description>ï¼å¯éã <name>ï¼å±æ§å称ã
- <requiredï¼å¯éï¼å±æ§æ¯å¦å¿ é¡»true/falseï¼ç¼ºç为falseã
- <rtexprvalueï¼å¯éï¼rtexprvalueæ¯runtime expression valueï¼true表示å 许ELè¿ç±»è¿è¡æ¶è®¡ç®å¼ï¼falseåä¸å 许ï¼ç¼ºç为falseã
- <type>ï¼å¯éï¼å±æ§ç±»åï¼ç¼ºç为Objectã
下一篇: DVD案例(对象数组)