zoukankan      html  css  js  c++  java
  • Expression Language Function

    
    

    java code:

    package test.el.function;
    
    public class ArithmaticOP {
    
    	public static int AddInt(int a,int b){
    		return a+b;
    	}
    }
    

    mytags.tld

    <?xml version="1.0" encoding="UTF-8"?>
    <taglib version="2.1" 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 web-jsptaglibrary_2_1.xsd">
     <tlib-version>1.0</tlib-version>
     <short-name>mytags</short-name>
     <function>
      <name>greet</name>
      <function-class>test.el.function.Functions</function-class>
      <function-signature>java.lang.String sayHello()</function-signature>
     </function>
     <function>
      <name>AddInt</name>
      <function-class>test.el.function.ArithmaticOP</function-class>
      <function-signature>int AddInt(int ,int )</function-signature>
     </function>
    </taglib>
    

    web.xml配置

     <jsp-config>
      <taglib>
       <taglib-uri>/myTags</taglib-uri>
       <taglib-location>/WEB-INF/mytags.tld</taglib-location>
      </taglib>
     </jsp-config>
    
    </web-app>
    

    使用:

     <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ taglib uri="/myTags" prefix="mf" %>

    //....

      ${mf:AddInt(11,221) }
      </body>
    </html>

    Expression-Language Functions vs. Custom Tags
    As you’ll see in later chapters, the JSP specification provides a powerful custom tag mechanism.
    You might ask why you would use tags over functions and vice versa. There are several
    factors that can help you to make the choice:
    • Is knowledge of the environment required? If the answer is yes, tags are the way to go. A
    tag provides easy access to pageContext and other variables; functions do not. To access
    these implicit objects within a function, you must pass them in as a parameter.

    Do you require iterative behavior over a body? If the answer is yes, you should use a tag.
    Functions do not provide functionality to process a body (they don’t have one), whereas
    tags do.
    • Are you trying to provide a small, reusable piece of functionality that acts on one or
    more arguments? If the answer to this is yes, you should use a function. Overall, functions
    are much simpler to write than tags; therefore, they provide a great opportunity to
    write small, self-contained pieces of functionality.
    • Would you like to reuse existing Java code in a web context? If the answer is yes, functions
    are ideal. Because functions are no more than static Java methods, you can easily
    reuse existing code.
    The choice of tags versus functions should be eased by consulting these points, but
    it’s worth noting that the true power of the EL becomes evident when it’s combined with custom
    tags.

  • 相关阅读:
    SQL 07: 外连接 (左连接和右连接查询)
    010 利用多线程使socket服务端可以与多个客户端同时通讯
    056 文件修改的两种方式
    009 模拟一个简单抢票小程序代码
    055 文件的高级应用
    054 with管理文件操作上下文
    008 通过开启子进程的方式实现套接字服务端可以并发的处理多个链接以及通讯循环(用到了subprocess模块,解决粘包问题)
    053 文件的三种打开模式
    052 绝对路径和相对路径
    051 基本的文件操作
  • 原文地址:https://www.cnblogs.com/wucg/p/1997890.html
Copyright © 2011-2022 走看看