Skip to content

Commit e3b1d3a

Browse files
Merge pull request eugenp#478 from k0l0ssus/master
JSF Page to demo EL intro
2 parents 10b5780 + 8a19546 commit e3b1d3a

2 files changed

Lines changed: 156 additions & 0 deletions

File tree

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* To change this template, choose Tools | Templates
3+
* and open the template in the editor.
4+
*/
5+
package com.baeldung.el.controllers;
6+
7+
import java.util.Random;
8+
import javax.annotation.PostConstruct;
9+
import javax.faces.application.Application;
10+
import javax.faces.application.FacesMessage;
11+
import javax.faces.bean.ManagedBean;
12+
import javax.faces.bean.ViewScoped;
13+
import javax.faces.component.html.HtmlInputText;
14+
import javax.faces.context.FacesContext;
15+
16+
/**
17+
*
18+
* @author Tayo
19+
*/
20+
@ManagedBean(name = "ELBean")
21+
@ViewScoped
22+
public class ELSampleBean {
23+
24+
private String firstName;
25+
private String lastName;
26+
private String pageDescription = "This page demos JSF EL Basics";
27+
private int pageCounter;
28+
private Random randomIntGen = new Random();
29+
30+
@PostConstruct
31+
public void init() {
32+
pageCounter = randomIntGen.nextInt();
33+
}
34+
35+
public void save(){
36+
37+
}
38+
39+
public void saveFirstName(String firstName) {
40+
this.firstName = firstName;
41+
}
42+
43+
44+
public void saveByELEvaluation(){
45+
firstName = (String)evaluateEL("#{firstName.value}", String.class);
46+
FacesContext ctx = FacesContext.getCurrentInstance();
47+
FacesMessage theMessage = new FacesMessage("Name component Evaluated: "+firstName);
48+
theMessage.setSeverity(FacesMessage.SEVERITY_INFO);
49+
ctx.addMessage(null,theMessage);
50+
51+
}
52+
53+
private Object evaluateEL(String elExpression, Class<?> clazz) {
54+
Object toReturn = null;
55+
FacesContext ctx = FacesContext.getCurrentInstance();
56+
Application app = ctx.getApplication();
57+
toReturn = app.evaluateExpressionGet(ctx, elExpression, clazz);
58+
59+
return toReturn;
60+
61+
}
62+
63+
/**
64+
* @return the firstName
65+
*/
66+
public String getFirstName() {
67+
return firstName;
68+
}
69+
70+
/**
71+
* @param firstName the firstName to set
72+
*/
73+
public void setFirstName(String firstName) {
74+
this.firstName = firstName;
75+
}
76+
77+
/**
78+
* @return the lastName
79+
*/
80+
public String getLastName() {
81+
return lastName;
82+
}
83+
84+
/**
85+
* @param lastName the lastName to set
86+
*/
87+
public void setLastName(String lastName) {
88+
this.lastName = lastName;
89+
}
90+
91+
/**
92+
* @return the pageDescription
93+
*/
94+
public String getPageDescription() {
95+
return pageDescription;
96+
}
97+
98+
/**
99+
* @param pageDescription the pageDescription to set
100+
*/
101+
public void setPageDescription(String pageDescription) {
102+
this.pageDescription = pageDescription;
103+
}
104+
105+
/**
106+
* @return the pageCounter
107+
*/
108+
public int getPageCounter() {
109+
return pageCounter;
110+
}
111+
112+
/**
113+
* @param pageCounter the pageCounter to set
114+
*/
115+
public void setPageCounter(int pageCounter) {
116+
this.pageCounter = pageCounter;
117+
}
118+
}

‎jsf/src/main/webapp/el_intro.xhtml‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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:f="http://java.sun.com/jsf/core">
6+
<h:head>
7+
<title>Baeldung | The EL Intro</title>
8+
9+
</h:head>
10+
11+
<h:body>
12+
<h:form id="elForm">
13+
14+
15+
<h:messages />
16+
<h:panelGrid columns="2">
17+
<h:outputText value="First Name"/>
18+
<h:inputText id="firstName" binding="#{firstName}" required="true" value="#{ELBean.firstName}"/>
19+
<h:outputText value="Last Name"/>
20+
<h:inputText id="lastName" required="true" value="#{ELBean.lastName}"/>
21+
<h:outputText value="Save by value binding"/>
22+
<h:commandButton value="Save" action="#{ELBean.save}">
23+
24+
</h:commandButton>
25+
<h:outputText value="Evaluate backing bean EL"/>
26+
<h:commandButton value="Save" action="#{ELBean.saveByELEvaluation}">
27+
28+
</h:commandButton>
29+
<h:outputText value="Save by passing value to method"/>
30+
<h:commandButton value="Save" action="#{ELBean.saveFirstName(firstName.value.toString().concat('(passed)'))}"/>
31+
<h:outputText value="JavaScript (click after saving First Name)"/>
32+
<h:button value="Alert" onclick="alert('Hello #{ELBean.firstName}')"/>
33+
</h:panelGrid>
34+
35+
</h:form>
36+
</h:body>
37+
</html>
38+

0 commit comments

Comments
 (0)