Struts2 自定义下拉框标签Tag分享


自定义标签主要包括三个步骤:

1、编写java类,继承TagSupport类;

2、创建tld文件,影射标签名和标签的java类;

3、jsp页面引入tld。

例子:自定义下拉框标签

如果页面上有下拉选择框,通常最好的解决方法是使用数据字典,因为有可能多个页面

使用同一个下拉框,便于后台统一维护。

自定义Tag类

 import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport;  public class DictionaryOptionTaget extends TagSupport {     private static final long serialVersionUID = 1L;     private String index; // 字段索引 ,页面上通过标签属性传回来的值      @SuppressWarnings("unchecked")     @Override     public int doEndTag() throws JspException {         JspWriter jspw = this.pageContext.getOut();          StringBuffer options = new StringBuffer();          /**          * 需要查询数据库 字段索引为SEX的option内容,这里是写死          */         if ("SEX".equals(index)) {             options.append("
"); options.append(""); options.append(""); } try { jspw.println(options); //输出 } catch (IOException e) { e.printStackTrace(); } return 0; } @Override public int doStartTag() throws JspException { return 0; } public String getIndex() { return index; } public void setIndex(String index) { this.index = index; } }

定义tld

 <code class="language-xml hljs "><!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%2D%2D%3E-->  <taglib>     <tlib-version>1.0</tlib-version>     <jsp-version>1.2</jsp-version>     <short-name>tagSample</short-name>     <uri>/hellotag</uri>      <tag><!--{cke_protected}{C}%3C!%2D%2D%20%E4%BB%8E%E6%95%B0%E6%8D%AE%E5%AD%97%E5%85%B8%E6%A3%80%E5%87%BA%E4%B8%80%E4%B8%AAoption%E5%88%97%E8%A1%A8%20%2D%2D%3E-->         <name>OptionDictionary</name>         <tag-class>             com.itmyhome.DictionaryOptionTaget         </tag-class>         <body-content>empty</body-content>         <attribute>             <name>index</name><!--{cke_protected}{C}%3C!%2D%2D%20%E5%AD%97%E6%AE%B5%E7%B4%A2%E5%BC%95%E5%90%8D%20%2D%2D%3E-->             <required>true</required><!--{cke_protected}{C}%3C!%2D%2D%20%E6%98%AF%E5%90%A6%E5%BF%85%E5%A1%AB%20%2D%2D%3E-->             <rtexprvalue>false</rtexprvalue><!--{cke_protected}{C}%3C!%2D%2D%20%E6%98%AF%E5%90%A6%E8%83%BD%E5%A4%9F%E4%BB%A5%24%7B%7D%E6%96%B9%E5%BC%8F%E4%BC%A0%E5%80%BC%20%2D%2D%3E-->         </attribute>     </tag>  </taglib></code>

需要注意的是:true 时候,可以使用JSP表达式

表示该自定义标签的属性值可以使用 ${} 方式动态传值。

使用自定义的标签

 <code class="language-jsp hljs xml"><%@ taglib uri="/WEB-TAG/platForm.tld" prefix="PF"%>  <select>       </select></code>

页面输出:

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/jspttutorial/111992.html

(0)
上一篇 2020年5月8日
下一篇 2020年5月8日

精彩推荐