Skip to content

Commit b7c563d

Browse files
Merge branch 'pr/582-tayo-jsf-3'
2 parents bea75a0 + 171cf1f commit b7c563d

3 files changed

Lines changed: 59 additions & 5 deletions

File tree

‎jsf/pom.xml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
</dependency>
2828
<dependency>
2929
<groupId>javax.el</groupId>
30-
<artifactId>el-api</artifactId>
30+
<artifactId>javax.el-api</artifactId>
3131
<version>${javax.el.version}</version>
3232
</dependency>
3333

@@ -126,8 +126,8 @@
126126
<org.springframework.version>4.2.5.RELEASE</org.springframework.version>
127127

128128
<!-- JSF -->
129-
<com.sun.faces.version>2.1.7</com.sun.faces.version>
130-
<javax.el.version>2.2</javax.el.version>
129+
<com.sun.faces.version>2.2.13</com.sun.faces.version>
130+
<javax.el.version>3.0.0</javax.el.version>
131131

132132
<!-- logging -->
133133
<org.slf4j.version>1.7.13</org.slf4j.version>

‎jsf/src/main/java/com/baeldung/springintegration/controllers/ELSampleBean.java‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package com.baeldung.springintegration.controllers;
22

3-
import java.util.Random;
43
import javax.annotation.PostConstruct;
4+
import javax.el.ELContextEvent;
5+
import javax.el.ELContextListener;
6+
import javax.el.LambdaExpression;
57
import javax.faces.application.Application;
68
import javax.faces.application.FacesMessage;
9+
import javax.el.LambdaExpression;
710
import javax.faces.bean.ManagedBean;
811
import javax.faces.bean.ViewScoped;
9-
import javax.faces.component.html.HtmlInputText;
1012
import javax.faces.context.FacesContext;
13+
import java.util.Collection;
14+
import java.util.Random;
1115

1216
@ManagedBean(name = "ELBean")
1317
@ViewScoped
@@ -16,22 +20,37 @@ public class ELSampleBean {
1620
private String firstName;
1721
private String lastName;
1822
private String pageDescription = "This page demos JSF EL Basics";
23+
public static final String constantField = "THIS_IS_NOT_CHANGING_ANYTIME_SOON";
1924
private int pageCounter;
2025
private Random randomIntGen = new Random();
2126

2227
@PostConstruct
2328
public void init() {
2429
pageCounter = randomIntGen.nextInt();
30+
FacesContext.getCurrentInstance().getApplication().addELContextListener(new ELContextListener() {
31+
@Override
32+
public void contextCreated(ELContextEvent evt) {
33+
evt.getELContext().getImportHandler().importClass("com.baeldung.springintegration.controllers.ELSampleBean");
34+
}
35+
});
2536
}
2637

2738
public void save() {
2839

2940
}
41+
42+
public static String constantField() {
43+
return constantField;
44+
}
3045

3146
public void saveFirstName(String firstName) {
3247
this.firstName = firstName;
3348
}
3449

50+
public Long multiplyValue(LambdaExpression expr) {
51+
Long theResult = (Long) expr.invoke(FacesContext.getCurrentInstance().getELContext(), pageCounter);
52+
return theResult;
53+
}
3554

3655
public void saveByELEvaluation() {
3756
firstName = (String) evaluateEL("#{firstName.value}", String.class);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version='1.0' encoding='UTF-8' ?>
2+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3+
<html xmlns="http://www.w3.org/1999/xhtml"
4+
xmlns:h="http://java.sun.com/jsf/html"
5+
xmlns:c="http://java.sun.com/jsp/jstl/core">
6+
<h:head>
7+
<title>Baeldung | Expression Language 3.0</title>
8+
</h:head>
9+
<h:body>
10+
<h:outputLabel id="valueLabel" for="valueOutput" value="Composite Lambda Evaluation:"/>
11+
<h:outputText id="valueOutput" value="#{(cube=(x->x*x*x);cube(4))}"/>
12+
<br/>
13+
<h:outputLabel id="staticLabel" for="staticFieldOutput" value="Static Field Output:"/>
14+
<h:outputText id="staticFieldOutput" value="#{ElBean.constantField}"/>
15+
<br/>
16+
<h:outputLabel id="avgLabel" for="avg" value="Average of Integer List Value:"/>
17+
<h:outputText id="avg" value="#{['1','2','3'].stream().average().get()}"/>
18+
<br/>
19+
<h:outputLabel id="lambdaLabel" for="lambdaPass" value="Passing Lambda Expressions:"/>
20+
<h:outputText id="lambdaPass" value="#{ELBean.multiplyValue(x->x*x*x)}"/>
21+
<br/>
22+
<c:set var='pageLevelNumberList' value="#{[1,2,3]}"/>
23+
<h:outputLabel id="avgPageVarLabel" for="avgPageVar" value="Average of Page-Level Integer List Value:"/>
24+
<h:outputText id="avgPageVar" value="#{pageLevelNumberList.stream().average().get()}"/>
25+
<br/>
26+
<h:panelGrid title="Data Structures" border="3" >
27+
<h:dataTable var="streamResult" value="#{pageLevelNumberList.stream().filter(x-> x>1).toList()}">
28+
<h:column id="nameCol">
29+
<h:outputText id="name" value="#{streamResult}"/>
30+
</h:column>
31+
</h:dataTable>
32+
</h:panelGrid>
33+
</h:body>
34+
</html>
35+

0 commit comments

Comments
 (0)