第二部分簡單講解:主要講解el表達式,核心標籤庫。本章主要講解:自定義標籤庫;404頁面,505頁面,錯誤頁面配置方法html
所有代碼下載:連接java
自定義標籤是用戶定義的JSP語言元素。當JSP頁面包含一個自定義標籤時將被轉化爲servlet,標籤轉化爲對被 稱爲tag handler的對象的操做,即當servlet執行時Web container調用那些操做。JSP標籤擴展可讓你建立新的標籤而且能夠直接插入到一個JSP頁面。 JSP 2.0規範中引入Simple Tag Handlers來編寫這些自定義標記。你能夠繼承SimpleTagSupport類並重寫的doTag()方法來開發一個最簡單的自定義標籤。web
下面的步驟創建一個自定義標籤用於戰術客戶端的ip地址:服務器
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; } }
<?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>
在jsp頁面的頭部導入自定義標籤庫:url爲你在tld中寫的url,前綴也是你在tld文件中定義的app
<%@ taglib uri="http://rlovep.com" prefix="rlovep" %>
<%-- 測試簡單的自定義標籤,標籤體(我是你)不顯示 --%> <rlovep:showIp>我是你 </rlovep:showIp>
當訪問:http://localhost:8080/stuJsp/Hellotags.jsp 時;要重啓Tomcat使服務器啓動時,加載每一個web應用的WEB-INF目錄下的全部文件!!!例如。web.xml, tld文件!!!
步驟以下:less
你能夠像標準標籤庫同樣在標籤中包含消息內容。如咱們要在咱們自定義的
<rlovep:showIp>我是你 </rlovep:showIp>
JspContext jspContext2 = this.getJspContext(); //顯示標籤體的兩種方法 //方法1直接調用 //jspBody.invoke(null); //方法2經過輸出到out //jspBody.invoke(jspContext2.getOut());
修改tld文件:ide
<tag> <!-- 標籤名 --> <name>showIp</name> <!-- 標籤處理類 --> <tag-class>com.rlovep.tags.ShowIp</tag-class> <!-- 輸出標籤體的內容格式標籤體不能夠寫jsp的java代碼 --> <body-content>scriptless</body-content> </tag>
<%-- 標籤提會顯示 --%> <rlovep:showIp>我是你 </rlovep:showIp>
你能夠在自定義標準中設置各類屬性,要接收屬性,值自定義標籤類必須實現setter方法;測試
<!-- 測試帶屬性的標籤,標籤體顯示經過類處理 --> <rlovep:AttributeTags name="peace" value="12345
添加倆個屬性: //聲明屬性的成員變量 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>
就像核心標籤庫的choose標籤同樣咱們也能夠定義嵌套的自定義標籤,這部分咱們主要講解本身建立一個相似核心標籤庫的choose標籤。步驟以下:
//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); } } }
<!-- 定義標籤,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>
<!-- 測試choose --> <rlovep:choose> <rlovep:When test="${10<5 }"> 條件成立執行when </rlovep:When> <rlovep:otherwise> 條件不成立執行otherwise </rlovep:otherwise> </rlovep:choose>
自定義標籤就介紹到這裏;
能夠在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)