<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                > 第二部分簡單講解:主要講解el表達式,核心標簽庫。本章主要講解:自定義標簽庫;404頁面,505頁面,錯誤頁面配置方法 全部代碼下載:[鏈接](http://download.csdn.net/detail/peace1213/9303561) ### 1.JSP自定義標簽: 自定義標簽是用戶定義的JSP語言元素。當JSP頁面包含一個自定義標簽時將被轉化為servlet,標簽轉化為對被 稱為tag handler的對象的操作,即當servlet執行時Web container調用那些操作。JSP標簽擴展可以讓你創建新的標簽并且可以直接插入到一個JSP頁面。 JSP 2.0規范中引入Simple Tag Handlers來編寫這些自定義標記。你可以繼承SimpleTagSupport類并重寫的doTag()方法來開發一個最簡單的自定義標簽。 ### 2.開發自定義標簽 下面的步驟建立一個自定義標簽用于戰術客戶端的ip地址: 1. 編寫一個普通的java類,繼承SimpleTagSupport類 ~~~ public class ShowIp extends SimpleTagSupport { /** * 以下屏蔽的代碼在SimpleTagSupport代碼中已經做了!這里不需要重復再做! */ /*private JspContext context; *//** * 傳入pageContext *//* @Override public void setJspContext(JspContext pc) { this.context = pc; }*/ @Override public void doTag() throws JspException, IOException { PageContext pageContext=(PageContext)this.getJspContext(); ServletRequest request = pageContext.getRequest(); String ip=request.getRemoteHost(); JspWriter out = pageContext.getOut(); out.write("使用自定義標簽展示客戶ip地址"+ip); List<String> a=null; } } ~~~ 2.在web項目的WEB-INF目錄下建立mytaglib.tld文件,這個tld叫標簽庫的聲明文件。(參考核心標簽庫的tld文件) ~~~ <?xml version="1.0" encoding="UTF-8" ?> <taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" version="2.1"> <description>A tag library exercising SimpleTag handlers.</description> <!-- 標簽庫的版本 --> <tlib-version>1.0</tlib-version> <!-- 標簽庫前綴 --> <short-name>rlovep</short-name> <!-- tld文件的唯一標記 --> <uri>http://rlovep.com</uri> <!-- 定義標簽,標簽要放在方法前面 --> <tag> <!-- 標簽名 --> <name>showIp</name> <!-- 標簽處理類 --> <tag-class>com.rlovep.tags.ShowIp</tag-class> <body-content>empty</body-content> </tag> <tag> </taglib> ~~~ 3.在jsp頁面的頭部導入自定義標簽庫:url為你在tld中寫的url,前綴也是你在tld文件中定義的 ~~~ <%@ taglib uri="http://rlovep.com" prefix="rlovep" %> ~~~ 4.在jsp中使用自定義標簽 ~~~ <%-- 測試簡單的自定義標簽,標簽體(我是你)不顯示 --%> <rlovep:showIp>我是你 </rlovep:showIp> ~~~ ### 3.自定義標簽的執行過程 當訪問:[http://localhost:8080/stuJsp/Hellotags.jsp](http://localhost:8080/stuJsp/Hellotags.jsp) 時;要重啟Tomcat使服務器啟動時,加載每個web應用的WEB-INF目錄下的所有文件!!!例如。web.xml, tld文件!!! 步驟如下: 1. 檢查jsp文件的taglib指令,是否存在一個url為[http://rlovep.com](http://rlovep.com)的tld文件。如果沒有,則報錯。 2. 執行jsp文件的轉化:把jsp文件翻譯成java源文件->編譯class->構造類對象->調用_jspService()方法 3. 讀到到mytaglib.tld文件中查詢是否存在為showIp的標簽 4. 找到對應的標簽,則讀到內容,得到com.rlovep.tags.ShowIp 5. 構造ShowIp對象,然后調用ShowIp里面的方法:dotag方法; ### 4.訪問標簽體 你可以像標準標簽庫一樣在標簽中包含消息內容。如我們要在我們自定義的中包含內容 1. 格式如下: ~~~ <rlovep:showIp>我是你 </rlovep:showIp> ~~~ 2.但要文字顯示需要修改處理類和tld文件: 修改處理類在doTag方法中增加如下內容: ~~~ JspContext jspContext2 = this.getJspContext(); //顯示標簽體的兩種方法 //方法1直接調用 //jspBody.invoke(null); //方法2通過輸出到out //jspBody.invoke(jspContext2.getOut()); ~~~ 修改tld文件: ~~~ <tag> <!-- 標簽名 --> <name>showIp</name> <!-- 標簽處理類 --> <tag-class>com.rlovep.tags.ShowIp</tag-class> <!-- 輸出標簽體的內容格式標簽體不可以寫jsp的java代碼 --> <body-content>scriptless</body-content> </tag> ~~~ 3.現在你可以將標簽體的內容顯示了; ~~~ <%-- 標簽提會顯示 --%> <rlovep:showIp>我是你 </rlovep:showIp> ~~~ 4.輸出標簽體的內容格式: ~~~ JSP: 在傳統標簽中使用的。可以寫和執行jsp的java代碼。 scriptless: 標簽體不可以寫jsp的java代碼 empty: 必須是空標簽。 tagdependent : 標簽體內容可以寫jsp的java代碼,但不會執 ~~~ ### 5.給標簽體帶屬性: 你可以在自定義標準中設置各種屬性,要接收屬性,值自定義標簽類必須實現setter方法; 1.格式如下: ~~~ <!-- 測試帶屬性的標簽,標簽體顯示通過類處理 --> <rlovep:AttributeTags name="peace" value="12345 ~~~ 2.定義屬性步驟如下: 編寫處理類:AttributeTags extends SimpleTagSupport ~~~ 添加倆個屬性: //聲明屬性的成員變量 private Integer value; private String name; 并為兩個成員屬性寫setter方法; public void setValue(Integer value) public void setName(String name) ~~~ 在標簽庫文件tld注明此標簽和屬性: ~~~ <!-- 標簽名 --> <name>AttributeTags</name> <!-- 標簽處理類 --> <tag-class>com.rlovep.tags.AttributeTags</tag-class> <!-- 輸出標簽體的內容格式標簽體不可以寫jsp的java代碼 --> <body-content>scriptless</body-content> <!-- 配置屬性name --> <attribute> <name>name</name> <!-- 是否必填 --> <required>true</required> <!-- 是否支持EL表達式 --> <rtexprvalue>true</rtexprvalue> </attribute> <!-- 配置屬性value --> <attribute> <name>value</name> <!-- 是否必填 --> <required>true</required> <!-- 是否支持EL表達式 --> <rtexprvalue>true</rtexprvalue> </attribute> </tag> ~~~ 3.現在就可以用帶屬性的標簽了 ~~~ <!-- 測試帶屬性的標簽,標簽體顯示通過類處理 --> <rlovep:AttributeTags name="peace" value="123456"> ~~~ 4.在tld配置屬性時你可以配置下面的屬性: ![12](https://box.kancloud.cn/2016-04-13_570e001367883.png "") ### 6.帶有子標簽的自定義標簽: 就像核心標簽庫的choose標簽一樣我們也可以定義嵌套的自定義標簽,這部分我們主要講解自己創建一個類似核心標簽庫的choose標簽。步驟如下: 1.建立處理類,處理類還是與前面一樣的方法。需要介紹的是用到了一個getParent()方法,從名字上就可以知道是為了獲得父標簽,對就是獲得父標簽類; 建立三個處理類文件: ChooseTag,OtherWiseTag,whenTag ~~~ //ChooseTag類: public class ChooseTag extends SimpleTagSupport{ //此去時變量不是標簽屬性,由when標簽更改;othewise獲得; private boolean flag; public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; } @Override public void doTag() throws JspException, IOException { // Choose標簽作用顯示標簽體,以及作為其他兩個標簽的父標簽; getJspBody().invoke(null); } } //whenTag類 public class whenTag extends SimpleTagSupport{ //增加test屬性 private boolean test; public boolean isTest() { return test; } public void setTest(boolean test) { this.test = test; } @Override public void doTag() throws JspException, IOException { //如果標簽屬性為true,顯示標簽體 if(test){ getJspBody().invoke(null); } //設置父標簽給otherwise用 ChooseTag parent=null; if(getParent() instanceof ChooseTag){ parent=(ChooseTag)getParent(); parent.setFlag(test); } } } //OtherWiseTag類: public class OtherWiseTag extends SimpleTagSupport { @Override public void doTag() throws JspException, IOException { boolean test=true; //獲取父標簽的test,由他的上一個when設置 if(getParent() instanceof ChooseTag) { //獲取父標簽的test,由他的上一個when設置 ChooseTag parent=(ChooseTag)getParent(); test=parent.isFlag(); } if(!test){ getJspBody().invoke(null); } } } ~~~ 2.編寫tld文件:與其他的標簽定義一模一樣 ~~~ <!-- 定義標簽,choose--> <tag> <!-- 標簽名 --> <name>choose</name> <!-- 標簽處理類 --> <tag-class>com.rlovep.tags.ChooseTag</tag-class> <!-- 輸出標簽體的內容格式標簽體不可以寫jsp的java代碼 --> <body-content>scriptless</body-content> </tag> <!-- 定義標簽,when--> <tag> <!-- 標簽名 when --> <name>When</name> <!-- 標簽處理類 --> <tag-class>com.rlovep.tags.whenTag</tag-class> <!-- 輸出標簽體的內容格式標簽體不可以寫jsp的java代碼 --> <body-content>scriptless</body-content> <!-- 配置屬性name --> <attribute> <name>test</name> <!-- 是否必填 --> <required>true</required> <!-- 是否支持EL表達式 --> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <!-- 定義標簽,Otherwise--> <tag> <!-- 標簽名 --> <name>otherwise</name> <!-- 標簽處理類 --> <tag-class>com.rlovep.tags.OtherWiseTag</tag-class> <!-- 輸出標簽體的內容格式標簽體不可以寫jsp的java代碼 --> <body-content>scriptless</body-content> </tag> ~~~ 3.使用帶子標簽的標簽:與使用其他標簽稍微有些不同,需要嵌套 ~~~ <!-- 測試choose --> <rlovep:choose> <rlovep:When test="${10<5 }"> 條件成立執行when </rlovep:When> <rlovep:otherwise> 條件不成立執行otherwise </rlovep:otherwise> </rlovep:choose> ~~~ 自定義標簽就介紹到這里; ### 404頁面,505頁面,錯誤頁面配置方法: 可以在web.xml中給你的網站配置全局的404頁面,505頁面,錯誤頁面;配置方法如下:記得建立相應的跳轉文件。 ~~~ <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <!-- 配置空指針異常 --> <error-page> <exception-type>java.lang.NullPointerException</exception-type> <location>/error.jsp</location> </error-page> <!-- 配置505錯誤頁面 --> <error-page> <error-code>500</error-code> <location>/common/500.jsp</location> </error-page> <!-- 配置404錯誤頁面 --> <error-page> <error-code>404</error-code> <location>/common/404.html</location> </error-page> </web-app> ~~~ 好的本章介紹到這里 JSP入門就介紹到這里,喲喲,切割鬧; 來自一條小鯊魚wpeace(rlovep.com)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看