首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

关于定制JSTL标签的有关问题

2012-01-13 
关于定制JSTL标签的问题我第一次定制一个JSTL标签,并写了个JSP页面测试首先写了一个JSTL标签处理器类:pack

关于定制JSTL标签的问题
我第一次定制一个JSTL标签,并写了个JSP页面测试

首先写了一个JSTL标签处理器类:
package   com.iven.tag;

import   javax.servlet.jsp.tagext.*;
import   javax.servlet.jsp.*;

public   class   TempChangeTag   extends   BodyTagSupport
{
        protected   String   to= "Celsius ";

        public   void   setTo(String   to)
        {
                this.to=to;
        }
        public   String   getTo()
        {
                return   this.to;
        }

        public   int   doAfterBody()
        {
                try
                {
                        BodyContent   bc=this.getBodyContent();
                        JspWriter   out=bc.getEnclosingWriter();
                        String   bd=bc.getString();
                        String   fa=String.valueOf((Integer.parseInt(bd)+112));
                        String   ke=String.valueOf((Integer.parseInt(bd)+273));

                        if(to.equals( "Fahrenheit ")==true   ||   to.equals( "F ")==true)
                        {
                                out.print(fa);
                        }
                        else   if(to.equals( "Kelvin ")==true   ||   to.equals( "K ")==true)
                        {
                                out.print(ke);
                        }
                        else
                        {
                                out.print(bd);
                        }
                }
                catch(Exception   e)
                {
                        try
                        {
                                  this.getBodyContent().getEnclosingWriter().print( "出错了! "+ " <br/> "+e);


                        }
                        catch(Exception   e1)
                        {
                                e1.printStackTrace();
                        }

                }
                return   EVAL_PAGE;

        }
}

将这个类放到了/WEB-INF/classes/com/iven/tag/目录下;
----------------------------
之后就开始写一个TLD文件:

<?xml   version= "1.0 "   encoding= "ISO-8859-1 "?>
<!DOCTYPE   taglib
        PUBLIC   "-//sun   Microsystems,   Inc.//DTD   JSP   Tag   Library   1.1//EN "
        "http://java.sun.com/j2ee/dtds/web_jsptaglibrary_1_1.dtd ">

<taglib>
        <tlibversion> 1.0 </tlibversion>
        <jspversion> 2.0 </jspversion>
        <shortname> switchtemp </shortname>


        <tag>
                <name> temp </name>
                <tagclass>
                        com.iven.tag.TempChangeTag
                </tagclass>
                <bodycontent> JSP </bodycontent>
                <attribute>
                        <name> to </name>
                        <required> false </required>
                </attribute>
        </tag>
</taglib>

然后将这个名为TempChe.tld文件放到WEB-INF目录下;

------------------------------------------------------

最后配置web.xml文件:
<taglib>
        <taglib-uri> /test/switchtemp </taglib-uri>
        <taglib-location> /WEB-INF/TempChe.tld </taglib-location>
</taglib>

我的测试JSP文件是:

<%@   page   contentType= "text/html;   charset=gb2312 "   language= "java "   import= "java.sql.*,com.iven.pub.Str "   errorPage= " "   %>

<%@   taglib   uri= "/test/switchtemp "   prefix= "temp_st "%>
下列代码将100摄氏度转换为212华氏度: <br/>
1:   <temp_st:temp   to= "Fahrenheit "> 100 </temp_st:temp> <br/>
2:   <temp_st:temp   to= "F "> 100 </temp_st:temp>

<br/> <br/>
下列代码将100摄氏度转换为373开氏度: <br/>
1:   <temp_st:temp   to= "Kelvin "> 100 </temp_st:temp> <br/>
2:   <temp_st:temp   to= "K "> 100 </temp_st:temp>


3:   <temp_st:temp> 100 </temp_st:temp>

---------------------------------------------------

结果报错:
Unable   to   initialize   TldLocationsCache:   XML   parsing   error   on   file   /WEB-INF/TempChe.tld

谁能告诉我怎么解决啊?哪儿有错?谢谢!

[解决办法]
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN "
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd ">

热点排行