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>

  • **
相關文章
相關標籤/搜索