This article mainly describes jsp custom tags and jstl custom functions.
1-processing class
Processing of JSP custom tags
Public class StringTag extends TagSupport {/*****/private static final long serialVersionUID = 1L; private String content; // String content private int count; // Number of strings private String append; public int doStartTag () {try {pageContext. getOut (). print (substring (content, count, append);} catch (IOException e) {e. printStackTrace ();} return SKIP_BODY;} public int doEndTag () {// execute the subsequent jsp page content return EVAL_PAGE;} public static String substring (String text, int length, string append) {if (StringUtils. isBlank (text) | text. length () <length) {return text;} int num = 0, I = 0, len = text. length (); StringBuilder sb = new StringBuilder (); for (; I <len; I ++) {char c = text. charAt (I); if (c> 127) {num + = 2;} else {num ++;} if (num <= length * 2) {sb. append (c) ;}if (num> = length * 2) {break ;}} if (I + 1 <len & StringUtils. isNotBlank (append) {if (text. charAt (I) & gt; 127) {sb. setLength (sb. length ()-1);} else {sb. setLength (sb. length ()-2);} sb. append (append);} return sb. toString ();} // Save the get and set Methods}
JSTL custom function processing class
import org.apache.commons.lang3.StringUtils;public abstract class StringHandler {public static String stringCut(String content, int count) {return stringCut(content, count, null);}public static String stringCut(String content, int count, String replace) {if (StringUtils.isBlank(content) || content.length() < count) {return content;}int num = 0, i = 0, len = content.length();StringBuilder buf = new StringBuilder();for (; i < len; i++) {char c = content.charAt(i);if (c > 127) {num += 2;} else {num++;}if (num <= count * 2) {buf.append(c);}if (num >= count * 2) {break;}}if (i + 1 < len && StringUtils.isNotBlank(replace)) {if (content.charAt(i) > 127) {buf.setLength(buf.length() - 1);} else {buf.setLength(buf.length() - 2);}buf.append(replace);}return buf.toString();}}
2-define tld files
Cus_fn.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 fn library</description><display-name>JSTL fu</display-name><tlib-version>1.1</tlib-version><short-name>fn</short-name><uri>http://www.cnblogs.com/zhouxyx/functionsc</uri><function><name>string_cut</name><function-class>org.zxy.tutorial.d130819.StringHandler</function-class><function-signature>boolean stringCut(java.lang.String,int,java.lang.String)</function-signature></function></taglib>
Utils. tld
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "web-jsptaglibrary_1_2.dtd" ><taglib><tlib-version>utils1.0</tlib-version><jsp-version>2.0</jsp-version><short-name>utils</short-name><uri>http://www.cnblogs.com/zhouxyx/utils</uri><tag><name>str</name><tag-class>org.zxy.tutorial.d130819.StringTag</tag-class><body-content>empty</body-content><attribute><name>content</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute><attribute><name>count</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute><attribute><name>append</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute></tag></taglib>
Next, we will use the defined tags and functions on the jsp page.
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><%@ taglib prefix="u" uri="http://www.cnblogs.com/zhouxyx/utils" %> <%@ taglib prefix="fn" uri="http://www.cnblogs.com/zhouxyx/functionsc" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
Create a servlet and set some simple test data. The effect is as follows:
3-conclusion
If we have some special requirements in the project and the existing methods are difficult to use. You may try to use it from today. All in all, the routines are the same, so it's okay to give a different picture.