Java Web Foundation Summary 10---jsp El expression

Source: Internet
Author: User
Tags tld

Java Web Foundation Summary 10---jsp El expressionA. An introduction to El expressions

EL is an abbreviation for expression language. Has a great role in JSP, El mainly has the following functions: Get the data, replace the script expression in the JSP page to retrieve the Java object from the various types of Web domains, get the data. You can also perform operations that use El expressions to perform some basic relational, logical, and arithmetic operations on JSP pages to accomplish simple logical operations such as ${user==null} in JSP pages. You can also get the implicit objects of JSP that are commonly used in web development, and with these implicit objects, web developers can easily obtain references to common Web objects to obtain the data in those objects. Finally, you can call the Java method, which allows the user to develop a custom El function that invokes a method of the Java class through an El expression in a JSP page.

two. Use El to get data.

Use El expression to get data syntax: "${variable name}", as discussed earlier, El Expression statement in the execution, will call the Pagecontext.findattribute method, the variable name is the keyword, from page, request, session, Application the value of the corresponding variable in four fields, finds the corresponding object and returns an empty string if it is not found.

For example: ${people}, will be from page, request, session, application four fields to find the property named people, if found, return, cannot find, return an empty string.

The EL expression can not only get the value of the variable, it can also easily get the properties of the JavaBean, get the array, the collection of data, such as: ${people.age}, ${map.key} and so on

three. Perform some operations with El

The syntax of the El execution operation: the ${expression},el expression supports the following operators, borrowed from a graph to illustrate, as follows:

Like a trinocular operation:

${people! = Null?people.age: "0"}

three. Get common objects for web development

The EL expression language defines 11 hidden objects, which make it easy to get some common objects in web development and read the data of those objects. The syntax is: ${implicit object name}, you can get a reference to the object. The 11 implicit objects are as follows:

Four. El function

The El function, which is a method of invoking a Java class using an El expression, can be customized. The syntax format is: ${prefix:method (params)}. With these functions, you can do a lot of things, such as working with strings that are displayed.

Sun Company PIN defines a set of El function libraries for some common processing. These El functions are described in the Jstl development package, so using the Sun Company's El Library in a JSP page requires importing the JSTL development package and importing the El libraries into the page:

<% @taglib uri= "http://java.sun.com/jsp/jstl/functions" prefix= "FN"%>

Here's a simple example: Use the Fn:contains () function to determine whether the input string contains the specified substring.

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


The result is: Found test

Five. Developing a custom Elfunction

Developing custom El Functions, as well as developing custom labels, includes the following three steps:

1. Write a Java class with a static method in the class.
Package Com.cc;public class helloworld{public static string Printouthello (String message) {        if (message = = NULL)            return (null);       Return "Hello" +message;    }}


2. Create a new Tag library descriptor (TLD) file under the web-inf\ directory to describe the custom function in the 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>        <function>        <name>printOutHello</name><function-class>com.cc.HelloWorld</function-class>< Function-signature>java.lang.string Printouthello (java.lang.String) </function-signature>    </ Function></taglib>

3. Import the tag library on the JSP page and call the El function

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


Java Web Foundation Summary 10---jsp El expression

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.