Jsp tag & amp; EL expression, jsp tag Library

Source: Internet
Author: User
Tags tld

Jsp tag & EL expression, jsp tag Library

1. jsp tag and el expression
(1) What is a jsp tag?
The jsp tag is used to replace the java code in the jsp file. After the container encounters a jsp tag, it finds the tag Class Based on the tag and then executes it.
Note: Writing java code directly in jsp is not conducive to the maintenance of jsp files (for example, it is inconvenient to hand over jsp containing java code to the artist for modification,
Sun developed the jsp label technical specifications.
Jsp tag technology has two advantages:
A. convenient maintenance of jsp files.
B. Convenient code reuse.
(2) What is an el expression?
It is a set of simple operation rules used to assign values to attributes of jsp tags, and can also be output directly.
(3) Use of el expressions
1) access bean attributes (a1.jsp)
A. method 1
$ {User. name} searches for the object with the bound name "user" from pageContext, request, session, and application in sequence. After finding the object,
The "getName" method of the object is called, and the running result of the method is output.
Note:
The a1. el expression converts null to "" output.
A2. if this object cannot be found based on the bound name, "" is output "".
A3. you can use pagination, requestScope, sessionScope, and applicationScope to specify the search range.
B. method 2
$ {User ["name"]}
Note:
B1.
B2. [] can use a subscript starting from 0 to access an element in the array.
2) perform some simple operations, and the calculation results can be used to assign values to the tag attributes of jsp, or directly output. (A2.jsp)
A. arithmetic operation +,-, *,/, %
Note: + can only sum and cannot connect strings.
B. relational operations>, >=, <, <=, = ,! =
C. Logical Operations &, | ,!
D. empty operation
Empty is used to determine whether the set is empty or whether it is an empty string.
3) read request parameter value (a3.jsp)
$ {Param. username} is equivalent to request. getParameter ("username ");
$ {ParamValues. city} is equivalent to request. getParameterValues ("city ");
(4) use of jstl labels
1) What is jstl? (Java standard tag lib)
A set of jsp labels developed by apache, which were later donated to sun, and sun named it jstl.
2) how to use it?
Step 1, copy the jar file related to jstl to the WEB-INF \ lib.
Note: If you are using javaee5.0 or a later version, you do not need to copy it. Some tomcat versions do not contain the jstl jar file and need to be copied.
Step 2: Use the taglib command to introduce the tag to be used.
<% @ Taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
Uri: Specify the namespace ).
Prefix: the prefix of the namespace.

Note: uri is in c. tld under jstl-1.2.jar under META-INF package

<Short-name> c </short-name>
<Uri>
Http://java.sun.com/jsp/jstl/core</Uri>

3) several core labels in jstl
A. if tag (c1.jsp)
<C: if test = "" var = "" scope = ""> tag body </c: if>
If the value of test is true, the TAG body is executed.
You can use el expressions to assign values to the test attribute..
Var attribute: Specify the binding name.
Scope attribute: Specify the binding range, Which can be "page", "request", "session", and "application ".

Gender: <c: if test = "$ {user. gender = 'M'}"> male </c: if> <br/>
<C: if test = "$ {user. gender = 'F'}"> female </c: if> <br/>

Gender: <c: if test = "$ {user. gender = 'M'}"> male </c: if> <br/>
<C: if test = "$ {user. gender! = 'M'} "> female </c: if> <br/>

Gender: <c: if test = "$ {user. gender = 'M'} "var =" flag "scope =" page "> male </c: if> <br/>
<C: if test = "$ {! Flag} "> female </c: if> <br/>


B. choose tag(C2.jsp) (equivalent to if... Else if ...)
<C: choose>
<C: when test = ""> </c: when>
<C: otherwise> </c: otherwise>
</C: choose>
When the value of test is true, the content of the label body can be executed once or multiple times. Otherwise can appear 0 times or 1 time.

<C: choose>
<C: when test = "$ {user. gender = 'M'}"> male </c: when>
<C: when test = "$ {user. gender = 'F'}"> female </c: when>
<C: otherwise> confidentiality </c: otherwise>
</C: choose>


C. forEach label(C3.jsp)
Used to traverse a set or Array
<C: forEach items = "" var = "" varStatus = ""> </c: forEach>
Items attribute: Specifies the set or array to be traversed. You can use the el expression.
Var attribute: Specify the binding name. The binding range is pageContext (each time an element is obtained from a set or array and bound to pageContext ).
VarStatus attribute: Specify the binding name. The binding value is an object that encapsulates the current traversal status (this object provides some methods to obtain the current traversal status ).
GetIndex (): returns the subscript of the element being traversed (starting from 0 ).
GetCount (): obtains the number of traversal times.

<C: forEach items = "$ {users}" var = "user" varStatus = "s">
<Tr class = "row $ {s. index % 2 + 1}">
<Td >$ {user. name} </td>
<Td >$ {user. age} </td>
<Td >$ {user. gender} </td>
<Td >$ {s. index} </td>
<Td >$ {s. count} </td>
</Tr>
</C: forEach>


(5) custom tags (Simple label Technology) (web10-2)
Step 1: Write a java class that inherits the SimpleTagSupport class.(HelloTag. java)
Step 2: override doTag method. Write the corresponding processing logic in the method.
Step 3: Describe the tag (. tld File) (Mytag. tld)

<Body-content> three values

<Body-content> empty </body-content>

Empty: the label does not have a label body.

   

Scriptless: the label can have a label body, but the content of the label body cannot contain java code <%>, <% = %>, <%! %>.

JSP: The tag can have a tag body, and the content of the TAG body can contain java code. However. Only the complex tag technology supports this value. Simple tag technology does not support JSP.

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.