Value and Method Expressions in jsp

Chapter 7 - Building JSP Pages Using the Expression Language (EL)

    Value and Method Expressions in jsp

  • Value and Method Expressions

    The EL are two types of expressions:

    • value expressions
    • method expressions

    Value expressions can either yield a value or set a value.

    Method expressions reference methods that can be invoked and can return a value.

    Value Expressions

    Value expressions can be categorized into rvalue and lvalue expressions. Rvalue expressions can read data but cannot write it. Lvalue expressions can both read and write data.

    All expressions that are evaluated immediately use the ${} delimiters and are always rvalue expressions. Expressions whose evaluation can be deferred use the #{} delimiters and can act as both rvalue and lvalue expressions.

    ${customer.name}
    #{customer.name}
    

    The first expression accesses the name property, gets its value, adds the value to the response, and gets rendered on the page. The same can happen with the second expression.The tag handler can defer the evaluation of this expression to a later time in the page lifecycle.

    In this case, this expression acts as an rvalue expression. During a postback request, this expression can be used to set the value of the name property with user input. In this case, the expression acts as an lvalue expression.

  • Value and Method Expressions in jsp

  • Value and Method Expressions

    The EL are two types of expressions: value expressions and method expressions. Value expressions can either yield a value or set a value.

    Method expressions reference methods that can be invoked and can return a value.

    Value Expressions

    Value expressions can be categorized into rvalue and lvalue expressions. Rvalue expressions can read data but cannot write it. Lvalue expressions can both read and write data.

    All expressions that are evaluated immediately use the ${} delimiters and are always rvalue expressions.

    Expressions whose evaluation can be deferred use the #{} delimiters and can act as both rvalue and lvalue expressions.

    ${customer.name}
    #{customer.name}
    

    The first expression accesses the name property, gets its value, adds the value to the response, and gets rendered on the page.

    The same can happen with the second expression.The tag handler can defer the evaluation of this expression to a later time in the page lifecycle.

    In this case, this expression acts as an rvalue expression. During a postback request, this expression can be used to set the value of the name property with user input. In this case, the expression acts as an lvalue expression.

    Referencing Objects Using Value Expressions

    Both rvalue and lvalue expressions can refer to the following objects and their properties or attributes:

    • JavaBeans components

    • Collections

    • Java SE enumerated types

    • Implicit objects

    To refer to these objects, write an expression using a variable that is the name of the object. The following expression references a managed bean called customer:

    ${customer}
    

    The web container evaluates the expression by its value according to the behavior of PageContext.findAttribute(String), where the String argument is the name of the variable.

    Referring to Object Properties Using Value Expressions

    To refer to properties of a bean or an enum instance, items of a collection, or attributes of an implicit object,use the . or [] notation.

    To reference the name property of the customer bean, use either the expression ${customer.name} or the expression ${customer["name"]}.

    The part inside the brackets is a String literal that is the name of the property to reference.

    use double or single quotes for the String literal.also combine the [] and . notations, as shown as follow:

    ${customer.address["street"]}
    

    Properties of an enum constant can also be referenced.However, as with JavaBeans component properties, the properties of an Enum class must follow JavaBeans component conventions.

    This means that a property must at least have an accessor method called getProperty, where Property is the name of the property that can be referenced by an expression.

    Accessing an item in an array or list,use either a literal value that can be converted to int or the [] notation with an int and without quotes.

    The examples shown  to the same item in a list or array, assume shoes can be converted to int:

    • ${customer.orders[1]}

    • ${customer.orders.shoes}

    In contrast, an item in a Map can be accessed using a string literal key; no coercion is required:

    ${customer.orders["socks"]}

    An rvalue expression also refers directly to values that are not objects, such as the result of arithmetic operations and literal values, as shown by these examples:

    • ${"literal"}

    • ${customer.age + 20}

    • ${true}

    • ${57}

    The EL defines the following literals:

    • Boolean: true and false

    • Integer: as in Java

    • Floating-point: as in Java

    • String: with single and double quotes; " is escaped as \", ' is escaped as \', and \ is escaped as \\

    • Null: null

    Uses of Value Expressions

    Value expressions using the ${} delimiters:

    • Static text

    • Any standard or custom tag attribute that can accept an expression

    The value of an expression in static text is computed and inserted into the current output. Example of an expression embedded in static text:


    < some:tag >
    some text ${expr} some text
    < /some:tag >

    If static text appears in a tag body,then expression will not be evaluated if the body is declared to be tagdependent.

    Lvalue expressions can be used only in tag attributes that can accept lvalue expressions. A tag attribute value using either an rvalue or lvalue expression can be set in the following ways:

    • With a single expression construct:

     < some:tag value="${expr}"/ >
    < another:tag value="#{expr}"/ >

    • With one or more expressions separated or surrounded by text:

     < some:tag value="some${expr}${expr}text${expr}"/ >
    < another:tag value="some#{expr}#{expr}text#{expr}"/ >

    These kinds of expression, called composite expressions, are evaluated from left to right.

    Each expression embedded in the composite expression is converted to a String and then concatenated with any intervening text.

    • With text only:

     
    < some:tag value="sometext"/ >

    This expression is called a literal expression.The attribute String value is converted to the attribute’s expected type. Literal value expressions have special syntax rules.

    Method Expressions

    A method expression is an expression used to invoke an arbitrary public method of a bean, which can return as a result.

    In JavaServer Faces,The component tag uses method expressions to invoke methods that perform some processing for the component. These methods are necessary for handling events that the components generate and for validating component data.

    Example:

     < h:form >
    < h:inputText
    id="name"
    value="#{customer.name}"
    validator="#{customer.validateName}"/ >
    < h:commandButton
    id="submit"
    action="#{customer.submit}" / >
    < /h:form >

    Method expressions must always use the deferred evaluation syntax.Like lvalue expressions, method expressions can use the . and the [] operators.

    For example, #{object.method} is equivalent to #{object["method"]}. The literal inside the [] is converted to String and is used to find the name of the method that matches it.

    Method expressions can be used only in tag attributes as follows:

    • With a single expression construct, where bean refers to a JavaBeans component and method refers to a method of the JavaBeans component:

     < some:tag value="#{bean.method}"/ >

    The expression is evaluated to a method expression, which is passed to the tag handler. The method represented by the method expression can then be invoked later.

    • With text only:

     < some:tag value="sometext"/ >

    When the method referenced by this method expression is invoked, the method returns the String literal, which is then converted to the expected return type, as defined in the tag’s tag library descriptor.

    Parameterized Method Calls

    Method calls can use parameters without having to use static EL functions.

    Both the . and [] operators can be used for invoking method calls with parameters. Expression Syntax:

    • expr-a[expr-b](parameters)
    
    • expr-a.identifier-b(parameters)
    

    In the first expression syntax, expr-a is evaluated to represent a bean object. The expression expr-b is evaluated and cast to a string that represents a method in the bean represented by expr-a.

    In the second expression syntax, expr-a is evaluated to represent a bean object, and identifier-b is a string that represents a method in the bean object.

    The parameters in parentheses are the arguments for the method invocation. Parameters can be zero or more values or expressions, separated by commas.

© 2015 by Learncertification All Rights Reserved. The certification names are the trademarks of their respective owners. Terms & Privacy Policy