Java Web Foundation Summary Nine--jsp tags

Source: Internet
Author: User
Tags tld

Java Web Foundation Summary Nine--jsp tags

JSP tags are also known as JSP Action, in the previous, the JSP is designed to act as a presentation layer. We want the JSP page to be used only as a data display module, not to embed any Java code into any business logic, but it is not possible to introduce a bit of business logic in real-world development, but introducing business logic can lead to unsightly Java code on the page. The JSP tag is to solve this problem. So there are some tags built into the JSP page, called JSP tags, that developers can use to accomplish some of the business logic of the page. We can also develop custom labels so that JSP pages do not appear in a single line of Java code.

A. Built-in tags for jsps

The JSP's built-in tag is a few of the tags that Sun has built into the JSP page, and does not need to be imported into the tag library via taglib directives, which can be used directly. Here are some common built-in tags.

1. <jsp:include> label

The function of <jsp:include> tag is to insert the output of another resource into the output of the current JSP page, because it is introduced in the JSP page execution, so it is called dynamic introduction.

Syntax: <jsp:include page= "URL | <%=expression%> "flush=" True|false "/>

The page property is used to specify the relative path to the resource being introduced, and it can also be obtained by executing a JSP expression. The Flush property specifies whether the output of the current JSP page is flushed to the client first when the output of the other resource is inserted.

There is a big difference between <jsp:include> and include directives. The <jsp:include> tag is a dynamically introduced,<jsp:include> tag involving 2 JSP pages that are translated into 2 servlets, and the contents of these 2 servlets are merged at execution time. The include directive is a static introduction, and the 2 JSP pages involved are translated into a servlet whose content is merged at the source file level. What they have in common is that they all merge two JSP page contents, so the two pages do not have duplicate HTML schema tags, otherwise the content output to the client will be a cluttered HTML document.

2. <jsp:forward> label

The <jsp:forward> tag is used to forward requests to another resource. Grammar:

<jsp:forwardpage= "URL | <%=expression%> "/>

The page property is used to specify the relative path of the resource to which the request is forwarded, and it can also be obtained by executing an expression.

3. <jsp:param> label

<jsp:param> tags can be considered as sub-labels of the first two tags. When using <jsp:include> and <jsp:forward> tags to introduce or forward requests to other resources, you can use the <jsp:param> tag to pass parameters to this resource. The Name property of the <jsp:param> tag is used to specify the parameter name, and the Value property is used to specify the parameter value. Multiple <jsp:param> tags can be used to pass multiple parameters in <jsp:include> and <jsp:forward> tags.

syntax when used in combination with <jsp:include> tags:

<jsp:includepage= "URL | <%=expression%> ">

<jsp:paramname= "ParameterName" value= "parametervalue|<%= expression%>"/>

</jsp:include>

syntax when used in combination with <jsp:forward> tags:

<jsp:forwardpage= "URL | <%=expression%> ">

<jsp:paramname= "ParameterName" value= "parametervalue|<%= expression%>"/>

</jsp:include>

4. <jsp:useBean> label

The <jsp:useBean> tag is used to find the JavaBean object of the specified name within the specified domain scope: Returns a reference to the JavaBean object if it exists. If it does not exist, it instantiates a new JavaBean object and stores it in the specified domain scope with the specified name.

Common syntax:

<jsp:usebeanid= "Beanname" class= "Package.class"

Scope= "Page|request|session|application"/>

The id attribute is used to specify the reference name of the JavaBean instance object and the name it stores in the domain scope. The class attribute is used to specify the full class name of the JavaBean (that is, it must have a package name). The scope property specifies the domain scope that the JavaBean instance object stores, and the value can only be one of four values, such as page, request, session, and application, and the default value is page.

two. Jstl Tag Library

JSTL is the abbreviation of the JSP Standardtag library. It is a new component of JSP development released by Sun Corporation. JSTL allows you to use tags for JSP page development, rather than Java code. Jstl can do almost anything that traditional JSP Java code can do.

Jstl tags can be divided into the following types according to their functions:

1. Key tags (core tags)

The core tag is prefixed with C and needs to be imported into the tag library using the taglib directive:

<%@ tagliburi= "Http://java.sun.com/jsp/jstl/core" prefix= "C"%>

For a common example,,<c:remove> tags:

<c:remove> tags are used to delete properties from four web domains.

The syntax format is as follows:

<c:remove var= "VarName"

[scope= "{page|request|session|application}"]/>

2. Formatting tags (formatting tags), also known as internationalization tags.

JSTL format tags can be used to format and display text, date, time and number, and to internationalize the operation. With the FMT prefix, the tag library needs to be imported using the TAGLIB directive:

<%@ taglibprefix= "FMT" uri= "Http://java.sun.com/jsp/jstl/fmt"%>

For example, formatting numbers <fmt:formatNumber> Tags:

<% @taglib prefix= "C" uri= "Http://java.sun.com/jsp/jstl/core"%><% @taglib prefix= "FMT" uri= "/HTTP/ Java.sun.com/jsp/jstl/fmt "%> 

The result of the output is:

formattednumber:£120,000.23

3. SQL tags (sql tags)

JSTL's SQL Tag library tag can be used instead of Java code to manipulate relational databases.

To prefix SQL, you need to import the tag library using the taglib directive:

<%@ taglibprefix= "SQL" uri= "Http://java.sun.com/jsp/jstl/sql"%>

For example, use the <sql:setDataSource> tag to configure the data source for the database:

<% @taglib uri= "Http://java.sun.com/jsp/jstl/core" prefix= "C"%><% @taglib uri= "http://java.sun.com/jsp/ Jstl/sql "prefix=" SQL "%>


 4. XML tag (XML tags)

JSTL XML tags are used to create and manipulate XML documents in JSPs. XML parsing can be implemented, XML data transformed, and so on. Prefixed with XML, the tag library needs to be imported using the TAGLIB directive:

<%@ taglib prefix= "x" uri= "Http://java.sun.com/jsp/jstl/xml"%>

You will also need to copy the Xercesimpl.jar and Xalan.jar two jar packages to the \lib directory of the <tomcat installation directory >. Since the use of less, there is not much to say.

three. Developing a custom tag library

To develop a custom label to remove Java code from a JSP page, you need to complete the following two steps: First write a Java class (Tag processor class) that implements the tag interface. The tag Library descriptor (TLD) file is then written to describe the label processor class in the TLD file.

1. Implementation process of custom label processor with tag interface

When the JSP engine encounters a custom label, it first creates an instance object of the label processor class, and then calls the following method in turn, according to the communication rules defined by the JSP specification.

Public Voidsetpagecontext (PageContext pc), after the JSP engine instantiates the label processor, the Setpagecontext method is called to pass the PageContext object of the JSP page to the label processor. The label processor can then communicate with the JSP page through this PageContext object.

public void SetParent (Tag t), after the Setpagecontext method executes, the Web container then calls the SetParent method to pass the parent tag of the current label to the current label processor, if the current label does not have a parent tag, The value of the parameter passed to the SetParent method is null.

public int doStartTag (), the doStartTag method of the label processor is called when the Web container executes to the start tag of the custom label after the Setpagecontext method and the SetParent method are called.

public int Doendtag (), the Web container executes the label body of the custom label and then executes the end tag of the custom label, at which point the Web container invokes the Doendtag method of the label processor.

public void release (), usually after the Web container finishes executing the custom label, the label processor resides in memory and the Web container calls the release method for the other pull server until the Web app is stopped.

Since the implementation of the tag interface to complete the custom label is a bit cumbersome, so in JSP 2.0 defines a simpler Simpletag interface to implement the function of the label. The label that implements the Simpletag interface is often referred to as a simple label.

2. Implement a simple output HelloWorld of custom labels. (1). First write a tag processor class that implements the tag interface.

The Helloworldtag.java code is as follows:

public class Helloworldtag implements Tag {private PageContext pagecontext;public int doStartTag () throws Jspexception {Ht Tpservletrequest request = (HttpServletRequest) pagecontext.getrequest (); JspWriter out = Pagecontext.getout (); String string = "Hello World";    try {out.write (string);} catch (IOException e) {throw new RuntimeException (e);} return 0;} public int Doendtag () throws jspexception {return 0;} Public Tag getParent () {return null;} public void Release () {}public void Setpagecontext (PageContext arg0) {this.pagecontext = arg0;} public void SetParent (Tag arg0) {}}


(2). Create a new TLD file in the web-inf/directory and describe the label processor in the TLD file.

Hello.tld file:

<?xml version= "1.0" encoding= "UTF-8"? ><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 Simpletag handlers.</description>    <tlib-version>1.0</tlib-version>    < short-name>simpletaglibrary</short-name>    <uri>/hello</uri>       <tag>        < Name>helloworld</name>  <tag-class>com.cc.helloworldtag</tag-class><body-content >empty</body-content>    </tag></taglib>


(3). Use the TAGLIB directive in the JSP page to import the tag library and use the custom label.

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><% @taglib uri= "/hello" prefix= "Hello"% ><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

Java Web Foundation Summary Nine--jsp tags

Related Article

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.