• JSTL 自定义标签


    编写描述标签的tld文件,把这个文件放到web-inf/目录下,才能在jsp页面上调用自定义的标签

    package test.yz;
    
    import java.io.IOException;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.jsp.JspContext;
    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.JspWriter;
    import javax.servlet.jsp.PageContext;
    import javax.servlet.jsp.tagext.JspFragment;
    import javax.servlet.jsp.tagext.JspTag;
    import javax.servlet.jsp.tagext.SimpleTag;
    
    public class Handsome implements SimpleTag {
        
        private PageContext pageContext;
        
        @Override
        public void doTag() throws JspException, IOException {
            HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
            JspWriter out = pageContext.getOut();
            out.print(" handsome boy");
            System.out.println(" handsome boy");
        }
    
        @Override
        public JspTag getParent() {
            return null;
        }
    
        @Override
        public void setJspBody(JspFragment arg0) {
        
        }
    
        @Override
        public void setJspContext(JspContext jspContext) {
            pageContext = (PageContext)jspContext;
            
    
        }
    
        @Override
        public void setParent(JspTag arg0) {
            
        }
    
    }
    package test.yz;
    
    import java.io.IOException;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.jsp.JspContext;
    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.JspWriter;
    import javax.servlet.jsp.PageContext;
    import javax.servlet.jsp.tagext.JspFragment;
    import javax.servlet.jsp.tagext.JspTag;
    import javax.servlet.jsp.tagext.SimpleTag;
    
    public class SumTag implements SimpleTag {
        
        private PageContext pageContext;
        private int num1;
        private int num2;
        
        public void setNum1(int num1) {
            this.num1 = num1;
        }
        
        public void setNum2(int num2) {
            this.num2 = num2;
        }
        
        @Override
        public void doTag() throws JspException, IOException {
            // TODO Auto-generated method stub
            HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
            JspWriter out = pageContext.getOut();
            out.print(num1+num2);
        }
    
        @Override
        public JspTag getParent() {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public void setJspBody(JspFragment arg0) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void setJspContext(JspContext arg0) {
            
            pageContext = (PageContext)arg0;
    
        }
    
        @Override
        public void setParent(JspTag arg0) {
            // TODO Auto-generated method stub
    
        }
    
    }
    <?xml version="1.0" encoding="UTF-8" ?>
    
    <taglib xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
        version="2.1">
        
        <!--  -->
        <description>JSTL 1.0 core library</description>
        <display-name>JSTL core</display-name>
        <tlib-version>1.0</tlib-version>
        <short-name>lf</short-name> <!-- 默认的前缀名 -->
        <uri>http://java.lf.com/handsome</uri> <!-- 指定访问路径 -->
      
          <tag>
              <name>hs</name> <!-- 标签名 -->
              <tag-class>test.yz.Handsome</tag-class><!-- 自定义类的全称 -->
              <body-content>empty</body-content>
          </tag>
          
          <!-- 配置属性标签 -->
          <tag>
              <name>sum</name>
              <tag-class>test.yz.SumTag</tag-class>
              <body-content>scriptless</body-content>
              
              <attribute>
                  <name>num1</name>
                  <required>true</required>
                  <rtexprvalue>true</rtexprvalue> <!-- true的话jsp中该属性只可以为表达式,false只能为静态值 -->  
                  <type>java.lang.Integer</type> <!-- 指明参数的类型 -->
              </attribute>
              
              <attribute>
                  <name>num2</name>
                  <required>true</required>
                  <rtexprvalue>true</rtexprvalue> <!-- true的话jsp中该属性只可以为表达式,false只能为静态值 -->  
                  <type>java.lang.Integer</type> <!-- 指明参数的类型 -->
              </attribute>
          </tag>
          
    </taglib>  
    
    
     
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ taglib prefix="lf" uri="http://java.lf.com/handsome" %>
    
    
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        
        
        <title>test</title>
        
        
    
      </head>
      
      <body>
              
          <!-- 测试自定义标签 -->    
         <h1><lf:hs/></h1>
         
         <lf:sum num2="2" num1="3"></lf:sum>
         
         
      </body>
      
    </html>
  • 相关阅读:
    编程填空:第i位替换
    poj 2192 Zipper
    3:拦截导弹
    vijos 1006 晴天小猪历险记之Hill——数字三角形的终极变化
    数字三角形【汇总】
    codevs 1576 最长严格上升子序列
    3299 有序数组合并求第K大问题
    输出数组第k大的元素
    Java线程同步的Monitor机制(Lock配合Condition)
    堆排序Heapsort的Java和C代码
  • 原文地址:https://www.cnblogs.com/lantu1989/p/6251060.html
Copyright © 2020-2023  润新知