码迷,mamicode.com
首页 > 其他好文 > 详细

自定义标签

时间:2015-12-17 22:32:13      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:

Step 1 :   写一个java类,实现SimpleTag接口或继承SimpleTagSupport抽象类

 

Step 2:    在java类中,override doTag()方法,再该方法中,实现标签的业务逻辑

 

Step 3:    在tld文件中描述标签。

 

Step 4:    在JSP页面中,使用taglib指令导入标签

 

java类:

 1 package mytag;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.jsp.*;
 6 import javax.servlet.jsp.tagext.SimpleTagSupport;
 7 
 8 public class HelloTag extends SimpleTagSupport{
 9 
10     private String  info;
11     private int num;
12     
13     public String getInfo() {
14         return info;
15     }
16 
17     public void setInfo(String info) {
18         System.out.println("setinfo");
19         this.info = info;
20     }
21 
22     public int getNum() {
23         return num;
24     }
25 
26     public void setNum(int num) {
27         System.out.println("setnum");
28         this.num = num;
29     }
30 
31     public HelloTag() {
32         System.out.println("constructor");
33     }
34 
35     @Override
36     public void doTag() throws JspException, IOException {
37         // TODO Auto-generated method stub
38         JspContext jc = getJspContext();
39         PageContext pc = (PageContext)jc;
40         JspWriter out=pc.getOut();
41         for(int i=0;i<num;i++){
42             out.println(info+"<br/>");
43         }
44     }
45 
46 }

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>JSTL 1.1 core library</description>
  <display-name>JSTL core</display-name>
  <tlib-version>1.1</tlib-version>
  <short-name>c1</short-name>
  <uri>http://www.caxa.com/mytag</uri>
  
  
   <tag>
    <name>sayHello</name>
    <tag-class>mytag.HelloTag</tag-class>
    <body-content>empty</body-content>
    <attribute>
        <name>info</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>num</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>
  
  
  
  
  
  
  
  </taglib>

jsp文件(也就是使用过程)如下:

<%@ page language="java" import="java.util.*,java.lang.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="fn1" 
           uri="http://www.caxa.com/fn1" %>
<%@  taglib prefix="c1"  
            uri="http://www.caxa.com/mytag" %>       
<%
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 ‘MyJsp.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>
  <%
  request.setAttribute("str1","xvgfdgsdfgfdgg");
   %>
   s1:${fn1:toUpperCase(str1)}
   
   <c1:sayHello num="100" info="hello world"/>
   
    This is my JSP page. <br>
  </body>
</html>

 

自定义标签

标签:

原文地址:http://www.cnblogs.com/hzzhero/p/5055403.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!