Custom El Functions

Source: Internet
Author: User

In the Jstl package, Url:http://java.sun.com/jsp/jstl/functions is a self-contained El function library, and if that doesn't meet your needs, you can write your own custom El function yourself.

Write an escaped El function 1th step, write aStatic MethodsJava class (El can only call static methods)

//http Escape Code

Public class HTTPFilter

{

Public static String Filter (Stringmessage)

{

if (Message = =null)

return (null);

char content[] =new char[Message.length ()];

Message.getchars (0,message.length (), content, 0);

Stringbufferresult = newstringbuffer (content. Length + 50);

for (int i = 0; i < content.) length; i++)

{

Switch (Content[i])

{

Case ' < ':

Result.append ("&lt;");

break;

Case ' > ':

Result.append ("&gt;");

break;

Case ' & ':

Result.append ("&amp;");

break;

Case ' "':

Result.append ("&quot;");

break;

default:

Result.append (Content[i]);

}

}

return (Result.tostring ());

}

}

2nd step, in web- inf\ directory to create a new TLDFile Zhong.tld, describing the function

<? 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/j2eehttp://java.sun.com/xml/ns/j2ee/ Web-jsptaglibrary_2_0.xsd "

version="2.0">

< Description > Custom E-functions </description>

< tlib-version >1.0</tlib-version>

< Short-name > Simpletaglibrary</short-name>

< URI >/zhong</uri>

< function >

< name >filter</name>

< Function-class >cn.zhong.web.el.HttpFilter</function-class>

< function-signature >java.lang.String Filter (java.lang.String)</function-signature> <!-- return value parameter type --

<!--<function-signature>java.lang.string Filter (java.lang.String,int[],java.lang.string[], Java.untils.List) </function-signature>-->

</ function >

</ taglib >

3rd step, use on JSP page

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>

<%@tagliburi= "/web-inf/itcast.tld" prefix= "Myfn"%>

<! DOCTYPE HTML Public "-//w3c//dtd HTML4.01 transitional//en" >

<title> Custom El function Testing </title>

<body>

${myfn:filter ("<a href= ' > Dot </a>")}

</body>

Custom El Functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.