jsp自定义标签中的复选框_在JSP中创建自定义标签

jsp自定义标签中的复选框

To create a Custom Tag the following components are required :

要创建自定义标签,需要以下组件:

  1. The Tag Handler class which should extend SimpleTagSupport.

    Tag Handler类,应该扩展SimpleTagSupport

  2. The Tag Library Descriptor(TLD) file

    标签库描述符(TLD)文件

  3. Use the Custom Tag in your JSP file

    在JSP文件中使用自定义标签

标签处理程序类 (Tag Handler Class)

You can create a Tag Handler class in two different ways:

您可以通过两种不同的方式创建Tag Handler类:

  1. By implementing one of three interfaces : SimpleTag, Tag or BodyTag, which define methods that are invoked during the life cycle of the tag.

    通过实现以下三个接口之一: SimpleTagTagBodyTag ,它们定义在标签的生命周期内调用的方法。

  2. By extending an abstract base class that implements the SimpleTag, Tag, or BodyTag interfaces. The SimpleTagSupport, TagSupport, and BodyTagSupport classes implement the SimpleTag, Tag andBodyTag interfaces . Extending these classes relieves the tag handler class from having to implement all methods in the interfaces and also provides other convenient functionality.

    通过扩展实现SimpleTagTagBodyTag接口的抽象基类。 SimpleTagSupportTagSupportBodyTagSupport类实现SimpleTag,Tag和BodyTag接口。 扩展这些类使标记处理程序类不必在接口中实现所有方法,并且还提供了其他便利的功能。

标签库描述符 (Tag Library Descriptor)

A Tag Library Descriptor is an XML document that contains information about a library as a whole and about each tag contained in the library. TLDs are used by the web container to validate the tags and also by JSP page development tools.

标签库描述符是一个XML文档,其中包含有关整个库以及该库中包含的每个标签的信息。 Web容器使用TLD来验证标签,JSP页面开发工具也使用TLD。

Tag library descriptor file must have the extension .tld and must be packaged in the /WEB-INF/ directory or subdirectory of the WAR file or in the /META-INF/ directory or subdirectory of a tag library packaged in a JAR.

标记库描述符文件必须具有扩展名.tld并且必须打包在WAR文件的/ WEB-INF /目录或子目录中,或打包在JAR中打包的标记库的/ META-INF /目录或子目录中。

自定义标签示例 (Example of Custom Tag)

In our example, we will be creating a Tag Handler class that extends the TagSupport class. When we extend this class, we have to override the method doStartTag(). There are two other methods of this class namely doEndTag() and release(), that we can decide to override or not depending on our requirement.

在我们的示例中,我们将创建一个扩展TagSupport类的Tag Handler类。 扩展此类时,我们必须重写doStartTag()方法。 此类的其他两种方法分别是doEndTag()release() ,我们可以根据需要决定是否重写。

CountMatches.java

CountMatches.java

package com.studytonight.taghandler;

import java.io.IOException;
import javax.servlet.jsp.*;
import org.apache.commons.lang.StringUtils;

public classCountMatches extends TagSupport {
  private String inputstring;
  private String lookupstring;
	     
  public String getInputstring() {
    return inputstring;
  }

  public void setInputstring(String inputstring) {
    this.inputstring = inputstring;
  }

  public String getLookupstring() {
    return lookupstring;
  }

  public void setLookupstring(String lookupstring) {
    this.lookupstring = lookupstring;
  }

  @Override
  public int doStartTag() throws JspException {
    try {
      JspWriter out = pageContext.getOut();
      out.println(StringUtils.countMatches(inputstring, lookupstring));
    } 
    catch (IOException e) {
      e.printStackTrace();
    }
    return SKIP_BODY;
  }
}

In the above code, we have an implementation of the doStartTag() method which is must if we are extending TagSupport class. We have declared two variables inputstring and lookupstring. These variables represents the attributes of the custom tag. We must provide getter and setter for these variables in order to set the values into these variables that will be provided at the time of using this custom tag. We can also specify whether these attributes are required or not.

在上面的代码中,我们有一个doStartTag()方法的实现,如果我们要扩展TagSupport类,则必须这样做。 我们已经声明了两个变量inputstringlookupstring 。 这些变量表示自定义标签的属性 。 我们必须为这些变量提供getter和setter,以便将值设置为这些变量,这些变量将在使用此自定义标记时提供。 我们还可以指定是否需要这些属性。

CountMatchesDescriptor.tld

CountMatchesDescriptor.tld

<?xml version="1.0" encoding="UTF-8"?>
<taglib> 
  <tlibversion>1.0</tlibversion>
  <jspversion>1.1</jspversion>
  <shortname>cntmtchs</shortname>
  <info>Sample taglib for Substr operation</info>
  <uri>http://studytonight.com/jsp/taglib/countmatches</uri>
  
  <tag>
    <name>countmatches</name>
    <tagclass>com.studytonight.taghandler.CountMatches</tagclass>
    <info>String Utility</info>
    <attribute>
       <name>inputstring</name>
       <required>true</required>
    </attribute>
    <attribute>
       <name>lookupstring</name>
       <required>true</required>
    </attribute>
  </tag>

</taglib>

The taglib element specifies the schema, required JSP version and the tags within this tag library. Each tag element within the TLD represents an individual custom tag that exist in the library. Each of these tag should have a tag handler class associated with them.

taglib元素指定模式,必需的JSP版本以及此标记库中的标记。 TLD中的每个标签元素代表库中存在的单个自定义标签。 这些标签中的每一个都应具有与之关联的标签处理程序类。

The uri element represents a Uniform Resource Identifier that uniquely identifies the tag library.

uri元素表示唯一标识标签库的统一资源标识符。

The two attribute elements within the tag element represents that the tag has two attributes and the true value provided to the required element represents that both of these attributes are required for the tag to function properly.

tag元素内的两个属性元素表示该标签具有两个属性,提供给required元素的true值表示这两个属性都是标签正常运行所必需的。

test.jsp

test.jsp

<%@taglib prefix="mytag" uri="/WEB-INF/CountMatchesDescriptor.tld"%>  
<html>
  <mytag:countmatches inputstring="Studytonight" lookupstring="t">
  </mytag:countmatches>
</html>

If this tag works fine it should print a value 3 in the browser as there 't' occurs 3 times in the word 'Studytonight'.

如果此标记正常运行,则应在浏览器中显示值3 ,因为单词'Studytonight'中出现了3次“ t”。

翻译自: https://www.studytonight.com/jsp/creating-a-custom-tag.php

jsp自定义标签中的复选框

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值