javaweb 自定义标签

来源:互联网 发布:mac不能玩qq游戏 编辑:程序博客网 时间:2024/06/04 20:10

一:自定义foreach标签

1、foreach为集合性子的,所以先定义一个items集合,建一个ForEachTag.java

<span style="font-size:18px;">package com.hp;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;public void setItems(Collection<?> items) {this.items = items;}private String var;public void setVar(String var) {this.var = var;}@Overridepublic void doTag() throws JspException, IOException {//1、遍历items集合if(items != null){for(Object obj:items){//把正在遍历的对象放到pageContext中,键:var 值:正在遍历的对象getJspContext().setAttribute(var, obj);//把标签体的内容直接输出到页面getJspBody().invoke(null);}}}}</span>

同时建一个实体类:Customer.java

package com.hp;public class Customer {private int id;private String  name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Customer(int id, String name) {super();this.id = id;this.name = name;}public  Customer(){}}

2、在lib下建一个Mytag.tld

<span style="font-size:18px;"><?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.2 core library</description><display-name>MyTag core</display-name><tlib-version>1.2</tlib-version><short-name>athp</short-name><uri>http://athp.com/MyTag/core</uri></span>
<span style="font-size:18px;"><pre name="code" class="java"><tag><name>forEach</name>                          <tag-class>com.hp.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></span>


3、建一个测试界面test.jsp

<span style="font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="athp" uri="http://athp.com/MyTag/core"%></span>
<span style="font-size:18px;"><%@page import="com.hp.Customer"%></span>
<span style="font-size:18px;"><pre name="code" class="java"><%String path = request.getContextPath();String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>My JSP 'test.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body></span>
<span style="font-size:18px;"><%List<Customer> customers = new ArrayList<Customer>();customers.add(new Customer(1, "AAA"));customers.add(new Customer(2, "BBB"));customers.add(new Customer(3, "CCC"));customers.add(new Customer(4, "DDD"));customers.add(new Customer(5, "EEE"));request.setAttribute("customers", customers);</span>

%>

<span style="font-size:18px;"><athp:forEach items="${requestScope.customers }" var="customer">     ${customer.id }   ${customer.name } <br><span style="white-space:pre"></span></athp:forEach></span>
<span style="font-size:18px;"></body></html></span>

二:自定义父、子标签

ParentTag.java

package com.hp;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;public class ParentTag extends SimpleTagSupport{private String name="ATHP";public String getName() {return name;}@Overridepublic void doTag() throws JspException, IOException {System.out.println("父标签处理类name"+name);getJspBody().invoke(null);}}
SonTag.java

<span style="font-size:18px;">package com.hp;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.JspTag;import javax.servlet.jsp.tagext.SimpleTagSupport;public class SonTag extends SimpleTagSupport{@Overridepublic void doTag() throws JspException, IOException {//1、得到父标签的引用JspTag parent=getParent();//2、获取父标签的name属性ParentTag parentTag=(ParentTag) parent;String name=parentTag.getName();//3、把name值打印到JSP页面上getJspContext().getOut().print("子标签输出name:"+name);}}</span>

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.2 core library</description><display-name>MyTag core</display-name><tlib-version>1.2</tlib-version><short-name>athp</short-name><uri>http://athp.com/MyTag/core</uri>
<pre name="code" class="java"><tag><name>parentTag</name><tag-class>com.hp.ParentTag</tag-class><body-content>scriptless</body-content></tag><tag><name>sonTag</name><tag-class>com.hp.SonTag</tag-class><body-content>empty</body-content></tag>

</taglib>

test.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="athp" uri="http://athp.com/MyTag/core"%>
<pre name="code" class="java"><%String path = request.getContextPath();String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>My JSP 'test.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body>


<pre name="code" class="java"><!-- 父标签打印name值到控制台 --><athp:parentTag><!-- 子标签以父标签的标签体存在,子标签把父标签的属性打印到页面 --><athp:sonTag/></athp:parentTag>

</body>
</html>

三:自定义选择标签

ChoseTag.java

<span style="font-size:18px;">package com.hp;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;public class ChoseTag extends SimpleTagSupport {private boolean flag=true;public void setFlag(boolean flag) {this.flag = flag;}public boolean isFlag() {return flag;}@Overridepublic void doTag() throws JspException, IOException {getJspBody().invoke(null);}}</span>
WhenTag.java

<span style="font-size:18px;">package com.hp;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;public class WhenTag extends SimpleTagSupport {private boolean test;public void setTest(boolean test) {this.test = test;}@Overridepublic void doTag() throws JspException, IOException {if(test){ChoseTag choseTag=(ChoseTag) getParent();boolean flag=choseTag.isFlag();if(flag){getJspBody().invoke(null);choseTag.setFlag(false);}}}}</span>
OtherwiseTag.java

<span style="font-size:18px;">package com.hp;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;public class OtherwiseTag extends SimpleTagSupport {@Overridepublic void doTag() throws JspException, IOException {ChoseTag choseTag=(ChoseTag) getParent();if(choseTag.isFlag()){getJspBody().invoke(null);}}}</span>
Mytag.tld

<span style="font-size:18px;"><?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.2 core library</description><display-name>MyTag core</display-name><tlib-version>1.2</tlib-version><short-name>athp</short-name><uri>http://athp.com/MyTag/core</uri></span>
<span style="font-size:18px;"><tag><name>choseTag</name><tag-class>com.hp.ChoseTag</tag-class><body-content>scriptless</body-content></tag><tag><name>whenTag</name><tag-class>com.hp.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.hp.OtherwiseTag</tag-class><body-content>scriptless</body-content></tag></span>
<span style="font-size:18px;"></taglib></span>
test.jsp

<span style="font-size:18px;"><%@page import="com.hp.Customer"%><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="athp" uri="http://athp.com/MyTag/core"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %><%String path = request.getContextPath();String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>My JSP 'test.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head></span>
<span style="font-size:18px;"><body></span>
<span style="font-size:18px;"><%List<Customer> customers = new ArrayList<Customer>();customers.add(new Customer(1, "AAA"));customers.add(new Customer(2, "BBB"));customers.add(new Customer(3, "CCC"));customers.add(new Customer(4, "DDD"));customers.add(new Customer(5, "EEE"));request.setAttribute("customers", customers);Map<String, Customer> customerMap=new HashMap<String,Customer>();customerMap.put("a", customers.get(0));customerMap.put("b", customers.get(1));customerMap.put("c", customers.get(2));customerMap.put("d", customers.get(3));customerMap.put("e", customers.get(4));request.setAttribute("customerMap", customerMap);%><!-- 遍历 <c:forEach items="${customerMap }" var="cust">${cust.key } ${cust.value.id } ${cust.value.name }<br></c:forEach><br><br><c:forEach items="${requestScope.customers }" var="customer">     ${customer.id }   ${customer.name } <br></c:forEach><br><athp:forEach items="${requestScope.customers }" var="customer">     ${customer.id }   ${customer.name } <br></athp:forEach> --></span>
<span style="font-size:18px;"><br><athp:choseTag><athp:whenTag test="${param.age >24}">大学毕业</athp:whenTag><athp:whenTag test="${param.age >20}">高中毕业</athp:whenTag><athp:otherWise>高中以下</athp:otherWise></athp:choseTag></span>
<span style="font-size:18px;"></body></html></span>









0 0
原创粉丝点击