先定義須要用el調用函數所在的類:
注意el所調用的方法必須是static的!java
Java代碼
- package com.qingsoft.el.function;
-
- public class CheckUsername {
-
- public static boolean check(Userinfo userinfo)
- {
- if(userinfo.getUsername().equals("admin"))
- {
- return true;
- }
- return false;
- }
- }
接下來定義tld文件以下:web
XML/HTML代碼
- <?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.1 core library</description>
- <display-name>JSTL core</display-name>
- <tlib-version>1.1</tlib-version>
- <short-name>qsfun</short-name>
- <uri>http://www.qingsoft.net/el/function</uri>
-
- <function>
- <description>check username</description>
- <name>check</name>
- <function-class>com.qingsoft.el.function.CheckUserName</function-class>
- <function-signature>boolean check(com.qingsoft.vo.Userinfo)</function-signature>
- </function>
-
- </taglib>
下面就能夠在jsp文件中使用了:jsp
首先導入標籤庫:函數
Java代碼
- <%@taglib prefix="qs" uri="http://www.qingsoft.net/el/function" %>
下面看如何使用: spa
Java代碼
- <%
- Userinfo userinfo = new Userinfo();
- userinfo.setUsername("admin");
- request.setAttribute("userinfo",userinfo);
- %>
-
- ${qs:check(userinfo)}