JavaWeb—自定义标签练习

练习一:<max:num1="" num2=""/>

<petrelsky:maxTagDemo num1="23" num2="35">

 

练习二:定制一个带有一个属性的标签<xxx:readFile src="">用于输出指定文件的内容

<petrelsky:readFileTagDemo src="WEB-INF/haha.txt">

 

练习一:

创建MaxTag类并实现SimpleTag,添加num1、num2属性和setter方法,在doTag()方法中完成比较和输出。

 

package com.demo.tag;

import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.JspTag;
import javax.servlet.jsp.tagext.SimpleTag;
import java.io.IOException;

public class MaxTag implements SimpleTag {
    private String num1;
    private String num2;

    public void setNum1(String num1) {
        this.num1 = num1;
    }

    public void setNum2(String num2) {
        this.num2 = num2;
    }

    @Override
    public void doTag() throws JspException, IOException {
        int a = 0;
        int b = 0;

        JspWriter out = pageContext.getOut();

        try {
            a = Integer.parseInt(num1);
            b = Integer.parseInt(num2);
            out.print(a > b ? a : b);
        }catch (Exception e){
            out.print("输入的属性格式不正确!!!");
        }

    }

    @Override
    public void setParent(JspTag jspTag) {

    }

    @Override
    public JspTag getParent() {
        return null;
    }

    private PageContext pageContext;

    @Override
    public void setJspContext(JspContext jspContext) {
            this.pageContext = (PageContext) jspContext;
    }

    @Override
    public void setJspBody(JspFragment jspFragment) {

    }
}

 

在mytld.tld中进行标签的相关配置

<tag>
        <name>max</name>
        <tag-class>com.demo.tag.MaxTag</tag-class>
        <body-content>empty</body-content>

        <attribute>
            <name>num1</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>

        <attribute>
            <name>num2</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>

  

在jsp页面引用标签,首先需要导入标签

<%@taglib prefix="yhs" uri="http://mycompany.com" %>
<yhs:max num1="${param.a}" num2="${param.b}"></yhs:max> 

 效果图:

 

通常情况下开发简单标签直接继承SimpleTagSupport就可以了,可以直接调用其对应的getter方法的到 对应API

public class SimpleTagSupport implements SimpleTag {
    private JspTag parentTag;
    private JspContext jspContext;
    private JspFragment jspBody;


    public void doTag() throws JspException, IOException {
    }

    public void setParent(JspTag parent) {
        this.parentTag = parent;
    }

    public JspTag getParent() {
        return this.parentTag;
    }

    public void setJspContext(JspContext pc) {
        this.jspContext = pc;
    }

    protected JspContext getJspContext() {
        return this.jspContext;
    }

    public void setJspBody(JspFragment jspBody) {
        this.jspBody = jspBody;
    }

    protected JspFragment getJspBody() {
        return this.jspBody;
    }

   
}

  

练习二:

创建ReadFileTag类继承SimpleTagSupport类,重写doTag()方法,添加src属性

 

package com.demo.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ReadFileTag extends SimpleTagSupport {
   //相对于当前web应用的根路径额文件名
    private String src;

    public void setSrc(String src) {
        this.src = src;
    }

    @Override
    public void doTag() throws JspException, IOException {
        PageContext pageContext = (PageContext) getJspContext();
        InputStream is = pageContext.getServletContext().getResourceAsStream(src);

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String str = null;
        while ((str = reader.readLine())!=null){
            pageContext.getOut().write(str);
            pageContext.getOut().write("<br>");
        }
    }
}

 

  

在读取时标签必须转换为特定的编码符号

str = Pattern.compile("<").matcher(str).replaceAll("&lt");

str = Pattern.compile(">").matcher(str).replaceAll("&gt");

 

在mytld.tld中进行标签的相关配置

   <tag>
        <name>readfile</name>
        <tag-class>com.demo.tag.ReadFileTag</tag-class>
        <body-content>empty</body-content>

        <attribute>
            <name>src</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>

  

在jsp页面引用标签,首先需要导入标签

<%@taglib prefix="yhs" uri="http://mycompany.com" %>
<yhs:readfile src="/WEB-INF/note.txt"></yhs:readfile>

  

效果图:

转载于:https://www.cnblogs.com/yangHS/p/11177542.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值