zoukankan      html  css  js  c++  java
  • 自定义标签

    一,jsp自定义标签

      1)在jsp中会遇到现有标签不能解决的问题,而自定义标签就是一个很好的选择

    二,定义步骤

      1)决定要实现的功能以及格式,例如 <control : if code="1">要展示的内容</control : if>

      2)编写tld文件,放在WEB-INF下

      3)编写处理类

    三,实现功能

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@taglib prefix="control" uri="WEB-INF/control.tld"%>
    <!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>控制</title>
    </head>
    <body>
           <control:if code="1">
        <a href="http://www.baidu.com">百度</a>
           </control:if>
         <a href="http://www.jd.com">京东</a>
         <a href="http://www.taobao.com">淘宝</a>
    </body>
    </html>

    四,编写tld文件

    <?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"
    >
      <tlib-version>1.0</tlib-version>
      <short-name>control</short-name>
      <tag>
        <name>if</name>
        <tag-class>com.****.utli.UserCode</tag-class>
        <body-content>scriptless</body-content>
             //body-content可以参考https://www.cnblogs.com/keyi/p/7127685.html
        <attribute>
           <description>权限码</description>
           <required>true</required>
           <rtexprvalue>true</rtexprvalue>
           <name>code</name>
        </attribute>
      </tag>
    </taglib>

    五,处理类

    public class UserCode extends SimpleTagSupport{
        
        private String code;
    
        public String getCode() {
            return code;
        }
    
        public void setCode(String code) {
            this.code = code;
        }
        
        @Override
        public void doTag() throws JspException, IOException {
            // 获取请求对象
            HttpServletRequest request = (HttpServletRequest) ((PageContext) this.getJspContext()).getRequest();
            // 获取 session域
            HttpSession session = request.getSession();
            if("1".equals(code)) {
                JspFragment body = this.getJspBody();
                body.invoke(null);
            }
        }
    }
  • 相关阅读:
    React之Antd组件<Input />属性defaultValue问题记录。
    java多继承问题
    python多继承问题
    React之state
    概念题
    修复linux的yum源
    stl
    java
    python
    各种执行方式
  • 原文地址:https://www.cnblogs.com/hi-feng/p/8029126.html
Copyright © 2011-2022 走看看