如何做EL表达式能调用的函数-小例子(转)
先定义需要用el调用函数所在的类:
注意el所调用的方法必须是static的!
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文件如下:
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文件中使用了:
首先导入标签库:
Java代码
- <%@taglib prefix="qs" uri="http://www.qingsoft.net/el/function" %>
下面看如何使用:
Java代码
- <%
- Userinfo userinfo = new Userinfo();
- userinfo.setUsername("admin");
- request.setAttribute("userinfo",userinfo);
- %>
- ${qs:check(userinfo)}