结构:
BinaryConverts.java
package com.jsp.tag; public class BinaryConverts { public static void main(String[] args) { System.out.println(toBinaryString("10","10".length())); } /** * 10进制转成2进制 * @param s * @param len * @return */ public static String toBinaryString(String s,Integer len) { try { String string = Integer.toBinaryString(Integer.valueOf(s)); int l = len - string.length(); for(int i=0;i<l;i++){ string = "0"+string; } return string; } catch (Exception e) { e.printStackTrace(); } return null; } }
testFunction.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/j2eeweb-jsptaglibrary_2_0.xsd" version="2.0"> <tlib-version>1.0</tlib-version> <short-name>mytest</short-name> <uri>http://zhengcj.test.com</uri> <function> <!--EL页面调用名字--> <name>toBinaryString</name> <!--指定标签的处理类,指定了标签由哪个Java类来处理。--> <function-class>com.jsp.tag.BinaryConverts</function-class> <!--指定EL页面调用名字中实际调用的方法.指定处理类的实际方法.参数和回调函数要写完整路径--> <function-signature> java.lang.String toBinaryString(java.lang.String,java.lang.Integer) </function-signature> </function> </taglib>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="myFn" uri="/WEB-INF/tld/testFunction.tld"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> ${myFn:toBinaryString(‘10‘,4)} </body> </html>
访问:http://localhost:8090/JspTag/index.jsp
原文:https://www.cnblogs.com/zcjyzh/p/9476264.html