JSP tag, jsp tag Library

Source: Internet
Author: User
Tags tld

JSP tag, jsp tag Library
1. although we want JSP pages to be used only as data display modules and do not nest any java code to introduce any business logic, it is impossible to introduce no business logic in actual development, but the introduction of business logic will lead to ugly java code on the page. How can this problem be solved?
Sun allows users to develop java code for custom tag encapsulation pages so that a line of java code does not appear on jsp pages. Of course, Sun also has some built-in tags (these tags are called jsp tags/actions) in the jsp page. developers can use these tags to complete some common business logic of the page.

JSP tags are also called Jsp Action elements. They are used to provide business logic functions on JSP pages.

2. Label Classification


3. Common JSP labels

<Jsp: include> tag

<Jsp: forward> tag

<Jsp: param> label

4<Jsp: include> tag

(1 ). <jsp: include> the tag is used to insert the output content of another resource into the output content of the current JSP page. This introduction method during JSP page execution is called dynamic introduction.
(2). Syntax:
<Jsp: include page = "relativeURL | <% = expression %>" flush = "true | false"/>
(3) The. page attribute is used to specify the relative path of the introduced resource. It can also be obtained by executing an expression.

(4) The flush attribute specifies whether to refresh the output content of the current JSP page to the client when inserting the output content of other resources. The default value is false. That isTrue: input the output of the current jsp page to the browser first, and then add the included page/false (add the original content to the browser at the same time) (default) to dynamically include, to call and execute

(5). <jsp: include page/> (dynamic, executed) and <% @ include file/> (static, translation)
(6). N JSPs generate N servlets. The structure of the total jsp is good, similar to function call.

5. <jsp: forward> tag

(1). <jsp: forward> tags are used to forward requests to another resource.
(2). Syntax:
<Jsp: forward page = "relativeURL | <% = expression %>"/>
The page attribute is used to specify the relative path of the resource to which the request is forwarded. It can also be obtained by executing an expression.

6. <jsp: param> label

(1 ). when you use the <jsp: include> and <jsp: forward> labels to introduce or forward requests to other resources, you can use the <jsp: param> label to pass parameters to this resource.
(2). Syntax 1:
<Jsp: include page = "relativeURL | <% = expression %>">
<Jsp: param name = "parameterName" value = "parameterValue | <% = expression %>"/>
</Jsp: include>
(3). Syntax 2:
<Jsp: forward page = "relativeURL | <% = expression %>">
<Jsp: param name = "parameterName" value = "parameterValue | <% = expression %>"/>
</Jsp: include>
<Jsp: param> the name attribute of the tag is used to specify the parameter name, and the value attribute is used to specify the parameter value. You can use multiple <jsp: param> tags in the <jsp: include> and <jsp: forward> tags to pass multiple parameters.

7. JSP ing JSP (configuring virtual access addresses)

<Servlet>
<Servlet-name> SimpleJspServlet </servlet-name>
<Jsp-file>/jsp/simple. jsp </jsp-file>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
......
<Servlet-mapping>
<Servlet-name> SimpleJspServlet </servlet-name>
<Url-pattern>/test/qq.html </url-pattern>
</Servlet-mapping>

8. garbled jsp tags:



JSP label usage Problems

Step 1: Introduce the tag library, such as introducing c. tld in the jstl-1.2.jar, find the uri element inside, and copy the value inside
Step 2: add
<% @ Tablib prefix = "c", uri = "java.sun.com/jsp/jstl/core" %>
The value of this prefix attribute is different from that of other tag libraries. It is best to be consistent with the prefix of XX. tld. uri is the value just copied,
Step 3: Use tags as needed on the jsp page. Generally, tags are used with EL expressions, such as cyclic tags. The common usage is as follows:
<C: forEach var = "temp_attribute" items = "$ {your_content}">
Content to be iterated here
</C: forEach>
At this point, jsp labels are basically used up.
If you do not know the attributes of many tags, you can view them in the. tld file.
Here we briefly describe the meaning of several elements:
<Name>: tag attributes.
<Required>: indicates whether the attribute must be: false -- not required; true --- required
<Rtexprvalue>: indicates whether the value of this attribute can reference the EL expression: false -- No, true -- yes

Use of the c label foreach in jsp

FOREACH label section.
First, check whether data is obtained in MAP.
<C: forEach [var = "varName"] items = "" [varStatus = "varStatusName"]
Meaning of the FOREACH Tag: items stores the aggregated objects that are iterated, and var stores the members that are now referred.
3. the parameters output by the var element must correspond to the parameters stored in the MAP. (generally, a JAVABEAN object is encapsulated in a LIST object ). in this case, whether the NAME equality exists in the JAVABEAN. if yes, the EL expression is written as $ {entry. name} can be entered directly, and other values are the same.

IF tag section.
EL expressions have their own judgment methods and cannot be used directly = ,! =, >=, <=Directly judge.
If you want to determine whether the encapsulated object is empty, use the empty keyword ....

The following is an example of the comprehensive use of the IF label and FOREACH:
<! -- If the collection is not empty, output data! --> <C: if test = "$ {! Empty adminlist} "> <c: forEach items =" $ {adminlist} "var =" list "> <tr> <td >$ {list. id} </td> <td >$ {list. name} </td> <td >$ {list. pwd} </td> </tr> </c: forEach> </c: if> <! -- If the set is empty, output a prompt in HTML! --> <C: if test = "$ {empty adminlist}"> the data cannot be found! </C: if>

To better display the FOREACH effect, we usually use the choose tag for determination. The following is an example of the complete tag segment in the JSP page: (IF tag is not used for example .)
<Table cellpadding = "5" cellspacing = "1" border = "0" width = "100%" align = "center"> <tr align = "center"> <td> ID </td> <td> User Account </td> <td> User Password </td> <td colspan = "2"> management </td> </tr> <c: choose> <c: when test = "$ {! Empty userlist} "> <c: forEach items =" $ {userlist} "var =" list "> <tr> <td> $ ...... remaining full text>
 

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.