Learning Notes (vi) custom labels and relative path absolute paths

Source: Internet
Author: User
Tags tld

2. Custom Labels

1). HelloWorld

①. Create a label processor class: Implement the Simpletag interface.
②. In the Web-inf folder, create a new. TLD (Tag library profile) as an XML file with an extension. and copy into the fixed part: and the
Description, Display-name, tlib-version, Short-name, Uri make changes

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

③. Describe the custom label in the TLD file:

<!--describe your custom Hellosimpletag label--
<tag>
<!--tag name: The name of the tag when using it on a JSP page--
<name>hello</name>

<!--the full-class name of the tag
<tag-class>com.atguigu.javaweb.tag.HelloSimpleTag</tag-class>
<!--label body type--
<body-content>empty</body-content>
</tag>

④. Using a custom label on a JSP page:

> Importing tag Library profiles using taglib directives: <% @taglib uri= "Http://www.atguigu.com/mytag/core" prefix= "Atguigu"%>

> Using a custom tag: <atguigu:hello/>

2). Setjspcontext: Must be called by the JSP engine, before Dotag, the PageContext representing the JSP engine to the label processor class.

Private PageContext PageContext;

@Override
public void Setjspcontext (Jspcontext arg0) {
System.out.println (arg0 instanceof PageContext);
This.pagecontext = (PageContext) arg0;
}

3). Custom Labels with attributes:

①. The setter method is defined in the label processor class first. It is recommended that all property types be set to String types.

private String value;
Private String count;

public void SetValue (String value) {
This.value = value;
}

public void SetCount (String count) {
This.count = count;
}

②. To describe the attribute in the TLD description file:

<!--describe the properties of the current label--
<attribute>
<!--property name, as defined by the setter method of the label processor class, and
<name>value</name>
<!--whether the property is required--
<required>true</required>
<!--rtexprvalue:runtime expression value
Whether the current property can accept dynamic values for run-time expressions--
<rtexprvalue>true</rtexprvalue>
</attribute>

③. Using attributes in the page, the property name is the name defined in the TLD file.

<atguigu:hello value= "${param.name}" count= "Ten"/>

4). Usually the development of simple tags directly inherit simpletagsupport. can call its corresponding getter method to get the corresponding 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. Relative path and absolute path:

1). Why solve the problem of relative paths: in the case of a Servlet forwarding page, the relative path can be confused.

a.jsp: <a href= "Tobservlet" >to B page2</a>
ToBServlet:request.getRequestDispatcher ("/dir/b.jsp"). Forward (request, response);

Note that at this point the value of the address bar of the browser after clicking to B Page2 hyperlink: Http://localhost:8989/day_36/ToBServlet, the actual display is
Dir-Path b.jsp

The b.jsp page has a hyperlink: <a href= "c.jsp" >to c Page</a>. By default, c.jsp should be under the same path as b.jsp. Click on the hyperlink now
will be displayed in the browser address bar: http://localhost:8989/day_36/c.jsp. However, there is no c.jsp in the root directory, so there is a problem with path confusion.

2). Using an absolute path will solve the above problem:

Absolute path: The path relative to the root directory of the current WEB site.

http://localhost:8989/day_36/c.jsp:http://localhost:8989/is the root directory of the WEB site,/day_36 is ContextPath,
/c.jsp is a file path relative to the current WEB application. We need to add the ContextPath to the current WEB application under any path.

Like what:
<a href= "Tobservlet" >to B page2</a> need to be changed to: <a href= "<%= request.getcontextpath ()%>/ToBServlet" > To B page2</a>
Response.sendredirect ("a.jsp"); Should be changed to: Response.sendredirect (Request.getcontextpath () + "/a.jsp");
<form action= "Addservlet" ></form> need to change to: <form action= "<%= request.getcontextpath ()%>/addservlet "></form>

3). In the Javaweb app/representative: sometimes represents the root of the current WEB app, sometimes representing the root directory of the site.

/represents the root path of the current web app: if/where the command or method needs to be parsed by the Web server, rather than directly to the browser, then/represents the root path of the web App. Write at this time
The absolute path does not need to be added ContextPath.
When you do a serlvet mapping path in the Web. xml file,
On request Forwarding: Request.getrequestdispatcher ("/dir/b.jsp"). Forward (request, response);

/represents the root directory of the site: if/directly to the browser parsing,/represents the site's root path, at this time must be added ContextPath
<form action= "/addservlet" ></form>
Response.sendredirect ("/a.jsp");

4). How to obtain ContextPath:
Servletcontext:getcontextpath ()
Httpservletrequest:getcontextpath ()

Learning Notes (vi) custom labels and relative path absolute paths

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.