struts自定义标签

一、类文件IconTag.java extends TagSupport

package com.cybersearch.taglib;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Logger;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/** 
* TagSupport与BodyTagSupport的区别: 
* 主要看标签处理类是否要读取标签体的内容和改变标签体返回的内容,如果不需要就用TagSupport,否则就用BodyTagSupport 
* 用TagSupport实现的标签,都可以用BodyTagSupport来实现,因为BodyTagSupport继承了TagSupport  
*/ 
public class IconTag extends TagSupport {
private static final Logger logger = Logger.getLogger(IconTag.class.getName());
private String value;

public void setValue(String value) {
this.value = value;
}
@Override
public int doEndTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.print(changeNameCode(value));
} catch (IOException e) {
logger.info("自定义标签 IconTag错误");
e.printStackTrace();
}
return EVAL_PAGE;
}

@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
return super.doStartTag();
}
    public String changeNameCode(String name){
    String nameCode = name;
    if(nameCode!=null&&!"".equals(nameCode)){
    //InputStream in = new BufferedInputStream(new FileInputStream("iconTag.properties"));
    //InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("iconTag.properties");   
    Properties p = new Properties();
    try {
       p.load(new BufferedInputStream(new FileInputStream("C:/Tomcat5/webapps/cybersearch2008-10-07/WEB-INF/iconTag.properties")));   
      } catch (IOException e1) {   
       e1.printStackTrace();   
      }   
    //System.out.println("ip:"+p.getProperty("ip")+",port:"+p.getProperty("port"));
      String[] listCode = p.getProperty("list").split(",");
      for(int i=0;i<listCode.length;i++){
      try{
      if(p.getProperty(listCode[i])!=null){
      nameCode = nameCode.replaceAll(listCode[i], "<img src='/images/icon/"+p.getProperty(listCode[i])+"'>");
      }
      }catch(Exception dd){
      System.out.println("p.getProperty error");
      }
      }
      return nameCode.replaceAll(",", " ");
    }else{
    return "";
    }
   
    }

}


二、tld文件
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>mytag</short-name>
<!--StringTag-->
<tag>
<name>string</name>
<tag-class>com.cybersearch.taglib.StringTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>value</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>  
      <name>icon</name>
      <tag-class>com.cybersearch.taglib.IconTag</tag-class>
      <body-content>empty</body-content>
      <attribute>
<name>value</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
   </tag>  
</taglib>

三、在页面上的使用
<mytag:icon value="${name }"/>

你可能感兴趣的:(java,Web,jsp,struts,servlet)