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"?>java

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

  • <!--taglib(标签库)的版本号 -->数据库

  • <tlib-version>2.2.3</tlib-version>
  • <!--缩写,没用到-->express

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

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

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

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

  • <tag>less

  • <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>jsp

  • </taglib>

  • 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规范定义的通讯规则依次调用它的方法。
  • 一、public void setPageContext(PageContext pc), JSP引擎实例化标签处理器后,将调用setPageContext方法将JSP页面的pageContext对象传递给标签处理器,标签处理器之后能够经过这个pageContext对象与JSP页面进行通讯。
  • 二、public void setParent(Tag t),setPageContext方法执行完后,WEB容器接着调用的setParent方法将当前标签的父标签传递给当前标签处理器,若是当前标签没有父标签,则传递给setParent方法的参数值为null。
  • 三、public int doStartTag(),调用了setPageContext方法和setParent方法以后,WEB容器执行到自定义标签的开始标记时,就会调用标签处理器的doStartTag方法。
  • 四、public int doEndTag(),WEB容器执行完自定义标签的标签体后,就会接着去执行自定义标签的结束标记,此时,WEB容器会去调用标签处理器的doEndTag方法。
  • 五、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>

  • **
相关文章
相关标签/搜索