zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    Linux Core Dump
    ODP.NET Managed正式推出
    获取EditText的光标位置
    (Java实现) 洛谷 P1603 斯诺登的密码
    (Java实现) 洛谷 P1603 斯诺登的密码
    (Java实现) 洛谷 P1036 选数
    (Java实现) 洛谷 P1036 选数
    (Java实现) 洛谷 P1012 拼数
    (Java实现) 洛谷 P1012 拼数
    (Java实现) 洛谷 P1028 数的计算
  • 原文地址:https://www.cnblogs.com/lantu1989/p/6251060.html
Copyright © 2011-2022 走看看