javaWeb學習(四)
2. 自定義標籤
1). HelloWorld
①. 創建一個標籤處理器類: 實現 SimpleTag 接口.
②. 在 WEB-INF 文件夾下新建一個 .tld(標籤庫描述文件) 爲擴展名的 xml 文件. 並拷入固定的部分: 並對
description, display-name, tlib-version, short-name, uri 做出修改
<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>JSTL 1.1 core library</description>
<display-name>JSTL core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>c</short-name>
<uri>http://java.sun.com/jsp/jstl/core</uri>
</taglib>
③. 在 tld 文件中描述自定義的標籤:
<!-- 描述自定義的 HelloSimpleTag 標籤 -->
<tag>
<!-- 標籤的名字: 在 JSP 頁面上使用標籤時的名字 -->
<name>hello</name>
<!-- 標籤所在的全類名 -->
<tag-class>com.atguigu.javaweb.tag.HelloSimpleTag</tag-class>
<!-- 標籤體的類型 -->
<body-content>empty</body-content>
</tag>
④. 在 JSP 頁面上使用自定義標籤:
> 使用 taglib 指令導入標籤庫描述文件: <%@taglib uri="http://www.atguigu.com/mytag/core" prefix="atguigu" %>
> 使用自定義的標籤: <atguigu:hello/>
2). setJspContext: 一定會被 JSP 引擎所調用, 先於 doTag, 把代表 JSP 引擎的 pageContext 傳給標籤處理器類.
private PageContext pageContext;
@Override
public void setJspContext(JspContext arg0) {
System.out.println(arg0 instanceof PageContext);
this.pageContext = (PageContext) arg0;
}
3). 帶屬性的自定義標籤:
①. 先在標籤處理器類中定義 setter 方法. 建議把所有的屬性類型都設置爲 String 類型.
private String value;
private String count;
public void setValue(String value) {
this.value = value;
}
public void setCount(String count) {
this.count = count;
}
②. 在 tld 描述文件中來描述屬性:
<!-- 描述當前標籤的屬性 -->
<attribute>
<!-- 屬性名, 需和標籤處理器類的 setter 方法定義的屬性相同 -->
<name>value</name>
<!-- 該屬性是否被必須 -->
<required>true</required>
<!-- rtexprvalue: runtime expression value
當前屬性是否可以接受運行時表達式的動態值 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
③. 在頁面中使用屬性, 屬性名同 tld 文件中定義的名字.
<atguigu:hello value="${param.name }" count="10"/>
4). 通常情況下開發簡單標籤直接繼承 SimpleTagSupport 就可以了. 可以直接調用其對應的 getter 方法得到對應的 API
public class SimpleTagSupport implements SimpleTag{
public void doTag()
throws JspException, IOException{}
private JspTag parentTag;
public void setParent( JspTag parent ) {
this.parentTag = parent;
}
public JspTag getParent() {
return this.parentTag;
}
private JspContext jspContext;
public void setJspContext( JspContext pc ) {
this.jspContext = pc;
}
protected JspContext getJspContext() {
return this.jspContext;
}
private JspFragment jspBody;
public void setJspBody( JspFragment jspBody ) {
this.jspBody = jspBody;
}
protected JspFragment getJspBody() {
return this.jspBody;
}
}
1. 相對路徑和絕對路徑:
1). 爲什麼要解決相對路徑的問題: 在有一個 Servlet 轉發頁面的情況下, 會導致相對路徑的混亂.
a.jsp: <a href="ToBServlet">To B Page2</a>
ToBServlet: request.getRequestDispatcher("/dir/b.jsp").forward(request, response);
注意, 此時點擊 To B Page2 超鏈接後的瀏覽器的地址欄的值: http://localhost:8989/day_36/ToBServlet, 實際顯示的是
dir 路徑下的 b.jsp
而 b.jsp 頁面有一個超鏈接: <a href="c.jsp">TO C Page</a>. 默認情況下, c.jsp 應該和 b.jsp 在同一路徑下. 此時點擊超鏈接
將在瀏覽器地址欄顯示: http://localhost:8989/day_36/c.jsp. 但在根目錄下並沒有 c.jsp, 所以會出現路徑混亂的問題.
2). 使用絕對路徑會解決以上的問題:
絕對路徑: 相對於當前 WEB 站點根目錄的路徑.
http://localhost:8989/day_36/c.jsp: http://localhost:8989/ 是 WEB 站點的根目錄, /day_36 是 contextPath,
/c.jsp 是相對於當前 WEB 應用的一個文件路徑. 我們需要在當前 WEB 應用的任何的路徑下都添加上 contextPath, 即可.
比如:
<a href="ToBServlet">To B Page2</a> 需改爲: <a href="<%= request.getContextPath() %>/ToBServlet">To B Page2</a>
response.sendRedirect("a.jsp"); 需改爲: response.sendRedirect(request.getContextPath() + "/a.jsp");
<form action="AddServlet"></form> 需改爲: <form action="<%= request.getContextPath() %>/AddServlet"></form>
3). 在 JavaWEB 應用中 / 代表的是: 有時代表當前 WEB 應用的根目錄, 有時代表的是站點的根目錄.
/ 代表的是當前 WEB 應用的根路徑: 若 / 所在的命令或方法需被 WEB 服務器解析, 而不是直接打給瀏覽器, 則 / 代表 WEB 應用的根路徑. 此時編寫
絕對路徑就不需要在添加 contextPath 了.
在 web.xml 文件中做 Serlvet 映射路徑時,
在請求轉發: request.getRequestDispatcher("/dir/b.jsp").forward(request, response);
/ 代表的是站點的根目錄: 若 / 直接交由瀏覽器解析, / 代表的就是站點的根路徑, 此時必須加上 contextPath
<form action="/AddServlet"></form>
response.sendRedirect("/a.jsp");
4). 如何獲取 contextPath:
ServletContext: getContextPath()
HttpServletRequest: getContextPath()
1). HelloWorld
①. 創建一個標籤處理器類: 實現 SimpleTag 接口.
②. 在 WEB-INF 文件夾下新建一個 .tld(標籤庫描述文件) 爲擴展名的 xml 文件. 並拷入固定的部分: 並對
description, display-name, tlib-version, short-name, uri 做出修改
<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>JSTL 1.1 core library</description>
<display-name>JSTL core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>c</short-name>
<uri>http://java.sun.com/jsp/jstl/core</uri>
</taglib>
③. 在 tld 文件中描述自定義的標籤:
<!-- 描述自定義的 HelloSimpleTag 標籤 -->
<tag>
<!-- 標籤的名字: 在 JSP 頁面上使用標籤時的名字 -->
<name>hello</name>
<!-- 標籤所在的全類名 -->
<tag-class>com.atguigu.javaweb.tag.HelloSimpleTag</tag-class>
<!-- 標籤體的類型 -->
<body-content>empty</body-content>
</tag>
④. 在 JSP 頁面上使用自定義標籤:
> 使用 taglib 指令導入標籤庫描述文件: <%@taglib uri="http://www.atguigu.com/mytag/core" prefix="atguigu" %>
> 使用自定義的標籤: <atguigu:hello/>
2). setJspContext: 一定會被 JSP 引擎所調用, 先於 doTag, 把代表 JSP 引擎的 pageContext 傳給標籤處理器類.
private PageContext pageContext;
@Override
public void setJspContext(JspContext arg0) {
System.out.println(arg0 instanceof PageContext);
this.pageContext = (PageContext) arg0;
}
3). 帶屬性的自定義標籤:
①. 先在標籤處理器類中定義 setter 方法. 建議把所有的屬性類型都設置爲 String 類型.
private String value;
private String count;
public void setValue(String value) {
this.value = value;
}
public void setCount(String count) {
this.count = count;
}
②. 在 tld 描述文件中來描述屬性:
<!-- 描述當前標籤的屬性 -->
<attribute>
<!-- 屬性名, 需和標籤處理器類的 setter 方法定義的屬性相同 -->
<name>value</name>
<!-- 該屬性是否被必須 -->
<required>true</required>
<!-- rtexprvalue: runtime expression value
當前屬性是否可以接受運行時表達式的動態值 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
③. 在頁面中使用屬性, 屬性名同 tld 文件中定義的名字.
<atguigu:hello value="${param.name }" count="10"/>
4). 通常情況下開發簡單標籤直接繼承 SimpleTagSupport 就可以了. 可以直接調用其對應的 getter 方法得到對應的 API
public class SimpleTagSupport implements SimpleTag{
public void doTag()
throws JspException, IOException{}
private JspTag parentTag;
public void setParent( JspTag parent ) {
this.parentTag = parent;
}
public JspTag getParent() {
return this.parentTag;
}
private JspContext jspContext;
public void setJspContext( JspContext pc ) {
this.jspContext = pc;
}
protected JspContext getJspContext() {
return this.jspContext;
}
private JspFragment jspBody;
public void setJspBody( JspFragment jspBody ) {
this.jspBody = jspBody;
}
protected JspFragment getJspBody() {
return this.jspBody;
}
}
1. 相對路徑和絕對路徑:
1). 爲什麼要解決相對路徑的問題: 在有一個 Servlet 轉發頁面的情況下, 會導致相對路徑的混亂.
a.jsp: <a href="ToBServlet">To B Page2</a>
ToBServlet: request.getRequestDispatcher("/dir/b.jsp").forward(request, response);
注意, 此時點擊 To B Page2 超鏈接後的瀏覽器的地址欄的值: http://localhost:8989/day_36/ToBServlet, 實際顯示的是
dir 路徑下的 b.jsp
而 b.jsp 頁面有一個超鏈接: <a href="c.jsp">TO C Page</a>. 默認情況下, c.jsp 應該和 b.jsp 在同一路徑下. 此時點擊超鏈接
將在瀏覽器地址欄顯示: http://localhost:8989/day_36/c.jsp. 但在根目錄下並沒有 c.jsp, 所以會出現路徑混亂的問題.
2). 使用絕對路徑會解決以上的問題:
絕對路徑: 相對於當前 WEB 站點根目錄的路徑.
http://localhost:8989/day_36/c.jsp: http://localhost:8989/ 是 WEB 站點的根目錄, /day_36 是 contextPath,
/c.jsp 是相對於當前 WEB 應用的一個文件路徑. 我們需要在當前 WEB 應用的任何的路徑下都添加上 contextPath, 即可.
比如:
<a href="ToBServlet">To B Page2</a> 需改爲: <a href="<%= request.getContextPath() %>/ToBServlet">To B Page2</a>
response.sendRedirect("a.jsp"); 需改爲: response.sendRedirect(request.getContextPath() + "/a.jsp");
<form action="AddServlet"></form> 需改爲: <form action="<%= request.getContextPath() %>/AddServlet"></form>
3). 在 JavaWEB 應用中 / 代表的是: 有時代表當前 WEB 應用的根目錄, 有時代表的是站點的根目錄.
/ 代表的是當前 WEB 應用的根路徑: 若 / 所在的命令或方法需被 WEB 服務器解析, 而不是直接打給瀏覽器, 則 / 代表 WEB 應用的根路徑. 此時編寫
絕對路徑就不需要在添加 contextPath 了.
在 web.xml 文件中做 Serlvet 映射路徑時,
在請求轉發: request.getRequestDispatcher("/dir/b.jsp").forward(request, response);
/ 代表的是站點的根目錄: 若 / 直接交由瀏覽器解析, / 代表的就是站點的根路徑, 此時必須加上 contextPath
<form action="/AddServlet"></form>
response.sendRedirect("/a.jsp");
4). 如何獲取 contextPath:
ServletContext: getContextPath()
HttpServletRequest: getContextPath()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.