JSP可以使用标签 **自定义函数
    1.自己描述一个类
    2.类中描述自己的方法
    方法必须是静态的且通常有返回值(没有返回值无法展示没有意义)
    3.配置一个说明书—-xxx.tld
    当前工程下web文件夹中WEB-INF文件夹下创建一个新的xxx.tld文件
    uri、short-name、很重要
    image.png**

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <taglib xmlns="http://java.sun.com/xml/ns/javaee"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
    5. version="2.1">
    6. <tlib-version>1.0</tlib-version>
    7. <short-name>myFn</short-name>
    8. <uri>http://http://www.duyiedu.com</uri>
    9. <!-- Invoke 'Generate' action to add tags or functions -->
    10. <function>
    11. <name>add</name>
    12. <function-class>myfn.MyFunctions</function-class>
    13. <function-signature>int add(int,int)</function-signature>
    14. </function>
    15. </taglib>

    4.在JSP中声明头信息(我们自己设置的)
    image.png
    运行结果
    image.png
    JSP可以使用标签 **自定义标签
    1.每一个标签都是一个单独的类,创建一个类实现Tag接口,重写里面的方法
    2.细致的看一下这些方法
    两组对应的get/set方法,按需求自己补全,并添加相应的私有属性
    还有一个单独的release方法,回收对象
    doStartTag
    doEndTag
    3.在后面两个方法内添加自己的逻辑**

    1. package mytag;
    2. import javax.servlet.jsp.JspException;
    3. import javax.servlet.jsp.JspWriter;
    4. import javax.servlet.jsp.PageContext;
    5. import javax.servlet.jsp.tagext.Tag;
    6. import java.io.IOException;
    7. public class MyOut implements Tag{
    8. private PageContext pageContext;
    9. @Override
    10. public void setPageContext(PageContext pageContext) {
    11. this.pageContext=pageContext;
    12. }
    13. public PageContext getPageContext() {
    14. return pageContext;
    15. }
    16. private Tag parent;
    17. @Override
    18. public void setParent(Tag tag) {
    19. this.parent=tag;
    20. }
    21. @Override
    22. public Tag getParent() {
    23. return this.parent;
    24. }
    25. private String value;
    26. public String getValue() {
    27. return value;
    28. }
    29. public void setValue(String value) {
    30. this.value = value;
    31. }
    32. @Override
    33. public int doStartTag() throws JspException {
    34. return Tag.EVAL_BODY_INCLUDE;
    35. }
    36. @Override
    37. public int doEndTag() throws JspException {
    38. JspWriter out=this.pageContext.getOut();
    39. try {
    40. out.write(value+" is cute !");
    41. } catch (IOException ioException) {
    42. ioException.printStackTrace();
    43. }
    44. return Tag.EVAL_PAGE;
    45. }
    46. @Override
    47. public void release() {
    48. }
    49. }

    4.描述一个说明书 tld
    先描述uri和short-name,再描述标签及标签内部的属性

    <?xml version="1.0" encoding="ISO-8859-1"?>
    
    <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">
    
        <tlib-version>1.0</tlib-version>
        <short-name>myTag</short-name>
        <uri>http://www.duyiedu.com</uri>
    
        <!-- Invoke 'Generate' action to add tags or functions -->
        <tag>
            <name>out</name>
            <tag-class>mytag.MyOut</tag-class>
            <body-content>JSP</body-content>
            <attribute>
                <name>value</name>
    <!--            属性是否必须有-->
                <required>true</required>
    <!--            是否支持EL表达式-->
                <rtexprvalue>true</rtexprvalue>
            </attribute>
        </tag>
    </taglib>
    

    5.声明头信息
    image.png
    运行结果
    image.png