Java web basics-jsp EL expressions and el expressions

Source: Internet
Author: User
Tags tld

Java web basics-jsp EL expressions and el expressions
Java web basics-jsp EL expression 1. EL expression Introduction

EL is short for Expression Language. In jsp, EL plays a major role in obtaining data and replacing the script expression on the JSP page to retrieve java objects and obtain data from various types of web domains. You can also perform operations. Using EL expressions, you can perform some basic relational operations, logical operations, and arithmetic operations on the JSP page to complete some simple logical operations on the JSP page, for example, $ {user = null }. You can also obtain the implicit objects of jsp commonly used in web development. With these implicit objects, web developers can easily obtain references to common web objects to obtain data in these objects. Finally, you can call the Java method. The EL expression allows you to develop custom EL functions to call Java class methods through EL expressions on the JSP page.

2. Use EL to obtain data.

The EL expression is used to obtain the data Syntax: "$ {variable name}". As discussed earlier, when executing an EL expression statement, pageContext is called. the findAttribute method uses the variable name keyword to search for the corresponding variable values from the four domains of page, request, session, and application. If the value is found, the corresponding object is returned. If the value is not found, an empty string is returned.

For example, $ {people} searches for the attribute "people" in the "page", "request", "session", and "application" fields. If the attribute is found, the system returns an empty string.

The EL expression can not only obtain the value of a variable, but also easily obtain the attributes of the JavaBean to obtain the data of arrays and sets, for example: $ {people. age}, $ {map. key} and so on

Iii. Use EL to execute some operations

The syntax for EL to execute operations: $ {operation expression}. EL expressions support some of the following operators, which are illustrated by a figure as follows:

 

For example:

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

 

3. obtain common web development objects

11 implicit objects are defined in EL Expression Language. Using these implicit objects, you can easily obtain some common objects in web development and read the data of these objects. Syntax: $ {implicit object name} to obtain object references. The 11 implicit objects are as follows:

 

Iv. EL Functions

EL functions call Java class methods using EL expressions, which can be customized. Syntax format: $ {prefix: method (params )}. Using these functions, you can perform many operations, such as processing the displayed strings.

SUN defines an EL function library for some common processing. These EL functions are described in the JSTL Development Kit. Therefore, to use the sun el function library on the JSP page, you must import the JSTL Development Kit and import the EL function library to the page:

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

The following is 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" %>


Result: Found test

 

5. develop custom elfunctions

The process of developing a custom EL function is the same as developing a custom tag:

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 tag library Descriptor (tld) file under the web-inf \ directory and describe the User-Defined 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">


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.