javaweb学习总结———jsp自定义标签与el表达式结合使用

  • 第一种方式(标签签到el表达式)实现从内存取数据,通过key获取value:
  • 一.编写一个实现Tag接口的Java类或者继承TagSupport 等...tag好多实现类,都可以继承实现(标签处理器类),此种方式可以获取到request对象,对于需要从内存中获取数据方便。
  • jsp中引用方式如下:
  • <input type="text" name="11" value="<ui:getName value='${user.roleid}'/>"></input>
  • 1.写一个tld文件(此文件需要放在web功能的WEB-INF目录下),jsp中需引入,<%@ taglib uri="/WEB-INF/right-tags.tld" prefix="ui"%>,此文件是jsp与服务器连接的桥梁, prefix="ui" 为jsp所用前缀标识
  • right-tags.tld 文件内容
  • <?xml version="1.0" encoding="UTF-8"?>

  • <!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>2.2.3</tlib-version>
  • <!--缩写,没用到-->

  • <short-name>s</short-name>
  • <!--

  •     为自定义标签库设置一个uri,uri以/开头,/后面的内容随便写,如这里的/gacl ,
    
  •     在Jsp页面中引用标签库时,需要通过uri找到标签库
    
  •     在Jsp页面中就要这样引入标签库:<%@taglib uri="/gacl" prefix="gacl"%>
    
  •  -->
    
  • <uri>/right-tags</uri>
  • <!-- description用来添加对taglib(标签库)的描述 -->

  • <description>孤傲苍狼开发的自定义标签库</description>
  • <!--一个taglib(标签库)中包含多个自定义标签,每一个自定义标签使用一个tag标记来描述 -->

  • <!-- 一个tag标记对应一个自定义标签 -->

  • <tag>

  • <name>getName</name>//
    
  • <tag-class>com.maystar.tag.TestTag</tag-class>
    
  • <body-content>JSP</body-content>
    
  • <description><![CDATA[validate the element can be show ]]></description>
    
  • <attribute>
    
  •   <name>value</name>
    
  •   <required>true</required>
    
  •   <rtexprvalue>true</rtexprvalue>
    
  • </attribute>
    
  • </tag>

  • </taglib>

    扫描二维码关注公众号,回复: 129115 查看本文章
  • 2.java处理器类:
  • public class TestTag extends TagSupport{
  • public static Logger logger = Logger.getLogger(TestTag.class.getName());
  • private static final long serialVersionUID = 1L;
  • //接收传递进来的PageContext对象
  • private static PageContext pageContext;
    
  • private String value;
    
  • public void setValue(String value) {
  •   this.value = value;
    
  • }
  • @Override
    
  • public int doStartTag() throws JspException {
    
  •     HttpServletRequest request =(HttpServletRequest) pageContext.getRequest();
    
  •   Map map = (Map)request.getSession().getServletContext().getAttribute("MAP_ROLE");
    
  •   String str1=Convert.trimNull(map.get(value));
    
  •     JspWriter out = pageContext.getOut();
    
  •     try {
    
  •         //这里输出的时候会抛出IOException异常
    
  •         out.write(str1);
    
  •     } catch (IOException e) {
    
  •         //捕获IOException异常后继续抛出
    
  •         throw new RuntimeException(e);
    
  •     }
    
  •     return 0;
    
  • }
    
  • @Override
    
  • public int doEndTag() throws JspException {
    
  •     System.out.println("调用doEndTag()方法");
    
  •     return 0;
    
  • }
    
  • @Override
    
  • public TagSupport getParent() {
    
  •     return null;
    
  • }
    
  • @Override
    
  • public void release() {
    
  •     System.out.println("调用release()方法");
    
  • }
    
  • @Override
    
  • public void setPageContext(PageContext pageContext) {
    
  •     System.out.println("setPageContext(PageContext pageContext)");
    
  •     this.pageContext = pageContext;
    
  • }
    
  • public void setParent(TagSupport arg0) {
    
  • }
    
  • }
  • 自定义标签的执行流程
  •   JSP引擎遇到自定义标签时,首先创建标签处理器类的实例对象,然后按照JSP规范定义的通信规则依次调用它的方法。
  • 1、public void setPageContext(PageContext pc), JSP引擎实例化标签处理器后,将调用setPageContext方法将JSP页面的pageContext对象传递给标签处理器,标签处理器以后可以通过这个pageContext对象与JSP页面进行通信。
    
  • 2、public void setParent(Tag t),setPageContext方法执行完后,WEB容器接着调用的setParent方法将当前标签的父标签传递给当前标签处理器,如果当前标签没有父标签,则传递给setParent方法的参数值为null。
    
  • 3、public int doStartTag(),调用了setPageContext方法和setParent方法之后,WEB容器执行到自定义标签的开始标记时,就会调用标签处理器的doStartTag方法。
    
  • 4、public int doEndTag(),WEB容器执行完自定义标签的标签体后,就会接着去执行自定义标签的结束标记,此时,WEB容器会去调用标签处理器的doEndTag方法。
    
  • 5、public void release(),通常WEB容器执行完自定义标签后,标签处理器会驻留在内存中,为其它请求服务器,直至停止web应用时,web容器才会调用release方法。
    
  • 第二种方式(el表达式嵌套标签)实例代码为取值得长度:
  • 一.编写一个普通标签处理器类,此种方式获取不到request对象,适用于前台需要转化编码方式或者时间类型,或者需要从缓存数据库中取数据等情形, jsp中引用方式如下:
    1. jsp页面中引入方式
  • <input type="text" name="11" value="${ui: getStringLength (user.username)}"></input>
  • <%@ taglib prefix="test" uri="/test-1.0"%>
  • 2.编写一个tlb文件内容如下,说明如下,这段tlb代码可以合并到第一种方法的tld文件里面,共用一个。
  • <?xml version="1.0" encoding="UTF-8" ?>

  • <!--

  • Copyright 2004 The Apache Software Foundation
  • Licensed under the Apache License, Version 2.0 (the "License");
  • you may not use this file except in compliance with the License.
  • You may obtain a copy of the License at
  • http://www.apache.org/licenses/LICENSE-2.0
  • Unless required by applicable law or agreed to in writing, software
  • distributed under the License is distributed on an "AS IS" BASIS,
  • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • See the License for the specific language governing permissions and
  • limitations under the License.
  • -->
  • <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
  • xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  • xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
  • version="2.0">
  • <description>A tag library exercising function handlers.</description>注意:该属性是可选择,属性,为了对该标签库的描述
  • <tlib-version>1.0</tlib-version>注意:这里是必选值,否则,调用出错。
  • <short-name>test</short-name>注意,该属性是可选择属性,因为在jsp页面定义了前缀,这里在定义一个,简略名,不知是什么意思。
  • <uri>/test-1.0</uri>注意:这里定义的路径,表示,jsp找到该标签的唯一路径
  • <function>

  • <description>测试: 与 el 结合使用 如  ${test:getStringLength("XX")}</description>
    
  • <name>getStringLength</name> 注意,这里,定义的是,标签对应的函数名
    
  • <function-class>taglib.StringUtils</function-class>注意:这里,给出了,该类,所在的路径,全路径
    
  • <function-signature>java.lang.Integer getStringLength(java.lang.String)</function-signature> 
    
  • </function>

  • </taglib>

  • 3.java类
  • public class StringUtils {
  • private static Logger logger = Logger.getLogger(StringUtils.class);
  • public static Integer getStringLength(String str){ 注意:这里的方法,必须都是静态的,并且,应该是public的
  • logger.info("输入值:" + str);
  • if(str == null){
  • return 0;
  • }
  • return str.length();
  • }
  • }
    1. web.xml中声明该标签,可以,声明,也可以不用,声明,因为,默认的,会去找/WEB-INF 下的,所有tld,标签库描述文件,然后自动加载
  • <jsp-config>

  • <taglib>

  • <taglib-uri>/test-1.0</taglib-uri>
  • <taglib-location>test-1.0.tld</taglib-location>
  • </taglib>

  • </jsp-config>

  • **

猜你喜欢

转载自my.oschina.net/u/1054538/blog/741108