java 自定义标签(二)

java

java 自定义标签(二)

2012-03-20 13:49

 继承SimpleTagSupport 类实现 DynamicAttributes接口

主要步骤

1.java类

package MyTag;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.DynamicAttributes;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class Tag2 extends SimpleTagSupport implements DynamicAttributes {
 
  private static final String ATTR_TEMPLATE = "%s='%s'";
  private static final String OPTION_TEMPLATE = "<option value='%1$s'>%1$s</option>";
  private List optionsList;
  private String name;
  private String size;
  private Map<String, Object> tagAttrs = new HashMap<String, Object>();

  public void setName(String name) {
   this.name = name;
  }

  public void setSize(String size) {
   this.size = size;
  }

  public void setOptionsList(List optionsList) {
   this.optionsList = optionsList;
  }

  @Override
  public void doTag() throws JspException, IOException {
   PageContext pageContext = (PageContext) getJspContext();
   JspWriter out = pageContext.getOut();
   out.print("<select ");
   out.print(String.format(ATTR_TEMPLATE, "name", this.name));
   out.print(String.format(ATTR_TEMPLATE, "size", this.size));
   for (String attrName : tagAttrs.keySet()) {
    String attrDefinition = String.format(ATTR_TEMPLATE, attrName, tagAttrs.get(attrName));
    out.print(attrDefinition);
   }
   out.print(">");

   for (Object option : this.optionsList) {

    String optionTag = String.format(OPTION_TEMPLATE, option.toString());
    out.println(optionTag);
   }
   out.println("</select>");
  }

  @Override
  public void setDynamicAttribute(String uri, String name, Object value) throws JspException {

//动态加载标签的其他属性(tld文件未定义属性) 例如 style="width:140px"   name 为style  value 为 width:140px  若存在过个其他属性时

//循环此方法
    tagAttrs.put(name, value);
  }
 }

2.jsp文件

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ page import="java.util.*" %>
     <%@ taglib uri="/mytag2" prefix="formTags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>  
  <%
 List<String> colorList = new ArrayList<String>();
    colorList.add("red");
    colorList.add("blue");
    colorList.add("white");
    request.setAttribute("colorList",colorList);
%>
<form action="" method="post">
 <formTags:select name="color" size="1" optionsList="${requestScope.colorList}" style="width:140px"/>
</form> 
</body>
</html>

3.tld文件

<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3g.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
version="2.0">
 <tlib-version>1.2</tlib-version>
 <jsp-version>1.2</jsp-version>
 <short-name>Forms Taglib</short-name>
 <uri>MyTagLib2</uri>
 <description>
  An example tab library of replacements for the html form tags.
 </description>
 
 <tag>
  <name>select</name>
  <tag-class>MyTag.Tag2</tag-class>
  <body-content>empty</body-content>
 
  <attribute>
   <name>optionsList</name>
   <required>true</required>
   <rtexprvalue>true</rtexprvalue>
   <type>java.util.List</type>
  </attribute>
 
  <attribute>
   <name>name</name>
   <required>true</required>
  </attribute>
 
  <attribute>
   <name>size</name>
   <required>true</required>
  </attribute>
  <dynamic-attributes>true</dynamic-attributes>  //动态加载其他属性 例如 style="width:140px
 </tag>
</taglib>
4.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" >
  <display-name>MyProject</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <jsp-config>
    <taglib>
    <taglib-uri>/mytag2</taglib-uri>
    <taglib-location>/WEB-INF/tlds/tagLib2.tld</taglib-location>
   </taglib>
 
  </jsp-config>
</web-app>

5.效果

以上是 java 自定义标签(二) 的全部内容, 来源链接: utcz.com/z/391321.html

回到顶部