Merge pull request #478 from k0l0ssus/master

JSF Page to demo EL intro
This commit is contained in:
slavisa-baeldung 2016-07-06 11:48:21 +02:00 committed by GitHub
commit e3b1d3a8dc
2 changed files with 156 additions and 0 deletions

View File

@ -0,0 +1,118 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.baeldung.el.controllers;
import java.util.Random;
import javax.annotation.PostConstruct;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;
/**
*
* @author Tayo
*/
@ManagedBean(name = "ELBean")
@ViewScoped
public class ELSampleBean {
private String firstName;
private String lastName;
private String pageDescription = "This page demos JSF EL Basics";
private int pageCounter;
private Random randomIntGen = new Random();
@PostConstruct
public void init() {
pageCounter = randomIntGen.nextInt();
}
public void save(){
}
public void saveFirstName(String firstName) {
this.firstName = firstName;
}
public void saveByELEvaluation(){
firstName = (String)evaluateEL("#{firstName.value}", String.class);
FacesContext ctx = FacesContext.getCurrentInstance();
FacesMessage theMessage = new FacesMessage("Name component Evaluated: "+firstName);
theMessage.setSeverity(FacesMessage.SEVERITY_INFO);
ctx.addMessage(null,theMessage);
}
private Object evaluateEL(String elExpression, Class<?> clazz) {
Object toReturn = null;
FacesContext ctx = FacesContext.getCurrentInstance();
Application app = ctx.getApplication();
toReturn = app.evaluateExpressionGet(ctx, elExpression, clazz);
return toReturn;
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @return the pageDescription
*/
public String getPageDescription() {
return pageDescription;
}
/**
* @param pageDescription the pageDescription to set
*/
public void setPageDescription(String pageDescription) {
this.pageDescription = pageDescription;
}
/**
* @return the pageCounter
*/
public int getPageCounter() {
return pageCounter;
}
/**
* @param pageCounter the pageCounter to set
*/
public void setPageCounter(int pageCounter) {
this.pageCounter = pageCounter;
}
}

View File

@ -0,0 +1,38 @@
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Baeldung | The EL Intro</title>
</h:head>
<h:body>
<h:form id="elForm">
<h:messages />
<h:panelGrid columns="2">
<h:outputText value="First Name"/>
<h:inputText id="firstName" binding="#{firstName}" required="true" value="#{ELBean.firstName}"/>
<h:outputText value="Last Name"/>
<h:inputText id="lastName" required="true" value="#{ELBean.lastName}"/>
<h:outputText value="Save by value binding"/>
<h:commandButton value="Save" action="#{ELBean.save}">
</h:commandButton>
<h:outputText value="Evaluate backing bean EL"/>
<h:commandButton value="Save" action="#{ELBean.saveByELEvaluation}">
</h:commandButton>
<h:outputText value="Save by passing value to method"/>
<h:commandButton value="Save" action="#{ELBean.saveFirstName(firstName.value.toString().concat('(passed)'))}"/>
<h:outputText value="JavaScript (click after saving First Name)"/>
<h:button value="Alert" onclick="alert('Hello #{ELBean.firstName}')"/>
</h:panelGrid>
</h:form>
</h:body>
</html>