jsf el 表达式_JSF表达式语言– JSF EL

jsf el 表达式

JSF Expression Language enables users to access the data dynamically from the JavaBeans components using various expressions.

JSF表达式语言使用户可以使用各种表达式从JavaBeans组件动态访问数据。

JSF表达语言 (JSF Expression Language)

JSF Expression Language or JSF EL supports the following kinds of expressions.

JSF表达式语言或JSF EL支持以下类型的表达式。

  1. Immediate value expressions or Deferred value expressions

    立即值表达式或递延值表达式
  2. Value expression or method expression

    值表达式或方法表达式
  3. rvalue or lvalue expressions

    右值或左值表达式

JSF EL –立即值表达式 (JSF EL – Immediate value expressions)

Immediate expressions are evaluated and results are rendered as soon as the page is displayed initially. The syntax for immediate evaluation is

最初显示该页面时,将立即评估立即表达式并呈现结果。 立即评估的语法是

${}

${}

Immediate evaluation expressions can be used within a template text or as tag attribute value that accepts runtime expressions. These type of expressions are read-only.

即时评估表达式可以在模板文本中使用,也可以用作接受运行时表达式的标记属性值。 这些类型的表达式是只读的。

JSF EL –延迟值表达式 (JSF EL – Deferred value expressions)

Deferred expressions are evaluated during the lifecycle phase whenever it is needed. The syntax for deferred evaluation is

在生命周期阶段,只要需要,便会评估延期表达式。 延迟评估的语法为

#{expression}

#{expression}

Deferred value expressions can be of two types

延迟值表达式可以有两种类型

  1. Value expressions

    值表达
  2. Method expressions

    方法表达式

JSF表达式语言–值表达式 (JSF Expression Language – Value Expressions)

Value expressions usually fetch a value or set a value. These expressions can be further categorized into rvalue and lvalue expressions.

值表达式通常获取值或设置值。 这些表达式可以进一步分为右值和左值表达式。

lvalue expressions can both read and write data whereas rvalue expressions can only read data.

左值表达式可以读取和写入数据,而右值表达式只能读取数据。

For example, if we want to fetch the name of the car from the managed bean

例如,如果我们想从托管bean中获取汽车的名称

${car.cname}: for immediate evaluation

$ {car.cname} :立即评估

#{car.cname}: for deferred evaluation

#{car.cname} :用于延迟评估

The immediate evaluation ${car.cname} acts as an rvalue expression whereas in some cases during a request this expression can be used to set the value to the cname field and hence acts as an lvalue expression.

立即计算$ {car.cname}用作右值表达式,而在某些情况下,在请求期间,该表达式可用于将值设置为cname字段,因此用作左值表达式。

JSF EL –使用值表达式引用对象 (JSF EL – Referencing objects with value expressions)

The value expressions can be referred to managed bean components, collections, enumeration types and implicit objects types.

可以将值表达式称为托管bean组件,集合,枚举类型和隐式对象类型。

For example : ${car}

例如:$ {car}

This expression refers to the managed bean component named “car”. While evaluating this expression the container searches for car in the page, request or session and returns the value. If the car is not found then the value returned is null.

此表达式引用名为“ car”的托管Bean组件。 评估此表达式时,容器在页面,请求或会话中搜索car并返回值。 如果找不到汽车,则返回的值为null。

Assuming there is list named carnames containing the following values,

假设有一个名为carnames的列表包含以下值,

public List carnames={"Santro","Zen","Polo","Innova"}

public List carnames={"Santro","Zen","Polo","Innova"}

Suppose if we want to access the car named Polo then we use the expression

假设如果要访问名为Polo的汽车,则使用表达式

${carnames [2]}

$ {carnames [2]}

where [2] refers to the third element in the list with value “Polo”

其中[2]表示列表中的第三个元素,其值为“ Polo”

JSF EL –使用值表达式引用对象属性 (JSF EL – Referring object properties using value expressions)

To access the managed bean properties, elements in a collection or implicit objects we use . or [] notation.

要访问托管bean属性,集合中的元素或隐式对象,我们使用。 或[]表示法。

If we consider an example where we want to access the cname of the car managed bean then we use the expression

如果我们考虑一个要访问汽车托管bean的cname的示例,那么我们使用以下表达式

${car.cname} or ${car[“cname”]}

$ {car.cname}或$ {car [“ cname”]}

If we want to access the elements in an array or a collection then we use

如果我们要访问数组或集合中的元素,则使用

${car.carnames[3]}

$ {car.carnames [3]}

where car is the managed bean and carnames is the list from which we are accessing the third element. A literal value can be used that can be converted to int without quotes as ${car.carnames.nval} assuming that nval can be converted to int data type.

其中car是托管bean,而carnames是我们要访问的第三个元素的列表。 假定可以将nval转换为int数据类型,则可以使用可以将其转换为int且不带引号的$ {car.carnames.nval}文字值。

In order to access element from a Map, then the quotes should be specified as

为了从地图访问元素,应将引号指定为

${car.cnames[“nval”]} where car is the bean, cnames is a map.

$ {car.cnames [“ nval”]}其中car是bean,cnames是地图。

The EL supports the Boolean, Integer, Floating, null and String data types.

EL支持Boolean,Integer,Floating,null和String数据类型。

JSF表达式语言–方法表达式 (JSF Expressions Language – Method Expressions)

JSF EL method expression allows user to invoke a public method of the bean that returns the result. Method expressions are necessary for validating the data component and handling events.

JSF EL方法表达式允许用户调用返回结果的bean的公共方法。 方法表达式对于验证数据组件和处理事件是必需的。

Consider an example of invoking a method in a bean

考虑在Bean中调用方法的示例

<h:commandButton id="sub" value="Add Details" action="#{car.add}"> </h:commandButtton>

Here on click of “Add Details” button we invoke the add method of the car bean.

在这里,单击“添加详细信息”按钮,我们将调用car bean的add方法。

Method expressions can use . or [] operator as #{car.add} or #{car[“add”]}.

方法表达式可以使用。 或[]运算符为#{car.add}或#{car [“ add”]}。

JSF EL参数化方法 (JSF EL Parameterized methods)

The Expression Language supports the methods with parameters. Parameters are supported for both value and method expressions.
Consider an example of how to invoke a method with parameters.

表达式语言支持带参数的方法。 值和方法表达式均支持参数。
考虑一个如何使用参数调用方法的示例。

<h:inputText id="speed" value="#{car.calculatespeed(‘70.2’)}"></h:inputText>

Here we are calling the calculatespeed method of bean car by passing parameter speed having the value 70.2.

在这里,我们通过传递值为70.2的参数speed来调用bean car的computespeed方法。

Now let’s see a simple example where we will see the use of JSF Expression Language.

现在,让我们看一个简单的示例,在该示例中,我们将看到JSF表达式语言的使用。

Create a JSF page car.xhtml as shown below.

创建一个JSF页面car.xhtml ,如下所示。

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml"
	xmlns:h="https://xmlns.jcp.org/jsf/html">
<h:head>
	<title>Facelet Title</title>
</h:head>
<h:body>
        Car Name:#{car.cname}
        <br>
	<br>
      
        Color:#{car.color}
        <br>
	<br>
        
        Model Number:#{car.modelno}
        <br>
	<br>
        
        Speed:#{car.calculatespeed(45.20)}
       
    </h:body>
</html>

Here we are invoking the calculate speed method by passing speed value 45.20 and fetching the values for cname, color and modelno attributes.

在这里,我们通过传递速度值45.20并获取cname,color和modelno属性的值来调用计算速度方法。

Create managed bean Car.java as

创建托管bean Car.java

package com.journaldev.jsf.beans;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class Car {

	private String cname = "Polo";
	private String color = "White";
	private String modelno = "P245";

	public Car() {
	}

	public Car(String cname, String color, String modelno) {
		this.cname = cname;
		this.color = color;
		this.modelno = modelno;
	}

	public String getCname() {
		return cname;
	}

	public void setCname(String cname) {
		this.cname = cname;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public String getModelno() {
		return modelno;
	}

	public void setModelno(String modelno) {
		this.modelno = modelno;
	}

	public float calculatespeed(float speed) {

		float totalspeed = speed + 10;
		return totalspeed;

	}

}

Now run the application to see the following output in the browser.

现在运行该应用程序以在浏览器中查看以下输出。

Below image shows the final project structure in Eclipse.

JSF EL Example

Finally, you can download the JSF EL example project from below link and play around with it to learn more.

下图显示了Eclipse中的最终项目结构。

最后,您可以从下面的链接下载JSF EL示例项目,并试用它来了解更多信息。

Reference: Oracle Documentation

参考: Oracle文档

翻译自: https://www.journaldev.com/7058/jsf-expression-language-jsf-el

jsf el 表达式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JSF中,EL表达式语言)是一种用于访问后台JavaBean的属性和方法的语法。以下是一些常用的EL表达式: 1. 访问JavaBean的属性: ``` #{bean.property} ``` 在上面的示例中,#{bean.property}用于访问JavaBean中的property属性。 2. 调用JavaBean中的方法: ``` #{bean.method()} ``` 在上面的示例中,#{bean.method()}用于调用JavaBean中的method方法。 3. 访问JavaBean中的属性,通过另一个属性或方法: ``` #{bean.property1.property2.property3} #{bean.method1().method2().property} ``` 在上面的示例中,#{bean.property1.property2.property3}用于访问JavaBean中的嵌套属性,#{bean.method1().method2().property}用于通过方法链访问JavaBean中的属性。 4. 在EL表达式中使用运算符: ``` #{bean.property1 + bean.property2} #{bean.property1 > bean.property2} #{bean.property1 == bean.property2} ``` 在上面的示例中,EL表达式中使用了加法、比较等运算符,可以对JavaBean中的属性进行计算和比较。 5. 在EL表达式中使用条件语句: ``` #{bean.visible ? 'visible' : 'hidden'} #{bean.property1 gt bean.property2 ? 'larger' : 'smaller'} ``` 在上面的示例中,EL表达式中使用了三元运算符,可以根据条件返回不同的值。 6. 访问JSF组件的属性: ``` #{component.property} ``` 在上面的示例中,#{}表达式用于访问JSF组件中的property属性。 7. 在EL表达式中使用注释: ``` #{'<!-- My comment -->'} ``` 在上面的示例中,#{}表达式用于在页面上添加注释。 以上是一些常用的EL表达式,但并不是全部,EL表达式JSF中非常重要的一个特性,可以实现页面与后台JavaBean的无缝集成,使得开发Web应用程序变得更加简单和高效。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值